EWM Update: Illegal Instructions

Today I found out that ProDOS 2.4.1 crashes in my Apple ][+ emulator. Very early on when booting a disk, even before it displays the welcome screen. This is odd because ProDOS 2.4.1 is supposed to be compatible with any Apple II model that has 64KB of RAM.

EWM Boot Loader

After some sleuthing I figured out what is happening here. The big hint is what the emulator prints:

CPU: Exited because of unimplemented instruction 0x1a at 0x2095

This is a good hint. The Apple ][+ uses a 6502, and for that CPU there is no instruction with opcode 0x1a. It does exist on the 65C02, which later models of the Apple II family use, where it is the INC, Increment Accumulator, instruction.

ProDOS 2.4.1 is supposed to run on all models, including the 6502, so why is this happening? Are we trying to execute some random invalid code in memory or is there something else happening?

Looking at the code around 0x2095 we can see this:

...
2093: A9 00    LDA #$00
2095: 1A       ???       ; Invalid on 6502, INC on 65C02
2096: D0 11    BNE $20A9
...

It is now pretty clear what this does. This is code to detect the CPU type.

On a 6502 the $1A instruction is invalid, but it does does not halt or interrupt the the CPU. On the 65C02 it increments the accumulator. The above CPU detection code takes advantage of that: on a 65C02, the accumulator wil increment to 1 and the branch to $20A9 will happen. On the 6502 the accumulator stays zero and the branch will not happen.

The problem is that my emulator does not handle illegal instructions correctly. It incorrectly triggers an interrupt, which puts the program counter at 0x03FE, which is what we see in the screenshot too.

(Ending up at 0x3FE is actually not correct since the interrupt handler is at 0x03FB. So maybe that is another bug.)

I fixed this with a patch that does two things:

  • I brought back the --strict option for the emulator. If --strict is enabled, then the emulator will immediate abort if it encounters an invalid instruction. This is not correct behaviour, because the 6502 does not actually do this in real hardware, but it can be used o verify that programs are correct.

  • I aliased all unimplemented 6502 instructions to nop. So they are simply ignored. This is also not entirely correct because some of those instructions have undocumented behaviour that some programs may take advantage of.

For now this is a decent fix. ProDOS 2.4.1 boots again. Maybe some day I will try to implement the undocumented behaviour of the 6502.

If you are interested in playing around with my emulator, you can find the project at github.com/st3fan/ewm.

EWM Update: Status Bar

I’ve been making some more progress on my Apple 1 / Apple ][+ emulator. One thing I just finished is a status bar at the bottom of the screen that shows up when you hit Command-I. It shows the emulated CPU speed and disk activity.

Status Bar

The status bar is using the same character set as the Apple ][+.

If you are interested in playing around with my emulator, you can find the project at github.com/st3fan/ewm.

EWM Update: Joystick Support

Finished initial Joystick support for my emulator. Works pretty well with the XBox 360 Wireless controller. This is where using SDL is paying off. It has nice abstractions for finding game controllers and joysticks.

Choplifter is very playable. Also tried Hard Hat Mack and Ms Pac-man. Strangely Cider Spider does not work at all. Not sure what is going on there yet.

I’m thinking I should implement some kind of keyboard to D-pad mapping. So that you can play keyboard games with a controller.

My work is slowing down, because I keep playing games. Not sure if good or bad. Lots of fun though.

If you are interested in playing around with my emulator, you can find the project at github.com/st3fan/ewm.

EWM Update: Replica 1

I have been posting a lot of pictures and movies of the progress that I’ve been making with my Apple ][+ emulator. That is going pretty well, but the project actually started as an Apple 1 emulator. And I have to admin that I have been neglecting that part.

So here is some progress on the Apple 1 side. I just finished a native terminal, using the same character generator as the Apple ][+. I think they are pretty similar.

(I actually emulate two machine profiles: ‘Apple 1, 6502, 8K RAM, 256B ROM’ and ‘Replica 1, 65C02, 48K RAM, 8KB KRUSADER ROM’. The attached recording is the latter in action.)

I hope to package this up in an actual macOS application in the coming days and put a 0.1 online for people to try out. It ain’t real until you ship it!

A Windows build is also on my list, but I have zero clue about Windows. Hopefully a C compiler is freely available these days?

If you are interested in playing around with my emulator, you can find the project at github.com/st3fan/ewm.

EWM Update: Hi-Res Color

Making some progress with color. I don’t think the colors are completely right. And other emulators show more solid colors instead of the stripey fills. So I have to figure out what is going on there. Getting there!

And how about this one:

EWM Running on Linux

If you are interested in playing around with my emulator, you can find the project at github.com/st3fan/ewm.

EWM Update: FPS = Frogs Per Second

So here is something funny. I added support for (monochrome) hires graphics to my emulator last week. It worked, but was too slow to play for example Frogger.

I thought my ‘renderer’ was too simple. I’m simply putting pixels on the screen with FillRect(), which SDL maps to OpenGL primitives. So that is a lot of calls. I started writing a little rendering benchmark so that I could establish a baseline performance and then improve on that.

Well, to my surprise things were actually pretty crazy fast. Just 5 milliseconds for a full HGR page refresh.

Turns out that I was doing way too many screen updates and too few instruction executions. The real solution for this is to properly schedule both but for now I simply balanced them a bit more in a way that runs well on my specific development machine.

Video attached of Frogger moving really well. Probably a bunch too fast even.

If you are interested in playing around with my emulator, you can find the project at github.com/st3fan/ewm.

EWM Update: Color & Monochrome

The emulator now has the option to switch between color and monochrome support with the --color command line option.

EWM Running on Linux

Only green is supported for monochrome, but I plan to support all the common colors like white, amber and scanline emulation..

EWM Running on Linux

Yes, the colors are not right, and there is outstanding work to make colors look more solid and follow NTSC display rules.

It is still weird for me to see games in colour. I grew up in The Netherlands, where PAL is used for colour encoding. The Apple ][+ only supported the North America NTSC standard, so we could only use the computer with a green monochrome display.

If you are interested in playing around with my emulator, you can find the project at github.com/st3fan/ewm.

EWM Update: Frogger & Karateka

Things are moving fast now. Monochrome Hi-Res support.

Frogger

The horizontal line in the Frogger screen is probably a typo in the table that I have to list the base addresses of lines of the HGR pages.

Karateka

This is how I remember the Apple ][+ - everything in monochrome. Except that we had a Monitor ///, which was green. So I’ll have to make some options for monochrome color selection I think.

(Isn’t it awesome that the Apple I, Apple II and Apple III history is so well documented? Wikipedia has fantastic pages on almost every single piece of hardware from that era.)

If you are interested in playing around with my emulator, you can find the project at github.com/st3fan/ewm.

EWM Update: ProDOS Boots!

I found a quiet spot at the airport (SFO) and resolved my Language Card issues. ProDOS 1.0.1 boots now!

ProDOS Boots!

The best explanation of the Language Card and memory architecture was actually in Understanding the Apple II by James Sather. What a fantastic reference that is. I should really try to find a hard copy.

If you are interested in playing around with my emulator, you can find the project at github.com/st3fan/ewm.

EWM Update: Language Card

The beginnings of Apple Language Card support are there for a glorious total of 64KB of (bankswitched) RAM.

This means that DOS 3.3 can now load Integer BASIC into the card and you can run Integer BASIC programs from disk.

DOS 3.3 Booting

ProDOS 1.0.1 however complains about ‘Unable to execute basic system’. That will require some debugging on the travel weekend with way too many hours at airports.

ProDOS 1.0.1 Booting

If you are interested in playing around with my emulator, you can find the project at github.com/st3fan/ewm.