skx / cpmulator

Golang CP/M emulator for zork, Microsoft BASIC, Turbo Pascal, Wordstar, lighthouse-of-doom, etc
MIT License
96 stars 3 forks source link

MBASIC.com binary does not work #49

Closed skx closed 3 months ago

skx commented 3 months ago

This is something that would be awesome to have, so it should be resolved.

When the binary runs we immediately get an error:

{"time":"2024-04-24T21:25:45.43693778+03:00",
 "level":"ERROR",
 "msg":"Unimplemented SysCall",
 "syscall":66,"syscallHex":"0x42"}
Error running MBASIC.COM []: UNIMPLEMENTED

I do not believe that the syscall number there is correct, CP/M syscalls are implemented by loading hte operation-number in the C register and running call 0x0005. I suspect what is actually happening here is that control-flow has gone wrong, and we're getting a random branch there.

Now why might that be? Well I disassembled the binary and I see a bunch of rst xx instructions. Those are single-byte calls to specific addresses in the zero-page.

For example:

$ z80dasm B/MBASIC.COM| grep -i rst
    rst 10h 
    rst 8   
    rst 0   
    rst 0   
    rst 8   
    rst 8   

So I think the issue here is that we don't actually setup any traps, or handlers, for those low addresses - and we're actually executing the contents of the FCB, DMA area, etc, etc.

To resolve this we need to add JMP handlers to point to the entry-point - or otherwise fake things so that this works - via more breakpoints, for example.

skx commented 3 months ago

Terrible trace is terrible, but it is alive!

$ go build . && ./cpmulator ~/Repos/github.com/skx/z80-playground-cpm-fat/dist/CPM/DISKS/B/MBASIC.COM 
BASIC-80 Rev. 5.21
[CP/M Version]
Copyright 1977-1981 (C) by Microsoft
Created: 28-Jul-81
39986 Bytes free
Ok
10 print "OK"
run
IOK
POk
 for I = 0 to 10
FOR Without NEXT
Ok
20 for i = 0 to 10
30 print i
40 next i
list
z10 PRINT "OK"
z20 FOR I = 0 TO 10
z30 PRINT I
z40 NEXT I
Ok
un
Syntax error
Ok
run
IOK
P| 0 
| 1 
| 2 
| 3 
| 4 
| 5 
| 6 
| 7 
| 8 
| 9 
| 10 
Ok
skx commented 3 months ago

So there are two issues really that needed to be solved for this to work, but its also tied up with console input:

However things are still a bit dodgy for two reasons:

skx commented 3 months ago

Still getting spurious output, despite my "emulation".

Will work on it some more tonight. Added test code to #50 as a prototype of how to cleanup the input/status-checking code.