From da5de5164fa234fb950fb3971a1b92221986398c Mon Sep 17 00:00:00 2001 From: Ivan Izaguirre Date: Thu, 9 Sep 2021 20:47:14 +0200 Subject: [PATCH] Support for acceptance tests --- bbz.go | 3 +++ consoleMock.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ environment.go | 21 +++++++++++++-------- main.go | 18 ++++++------------ osCLI.go | 18 ++++++++++-------- osCLI_test.go | 25 +++++++++++++++++++++++++ vdu.go | 10 +++++----- 7 files changed, 106 insertions(+), 33 deletions(-) create mode 100644 consoleMock.go create mode 100644 osCLI_test.go diff --git a/bbz.go b/bbz.go index 6b9e636..c49c904 100644 --- a/bbz.go +++ b/bbz.go @@ -14,6 +14,9 @@ import ( */ func RunMOS(env *environment) { + + env.initUpperLanguage() + // Execute for !env.stop { env.cpu.ExecuteInstruction() diff --git a/consoleMock.go b/consoleMock.go new file mode 100644 index 0000000..1dd802a --- /dev/null +++ b/consoleMock.go @@ -0,0 +1,44 @@ +package main + +type consoleMock struct { + linesIn []string + lineIn int + output string + env *environment +} + +func newConsoleMock(env *environment, linesIn []string) *consoleMock { + var c consoleMock + c.linesIn = linesIn + c.output = "" + c.env = env + return &c +} + +func (c *consoleMock) readline() (string, bool) { + if c.lineIn >= len(c.linesIn) { + c.env.stop = true + return "", true + } + line := c.linesIn[c.lineIn] + c.env.writeSpool(line) + c.env.writeSpool("\n") + c.lineIn++ + return line, false +} + +func (c *consoleMock) readChar() (uint8, bool) { + s, stop := c.readline() + if s == "" { + return ' ', stop + } else { + return s[0], stop + } +} + +func (c *consoleMock) write(s string) { + c.output += s + c.env.writeSpool(s) +} + +func (c *consoleMock) close() {} diff --git a/environment.go b/environment.go index 7e6a519..b9120e5 100644 --- a/environment.go +++ b/environment.go @@ -34,13 +34,8 @@ type environment struct { panicOnErr bool } -func newEnvironment(cpuLog bool, apiLog bool, apiLogIO bool, memLog bool, panicOnErr bool, rawline bool) *environment { +func newEnvironment(roms []*string, cpuLog bool, apiLog bool, apiLogIO bool, memLog bool, panicOnErr bool) *environment { var env environment - if rawline { - env.con = newConsoleSimple(&env) - } else { - env.con = newConsoleLiner(&env) - } env.referenceTime = time.Now() env.timer = 0 env.lastTimerUpdate = time.Now() @@ -49,10 +44,20 @@ func newEnvironment(cpuLog bool, apiLog bool, apiLogIO bool, memLog bool, panicO env.cpu = core6502.NewNMOS6502(env.mem) //env.cpu = core6502.NewCMOS65c02(env.mem) env.cpu.SetTrace(cpuLog) - env.vdu = newVdu(env.con) + env.vdu = newVdu(&env) env.apiLog = apiLog env.apiLogIO = apiLogIO env.panicOnErr = panicOnErr + + env.mem.loadFirmware() + + for i, rom := range roms { + if *rom != "" { + env.mem.loadRom(*rom, uint8(0xf-i)) + } + } + env.mem.completeWithRam() + return &env } @@ -98,7 +103,7 @@ func (env *environment) initLanguage(slot uint8) { The MOS also automatically prints the ROM's title string (&8009) so that the user is acknowledged. */ language := env.mem.peekString(romTitleString, 0) - fmt.Printf("%s\n", language) + env.con.write(fmt.Sprintf("%s\n", language)) _, x, y, p := env.cpu.GetAXYP() env.cpu.SetAXYP(1, x, y, p) diff --git a/main.go b/main.go index c6bc573..9ef7676 100644 --- a/main.go +++ b/main.go @@ -58,26 +58,20 @@ func main() { } - env := newEnvironment(*traceCPU, + env := newEnvironment(roms, *traceCPU, (*traceMOS) || (*traceMOSFull), *traceMOSFull, *traceMemory, - *panicOnErr, - *rawline) + *panicOnErr) defer env.close() handleControlC(env) - env.mem.loadFirmware() - - for i, rom := range roms { - if *rom != "" { - env.mem.loadRom(*rom, uint8(0xf-i)) - } + if *rawline { + env.con = newConsoleSimple(env) + } else { + env.con = newConsoleLiner(env) } - env.initUpperLanguage() - env.mem.completeWithRam() - RunMOS(env) } diff --git a/osCLI.go b/osCLI.go index 5d929c4..4f68738 100644 --- a/osCLI.go +++ b/osCLI.go @@ -117,7 +117,7 @@ func execOSCLI(env *environment) { case "CAT": // TODO - fmt.Println("\n<>") + env.con.write("\n<>\n") case "CODE": execOSCLIfx(env, 0x88, line, pos) @@ -175,7 +175,7 @@ func execOSCLI(env *environment) { //case "EXEC": case "HELP": - fmt.Println("\nBBZ 0.0") + env.con.write("\nBBZ 0.0\n") // Send to the other ROMS if available. env.mem.pokeWord(zpStr, xy) @@ -195,7 +195,8 @@ func execOSCLI(env *environment) { if err != nil { env.raiseError(errorTodo, err.Error()) } - fmt.Println(string(stdout)) + env.con.write(string(stdout)) + env.con.write("\n") case "INFO": filename := "" @@ -207,9 +208,10 @@ func execOSCLI(env *environment) { attr := getFileAttributes(env, filename) if attr.hasMetadata { - fmt.Printf("%s\t %06X %06X %06X\n", filename, attr.loadAddress, attr.executionAddress, attr.fileSize) + env.con.write(fmt.Sprintf("%s\t %06X %06X %06X\n", filename, + attr.loadAddress, attr.executionAddress, attr.fileSize)) } else { - fmt.Printf("%s\t ?????? ?????? %06X\n", filename, attr.fileSize) + env.con.write(fmt.Sprintf("%s\t ?????? ?????? %06X\n", filename, attr.fileSize)) } // case "KEY": @@ -267,7 +269,7 @@ func execOSCLI(env *environment) { env.mem.Poke(sheilaRomLatch, uint8(i)) name := env.mem.peekString(romTitleString, 0) if name == "" { - fmt.Printf("ROM %X ?\n", i) + env.con.write(fmt.Sprintf("ROM %X ?\n", i)) } else { version := env.mem.Peek(romVersion) romType := env.mem.Peek(romTypeByte) @@ -280,10 +282,10 @@ func execOSCLI(env *environment) { } attributes += ")" - fmt.Printf("ROM %X %s %02v %s\n", i, name, version, attributes) + env.con.write(fmt.Sprintf("ROM %X %s %02v %s\n", i, name, version, attributes)) } } else { - fmt.Printf("RAM %X 16K\n", i) + env.con.write(fmt.Sprintf("RAM %X 16K\n", i)) } } env.mem.Poke(sheilaRomLatch, selectedROM) diff --git a/osCLI_test.go b/osCLI_test.go new file mode 100644 index 0000000..d0b4a63 --- /dev/null +++ b/osCLI_test.go @@ -0,0 +1,25 @@ +package main + +import ( + "strings" + "testing" +) + +func Test_OSCLI_HELP(t *testing.T) { + + def := "BASIC.ROM" + roms := []*string{&def} + + env := newEnvironment(roms, false, false, false, false, false) + con := newConsoleMock(env, []string{ + "*HELP", + }) + env.con = con + + RunMOS(env) + + if !strings.Contains(con.output, "BBZ") { + t.Log(con.output) + t.Error("*HELP is not returning BBZ") + } +} diff --git a/vdu.go b/vdu.go index 26a69fa..4b8f10d 100644 --- a/vdu.go +++ b/vdu.go @@ -3,7 +3,7 @@ package main import "fmt" type vdu struct { - con console + env *environment queue []uint8 mode uint8 @@ -25,7 +25,7 @@ type vdu struct { var argsNeeded [256]int -func newVdu(con console) *vdu { +func newVdu(env *environment) *vdu { // Init args needed array, 0 for all except: argsNeeded[1] = 1 argsNeeded[17] = 1 @@ -41,7 +41,7 @@ func newVdu(con console) *vdu { argsNeeded[31] = 2 var v vdu - v.con = con + v.env = env // Mode 7 on startup v.mode = 7 v.m7fgColour = 7 // white @@ -479,7 +479,7 @@ func (v *vdu) writeInternal(cmd uint8, q []uint8) { } if out != "" && !v.ignore { - v.con.write(out) + v.env.con.write(out) } } @@ -504,7 +504,7 @@ func (v *vdu) mode7ResetCode() string { } func (v *vdu) mode7Reset() { - fmt.Print(v.mode7ResetCode()) + v.env.con.write(v.mode7ResetCode()) } func adjustAscii(ch uint8) string {