-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
430 lines (362 loc) · 12 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// cpmulator entry-point / driver
package main
import (
"context"
"flag"
"fmt"
"log/slog"
"os"
"slices"
"sort"
"strings"
"time"
cpmccp "github.com/skx/cpmulator/ccp"
"github.com/skx/cpmulator/consolein"
"github.com/skx/cpmulator/consoleout"
"github.com/skx/cpmulator/cpm"
"github.com/skx/cpmulator/static"
cpmver "github.com/skx/cpmulator/version"
)
var (
// log holds our logging handle
log *slog.Logger
)
// Recovery is good
func recoverPanic() {
if r := recover(); r != nil {
out := fmt.Sprintf("%s", r)
fmt.Printf("recovered from panic while running %v\r\n%s\r\n\r\n", os.Args, out)
if strings.Contains(out, "termbox") {
fmt.Printf("\r\nThis error seems related to terminal/console handling.\r\n")
fmt.Printf("\r\nIf you're on a Unix-like system you might try '-input stty' to change input-handler and see if that helps\r\n")
}
fmt.Printf("\r\n\r\nIf this error persists please report a bug:")
fmt.Printf("\r\n\r\n https://github.com/skx/cpmulator/issues/new\r\n\r\n")
}
}
// main is our entry point
func main() {
//
// Catch errors
//
defer recoverPanic()
//
// Parse the command-line flags for this driver-application
//
ccp := flag.String("ccp", "ccpz", "The name of the CCP that we should run (ccp vs. ccpz).")
cd := flag.String("cd", "", "Change to this directory before launching")
createDirectories := flag.Bool("create", false, "Create subdirectories on the host computer for each CP/M drive.")
embedBin := flag.Bool("embed", true, "Should we embed our utility commands into the A: filesystem.")
execPrefix := flag.String("exec-prefix", "", "Execute system commands, on the host, prefixed by this string.")
input := flag.String("input", cpm.DefaultInputDriver, "The name of the console input driver to use (-list-input-drivers will show valid choices).")
output := flag.String("output", cpm.DefaultOutputDriver, "The name of the console output driver to use (-list-output-drivers will show valid choices).")
logAll := flag.Bool("log-all", false, "Log all function invocations, including the noisy console I/O ones.")
logPath := flag.String("log-path", "", "Specify the file to write debug logs to.")
prnPath := flag.String("prn-path", "print.log", "Specify the file to write printer-output to.")
showVersion := flag.Bool("version", false, "Report our version, and exit.")
timeout := flag.Int("timeout", 0, "Timeout execution after the given number of seconds")
useDirectories := flag.Bool("directories", false, "Use subdirectories on the host computer for CP/M drives.")
// listing
listCcps := flag.Bool("list-ccp", false, "Dump the list of embedded CCPs, and exit.")
listOutput := flag.Bool("list-output-drivers", false, "Dump the list of valid console output drivers, and exit.")
listInput := flag.Bool("list-input-drivers", false, "Dump the list of valid console input drivers, and exit.")
listSyscalls := flag.Bool("list-syscalls", false, "Dump the list of implemented BIOS/BDOS syscall functions, and exit.")
// drives
drive := make(map[string]*string)
drive["A"] = flag.String("drive-a", "", "The path to the directory for A:")
drive["B"] = flag.String("drive-b", "", "The path to the directory for B:")
drive["C"] = flag.String("drive-c", "", "The path to the directory for C:")
drive["D"] = flag.String("drive-d", "", "The path to the directory for D:")
drive["E"] = flag.String("drive-e", "", "The path to the directory for E:")
drive["F"] = flag.String("drive-f", "", "The path to the directory for F:")
drive["G"] = flag.String("drive-g", "", "The path to the directory for G:")
drive["H"] = flag.String("drive-h", "", "The path to the directory for H:")
drive["I"] = flag.String("drive-i", "", "The path to the directory for I:")
drive["J"] = flag.String("drive-j", "", "The path to the directory for J:")
drive["K"] = flag.String("drive-k", "", "The path to the directory for K:")
drive["L"] = flag.String("drive-l", "", "The path to the directory for L:")
drive["M"] = flag.String("drive-m", "", "The path to the directory for M:")
drive["N"] = flag.String("drive-n", "", "The path to the directory for N:")
drive["O"] = flag.String("drive-o", "", "The path to the directory for O:")
drive["P"] = flag.String("drive-p", "", "The path to the directory for P:")
flag.Parse()
// Are we dumping CCPs?
if *listCcps {
x := cpmccp.GetAll()
for _, x := range x {
fmt.Printf("%5s %-10s %04X bytes, entry-point %04X\n", x.Name, x.Description, len(x.Bytes), x.Start)
}
return
}
// Are we dumping console input drivers?
if *listInput {
obj, _ := consolein.New("null")
valid := obj.GetDrivers()
slices.Sort(valid)
for _, name := range valid {
suffix := ""
if name == cpm.DefaultInputDriver {
suffix = "\t[default]"
}
fmt.Printf("%s%s\n", name, suffix)
}
return
}
// Are we dumping console output drivers?
if *listOutput {
obj, _ := consoleout.New("null")
valid := obj.GetDrivers()
slices.Sort(valid)
for _, name := range valid {
suffix := ""
if name == cpm.DefaultOutputDriver {
suffix = "\t[default]"
}
fmt.Printf("%s%s\n", name, suffix)
}
return
}
// Are we dumping syscalls?
if *listSyscalls {
// dumper is a helper to dump the contents of
// the given map in a human readable fashion.
dumper := func(name string, arg map[uint8]cpm.CPMHandler) {
// Get the syscalls in sorted order
ids := []int{}
for i := range arg {
ids = append(ids, int(i))
}
sort.Ints(ids)
// Now show them.
fmt.Printf("%s syscalls:\n", name)
for _, id := range ids {
ent := arg[uint8(id)]
fake := ""
if ent.Fake {
fake = "FAKE"
}
fmt.Printf("\t%03d %-20s %s\n", int(id), ent.Desc, fake)
}
}
// Create helper - with defaults.
c, err := cpm.New()
if err != nil {
fmt.Printf("error creating CPM object: %s\n", err)
return
}
dumper("BDOS", c.BDOSSyscalls)
dumper("BIOS", c.BIOSSyscalls)
return
}
// show version
if *showVersion {
fmt.Printf("%s\n", cpmver.GetVersionBanner())
return
}
// Default program to execute, and arguments to pass to program
program := ""
args := []string{}
// If we have a program
if len(flag.Args()) != 0 {
program = flag.Args()[0]
if len(flag.Args()) > 1 {
args = flag.Args()[1:]
}
}
// Setup our logging level - default to warnings or higher.
lvl := new(slog.LevelVar)
lvl.Set(slog.LevelWarn)
// The default log behaviour is to show critical issues to STDERR
logFile := os.Stderr
// But if we have a logfile, we'll write there
if *logPath != "" {
var err error
logFile, err = os.OpenFile(*logPath, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Printf("failed to open logfile for writing %s:%s\n", *logPath, err)
return
}
// And that will trigger more verbose output
lvl.Set(slog.LevelDebug)
defer logFile.Close()
}
ctx := context.Background()
var cancel context.CancelFunc
if *timeout != 0 {
ctx, cancel = context.WithTimeout(context.Background(), time.Duration(*timeout)*time.Second)
defer cancel()
}
// Create our logging handler, using the level we've just setup.
log = slog.New(
slog.NewJSONHandler(
logFile,
&slog.HandlerOptions{
Level: lvl,
}))
// Set the logger now we've updated as appropriate.
slog.SetDefault(log)
// Create a new emulator.
obj, err := cpm.New(cpm.WithPrinterPath(*prnPath),
cpm.WithOutputDriver(*output),
cpm.WithInputDriver(*input),
cpm.WithHostExec(*execPrefix),
cpm.WithContext(ctx),
cpm.WithCCP(*ccp))
if err != nil {
fmt.Printf("error creating CPM object: %s\n", err)
return
}
// Are we logging noisy functions?
if *logAll {
obj.LogNoisy()
}
// I/O SETUP
err = obj.IOSetup()
if err != nil {
fmt.Printf("Error setting up the console input driver %s:%s", *input, err)
return
}
// I/O TearDown
// When we're finishing we'll reset some (console) state.
defer func() {
err := obj.IOTearDown()
if err != nil {
fmt.Printf("Error cleaning up console input driver %s:%s", *input, err)
}
}()
// change directory?
//
// NOTE: We deliberately do this after setting up the logfile.
if *cd != "" {
err := os.Chdir(*cd)
if err != nil {
fmt.Printf("failed to change to %s:%s\n", *cd, err)
return
}
}
// Should we create child-directories? If so, do so.
//
// NOTE: This is also done deliberately after the changing
// of directory.
if *createDirectories {
for _, d := range []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P"} {
if _, err := os.Stat(d); os.IsNotExist(err) {
_ = os.Mkdir(d, 0755)
}
}
}
// Load any embedded files within our binary
if *embedBin {
obj.SetStaticFilesystem(static.GetContent())
} else {
obj.SetStaticFilesystem(static.GetEmptyContent())
}
// Default to not using subdirectories for drives
obj.SetDrives(false)
// Are we using drives?
if *useDirectories {
// Enable the use of directories.
obj.SetDrives(true)
// Count how many drives exist - if zero show a warning
found := 0
for _, d := range []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P"} {
if _, err := os.Stat(d); err == nil {
found++
}
}
if found == 0 {
fmt.Printf("WARNING: You've chosen to use subdirectories as drives.\r\n")
fmt.Printf(" i.e. A/ would be used for the contents of A:\r\n")
fmt.Printf(" i.e. B/ would be used for the contents of B:\r\n")
fmt.Printf("\r\n")
fmt.Printf(" However no drive-directories are present!\r\n")
fmt.Printf("\r\n")
fmt.Printf("You could fix this, like so:\r\n")
fmt.Printf(" mkdir A\r\n")
fmt.Printf(" mkdir B\r\n")
fmt.Printf(" mkdir C\r\n")
fmt.Printf(" etc\r\n")
fmt.Printf("\r\n")
fmt.Printf("Or you could launch this program with the '-create' flag.\r\n")
fmt.Printf("That would automatically create directories for drives A-P.\r\n")
}
}
// Do we have custom paths? If so set them.
for d, pth := range drive {
if pth != nil && *pth != "" {
obj.SetDrivePath(d, *pth)
}
}
// Load the binary, if we were given one.
if program != "" {
err := obj.LoadBinary(program)
if err != nil {
fmt.Printf("Error loading program %s:%s\n", program, err)
return
}
err = obj.Execute(args)
if err != nil {
// Deliberate stop of execution
if err == cpm.ErrHalt {
fmt.Printf("\r\n")
return
}
// Reboot attempt, also fine
if err == cpm.ErrBoot {
fmt.Printf("\r\n")
return
}
if err == cpm.ErrTimeout {
fmt.Printf("\r\nThe timeout of %d seconds was exceeded. Terminating!\r\n", *timeout)
return
}
fmt.Printf("Error running %s [%s]: %s\n",
program, strings.Join(args, ","), err)
}
fmt.Printf("\n")
return
}
// Show a startup-banner.
fmt.Printf("\ncpmulator %s\r\nConsole input:%s Console output:%s BIOS:0x%04X BDOS:0x%04X CCP:%s\n", cpmver.GetVersionString(), obj.GetInputDriver().GetName(), obj.GetOutputDriver().GetName(), obj.GetBIOSAddress(), obj.GetBDOSAddress(), obj.GetCCPName())
// We will load AUTOEXEC.SUB, once, if it exists (*)
//
// * - Terms and conditions apply.
obj.RunAutoExec()
// We load and re-run eternally - because many binaries the CCP
// would launch would end with "exit" which would otherwise cause
// our emulation to terminate
//
// Large binaries would also overwrite the CCP in RAM, so we can't
// just jump back to the entry-point for that.
//
for {
// Load the CCP binary - resetting RAM in the process.
err := obj.LoadCCP()
if err != nil {
fmt.Printf("error loading CCP: %s\n", err)
return
}
// Run the CCP, which will often load a child-binary.
// The child-binary will call "P_TERMCPM" which will cause
// the CCP to terminate.
err = obj.Execute(args)
if err != nil {
// Start the loop again, which will reload the CCP
// and jump to it. Effectively rebooting.
if err == cpm.ErrBoot {
continue
}
// Deliberate stop of execution.
if err == cpm.ErrHalt {
fmt.Printf("\n")
return
}
if err == cpm.ErrTimeout {
fmt.Printf("\r\nThe timeout of %d seconds was exceeded. Terminating!\r\n", *timeout)
return
}
fmt.Printf("\nError running CCP: %s\n", err)
return
}
}
}