commanderx16 / x16-emulator

Emulator for the Commander X16 8-bit computer
384 stars 60 forks source link

Echo works erratically when turning it off and then back on #174

Closed MCPEmaniac closed 5 years ago

MCPEmaniac commented 5 years ago

The -echo option works erratically when turning it off and back on using address $9FB3. Sometimes it doesn't echo anything, and at other times it echos everything on the same terminal line instead of separate terminal lines.

The following program demonstrates the problem when the emulator is started with the "-echo" option. Even though there are two list statements with echo turned on, when the program is run the program is only listed a single time. When the following program is started with the "-echo raw" option all lines of the program are output to the same terminal line.

10 X=PEEK($9FB3):PRINT X 20 PRINT "A":LIST 30 POKE $9FB3,0 40 POKE $9FB3,X 50 PRINT "B":LIST

My sprite editor program turns echo off at the start and turns it back on temporarily only when exporting sprite data. I took this approach so the terminal wouldn't have a lot of irrelevant garbage data mixed with the sprite data. This approach worked with R32 but now seems to be broken with R33. With R33 all lines of my output are echoed on the same terminal line whereas with R32 each line of my output was output to a separate terminal line. I include a "\n" between each line of my output.

I noticed after "manually" poking $9FB3 with the value 2 (which I believe is associated with the "-echo" option) or the value 3 (which I believe is associated with the "-echo iso" option), "manually" peeking $9FB3 reads back as the value 1 (which I believe is associated with the "-echo raw" option). By "manually" I mean by typing in the POKE and PEEK commands versus running them in a program.

mobluse commented 5 years ago

I tried this program in both x16emu and x64 (C64 emulator part of VICE) and it works in the same way on the screen:

10 LIST
15 PRINT "HELLO"
20 LIST
30 LIST

It only LISTs once on the screen and then terminates. I.e. HELLO is not printed. This seems to be how LIST in BASIC V2 is supposed to work.

When you use -echo raw the emulator echoes exactly what the ROM prints, and on C64 and x16emu a single CR (i.e. \x0D) is used as newline. It was the same on Apple II and macOS 9 and earlier. If you want newlines translated to LF (\x0A) you should use -echo or -echo iso.

You could try x16emu -echo raw | xxd in Linux to see exactly what is printed. Now you see it sometimes prints '\x0A' but I think this is a bug (i.e. I don't believe this was in the original C64 ROM). I believe someone tried to adapt the ROM to Unix/Linux or Windows terminal. I reported this as an issue in the ROM https://github.com/commanderx16/x16-rom/issues/37. [Edit: Actually C64 basic ROM has '\x0A' that is printed, but it has no effect on screen, so it is strange that Commodore included it.]

There could still be some issue, as you say, when changing echo mode during runtime. Maybe it can only switch echo on or off during runtime, but not change mode. One has to study the emulator code.

mobluse commented 5 years ago

I think I've found one problem now: https://github.com/commanderx16/x16-emulator/blob/master/memory.c#L206 The value is made into a boolean v, so the echo mode can only become raw or none when you set it using POKE $9FB3,X. ?PEEK($9FB3) seems to print the correct value set by -echo.

This could be fixed by using value instead of v in the switch. One would also need to change https://github.com/commanderx16/x16-emulator/blob/master/glue.h#L53 to extern.