ZX81 emulator

for bbc micro/electron hardware, peripherals & programming issues (NOT emulators!)
RobC
Posts: 1818
Joined: Sat Sep 01, 2007 9:41 pm

ZX81 emulator

Postby RobC » Mon Nov 20, 2017 10:21 pm

Didn't quite manage to get this working at ABUG but it's working now: a Pi co-pro ZX81 emulator for the Beeb.

The ZX81 was the first computer I ever encountered - my uncle had built one from a kit and showed it to me when I was seven. I was immediately hooked and my parents bought me one of my own a few months later. I wrote a few very simple BASIC games for it and later ported them to the Beeb at school.

I used it pretty much every day before getting a Beeb for Christmas in 1983 and then didn't use it again :oops:. I've never even used one under emulation so today is the first time I've seen the 'K' prompt since I was 9!

I still have to add support to load and save programs so that's next on the list...

IMG_20171120_220753.jpg

IMG_20171120_220801.jpg

User avatar
hoglet
Posts: 6605
Joined: Sat Oct 13, 2012 6:21 pm
Location: Bristol

Re: ZX81 emulator

Postby hoglet » Mon Nov 20, 2017 10:23 pm

Fantastic!

Which Co Pro does it run on?

RobC
Posts: 1818
Joined: Sat Sep 01, 2007 9:41 pm

Re: ZX81 emulator

Postby RobC » Mon Nov 20, 2017 10:26 pm

It's running on the native Pi core - after figuring out how to build C applications for it with the PDP11 emulator, it was pretty simple.

User avatar
tricky
Posts: 1916
Joined: Tue Jun 21, 2011 8:25 am
Contact:

Re: ZX81 emulator

Postby tricky » Mon Nov 20, 2017 10:31 pm

Well done Rob.

User avatar
1024MAK
Posts: 6786
Joined: Mon Apr 18, 2011 4:46 pm
Location: Looking forward to summer in Somerset, UK...

Re: ZX81 emulator

Postby 1024MAK » Tue Nov 21, 2017 12:19 am

=D> =D> =D>

Mark
For a "Complete BBC Games Archive" visit www.bbcmicro.co.uk NOW!
BeebWiki‬ - for answers to many questions...

crj
Posts: 315
Joined: Thu May 02, 2013 4:58 pm

Re: ZX81 emulator

Postby crj » Tue Nov 21, 2017 2:48 am

I thought the ZX81 amd ZX80 cheated by having next to no video circuitry and expecting the CPU to produce the output either via NMI or by turning the screen off while processing and enabling it only while waiting for input?

I'm fascinated to know how you manage to get such a display reproduced via the Tube to a BBC Micro with any degree of speed/accuracy.

ThomasHarte
Posts: 363
Joined: Sat Dec 23, 2000 5:56 pm

Re: ZX81 emulator

Postby ThomasHarte » Tue Nov 21, 2017 3:22 am

crj wrote:I thought the ZX81 amd ZX80 cheated by having next to no video circuitry and expecting the CPU to produce the output either via NMI or by turning the screen off while processing and enabling it only while waiting for input?

I'm fascinated to know how you manage to get such a display reproduced via the Tube to a BBC Micro with any degree of speed/accuracy.

It's one of the machines included in my current emulator so:

The opcode fetch on a Z80 is a two-clock-cycle memory read followed by a two-clock-cycle refresh cycle.

If the CPU reads an opcode from any address with the top bit set, the HALT line is not currently active and the value does not have bit six set, then the value is diverted to a latch and a NOP is supplied to the CPU. The latched value is the character code. Parts of the next refresh address are then subverted to fetch a byte of pixels based on the latched value and the value of an internal three-bit counter.

The Z80 increments the refresh address for every refresh cycle, and if the instruction is of the shortest available length, the Z80 also bases an interrupt decision based on sampling the interrupt line during the refresh cycle. So the hardware reuses bit 4 of the intended refresh address as the interrupt line.

The bit six test ensures a HALT operation gets through, which can be used to terminate a line and use no additional memory, which is helpful if you have only the base 1kb. Use of that bit explains why those machines have non-ASCII 64-character sets.

So characters are output by loading a refresh address that points to the character graphic table and will trigger an interrupt in 32 counts, jumping the PC to the characters, then upon interrupt setting up for the next line.

Horizontal sync is automatic on both the ZX80 and ZX81. Vertical sync is programmatic. The ZX81 adds a line counter that can generate an NMI each line so that you can count them without having to dedicate any conscious thought to updating the display. That's the basis of SLOW mode.

Some games back in the day partially escape character mode by creatively using other positions in ROM to get close to pixel addressability. Due to a quirk of the bus, some extended RAM packs can also be used as a simple linear frame buffer (and others can be trivially adapted for such use).

So I guess for complete accuracy you'd need to pipe the actual video byte stream back, and hope that the program in use isn't being clever with syncs. It's perfectly possible to generate an interlaced signal if you fancy it, or a frame of any other number of lines (arbitrarily non-integral if you desire) in height.

But you could do a pretty good job and run most programs just by capturing the normal text buffer location, as most games just let things run as BASIC does. The machine wasn't on the market for all that long, and the development community was nascent.

If you're really careful about it, you can even do a flicker-free display on a ZX80, by writing code that always runs for the same number of display-window-available cycles regardless of branching, and that had been figured out in the wild by 1983. So a lot of people were on the ball. I just think they quickly realised how much more money you can make being intelligent on a Spectrum, Acorn or Commodore.

(EDIT: sneaking it in because I forgot last night and don't want to dominate the conversation: the ZX81 also applies WAIT while applying NMI to ensure that the NMI routine begins with exactly the expected phase relative to the horizontal counter, so that you can segue back into using the CPU for video fetching at a deterministic moment relative to the raster, regardless of the length of the instruction you were running when NMI was first asserted, in case anybody was wondering about that dangling thread)
Last edited by ThomasHarte on Tue Nov 21, 2017 4:46 pm, edited 1 time in total.

RobC
Posts: 1818
Joined: Sat Sep 01, 2007 9:41 pm

Re: ZX81 emulator

Postby RobC » Tue Nov 21, 2017 7:31 am

crj wrote:I thought the ZX81 amd ZX80 cheated by having next to no video circuitry and expecting the CPU to produce the output either via NMI or by turning the screen off while processing and enabling it only while waiting for input?

I'm fascinated to know how you manage to get such a display reproduced via the Tube to a BBC Micro with any degree of speed/accuracy.

I'm not doing anything clever - as Thomas suggests, it's just capturing the text buffer and sending any modified rows across the TUBE.

It's quick enough to run BASIC programs smoothly but I haven't tested it on any games yet.

User avatar
tricky
Posts: 1916
Joined: Tue Jun 21, 2011 8:25 am
Contact:

Re: ZX81 emulator

Postby tricky » Tue Nov 21, 2017 7:47 am

Thanks Thomas for a nice little read.

User avatar
fordp
Posts: 919
Joined: Sun Feb 12, 2012 9:08 pm
Location: Kent, England

Re: ZX81 emulator

Postby fordp » Tue Nov 21, 2017 8:01 am

Is the information how to build native apps fo the Pi Copro on the Wiki (https://github.com/hoglet67/PiTubeDirect/wiki). Sounds like fun. Great little project.
FordP (Simon Ellwood)
Time is an illusion. Lunchtime, doubly so!

RobC
Posts: 1818
Joined: Sat Sep 01, 2007 9:41 pm

Re: ZX81 emulator

Postby RobC » Tue Nov 21, 2017 9:10 am

fordp wrote:Is the information how to build native apps fo the Pi Copro on the Wiki (https://github.com/hoglet67/PiTubeDirect/wiki). Sounds like fun. Great little project.

Not sure but I used this as a guide after Dave/hoglet pointed me at it: http://www.valvers.com/open-software/raspberry-pi/step01-bare-metal-programming-in-cpt1/

I also used some of the documentation for the ARMTDMI co-pro:
http://www.sprow.co.uk/bbc/hardware/armcopro/writinginc.pdf

If there isn't one, it would be good if someone could write a definitive guide as I haven't got everything sorted out yet!

User avatar
BigEd
Posts: 1484
Joined: Sun Jan 24, 2010 10:24 am
Location: West
Contact:

Re: ZX81 emulator

Postby BigEd » Tue Nov 21, 2017 9:57 am

It's a great idea, Rob, to do this by writing a C application for the Native ARM - I think the page we need is one about doing that. I'd've thought it's not quite a bare-metal ARM, in that you're writing to the SWIs provided in PiTubeDirect, which is then doing the work of talking to the host over the Tube. Or have I got the wrong end of the stick?

That said, you must have done a bit more, to run your own byte stream protocol over the Tube back to a custom VDU driver that your application uploads to the host? That's almost a second page - it's what Elite does, and it's what the fast Life program does, and it's a good way to get screen updates done efficiently.

What you didn't need to do, if I understand correctly, is rebuild the PiTubeDirect software and reboot the Pi - you're running a well-behaved application on an existing copro model. The great advantage of using the native ARM model, other than having C to program in, is that you get native performance without any extra layer of emulation.

dominicbeesley
Posts: 464
Joined: Tue Apr 30, 2013 11:16 am

Re: ZX81 emulator

Postby dominicbeesley » Tue Nov 21, 2017 10:20 am

Nice one rob. Another triumph!

I'm not clear where the pdp11 comes in?

RobC
Posts: 1818
Joined: Sat Sep 01, 2007 9:41 pm

Re: ZX81 emulator

Postby RobC » Tue Nov 21, 2017 10:52 am

BigEd wrote:It's a great idea, Rob, to do this by writing a C application for the Native ARM - I think the page we need is one about doing that. I'd've thought it's not quite a bare-metal ARM, in that you're writing to the SWIs provided in PiTubeDirect, which is then doing the work of talking to the host over the Tube. Or have I got the wrong end of the stick?

That's right - it's an application written in a combination of C and assembler calling the SWIs provided by the native ARM copro in PiTubeDirect.

BigEd wrote:That said, you must have done a bit more, to run your own byte stream protocol over the Tube back to a custom VDU driver that your application uploads to the host? That's almost a second page - it's what Elite does, and it's what the fast Life program does, and it's a good way to get screen updates done efficiently.

At the moment, it's simpler than that. I'm updating the Beeb's font to look like the ZX81 and then writing any characters that need updating from one frame to the next with OS_WriteC (so a simple VDU stream). I may look at doing something cleverer if needed but effectively all it is at the moment is a series of VDU 31,x,y followed by the bytes that have changed.

BigEd wrote:What you didn't need to do, if I understand correctly, is rebuild the PiTubeDirect software and reboot the Pi - you're running a well-behaved application on an existing copro model. The great advantage of using the native ARM model, other than having C to program in, is that you get native performance without any extra layer of emulation.

Yes - being able to program in C and run at speed made it very easy to write.

dominicbeesley wrote:Nice one rob. Another triumph!I'm not clear where the pdp11 comes in?

Cheers. Previously, I built a C pdp11 emulator application for the native Pi core to run Unix and then used what I'd learnt about compiling C on this project.

dominicbeesley
Posts: 464
Joined: Tue Apr 30, 2013 11:16 am

Re: ZX81 emulator

Postby dominicbeesley » Tue Nov 21, 2017 11:03 am

Aha, got it - I thought you might have actually got the gcc cross compiler tools working on there!

D

User avatar
BigEd
Posts: 1484
Joined: Sun Jan 24, 2010 10:24 am
Location: West
Contact:

Re: ZX81 emulator

Postby BigEd » Tue Nov 21, 2017 12:03 pm

RobC wrote:
BigEd wrote:That said, you must have done a bit more, to run your own byte stream protocol over the Tube back to a custom VDU driver that your application uploads to the host? That's almost a second page - it's what Elite does, and it's what the fast Life program does, and it's a good way to get screen updates done efficiently.

At the moment, it's simpler than that. I'm updating the Beeb's font to look like the ZX81 and then writing any characters that need updating from one frame to the next with OS_WriteC (so a simple VDU stream). I may look at doing something cleverer if needed but effectively all it is at the moment is a series of VDU 31,x,y followed by the bytes that have changed.

Ah, yes, that's neat. And now I think of it, I think you already explained that to me at ABUG!

User avatar
Lardo Boffin
Posts: 670
Joined: Thu Aug 06, 2015 6:47 am

Re: ZX81 emulator

Postby Lardo Boffin » Tue Nov 21, 2017 10:51 pm

Next step a Spectrum emulator so I can play Underwurlde on my Beeb! You could put a bunch of support code on a ROM and call it a SpectROM! #-o
BBC model B 32k issue 4, 16k sideways RAM, Watford 12 ROM board, Retroclinic Datacentre + HDD, matchbox co-proc, Viglen twin 40/80 5.25" discs, acorn cassette
BBC model B 32k issue 7, turboMMC, Opus Challenger 3 512k, Pi 3 coproc, Acorn 6502 coproc

crj
Posts: 315
Joined: Thu May 02, 2013 4:58 pm

Re: ZX81 emulator

Postby crj » Tue Nov 21, 2017 11:16 pm

Trouble is, the Beeb can't actually do the Spectrum's display!

While the Beeb's graphics are superior in various respects, to emulate the Spectrum's attribute-based display system would require a 16-colour mode that was 32 characters by 24. And eight of those colours would have to be ones the Beeb lacked.

RobC
Posts: 1818
Joined: Sat Sep 01, 2007 9:41 pm

Re: ZX81 emulator

Postby RobC » Wed Nov 22, 2017 7:26 am

crj wrote:Trouble is, the Beeb can't actually do the Spectrum's display!

One of my thoughts for VideoNuLA was to implement something that could mimic the Spectrum's display using alternate bytes for colour attributes and pixel data. The attributes would have affected each 8 x 1 pixel block as with the Thomson machines.

User avatar
tricky
Posts: 1916
Joined: Tue Jun 21, 2011 8:25 am
Contact:

Re: ZX81 emulator

Postby tricky » Wed Nov 22, 2017 7:49 am

Is there a 16 palette 1bpp 4pixel per byte mode?
Or room for one?

User avatar
1024MAK
Posts: 6786
Joined: Mon Apr 18, 2011 4:46 pm
Location: Looking forward to summer in Somerset, UK...

Re: ZX81 emulator

Postby 1024MAK » Wed Nov 22, 2017 8:11 am

All the gory details of how the ZX81 (and ZX80) display system works are on these web sites:

ZXdocs (Martin Korth)
Wilf Rigter's ZX Video Tutorial
(see notes below)
Wilf Rigter's ZX Video Tutorial (mirror)
(see notes below)

Wilf Rigter's web pages will display incorrect characters and symbols. The documents have been created with Microsoft Word Codepage 1252 while the web server delivers the documents as UTF8. If you choose to manually select code page west european (Latin 1), you will get the correct display. We suspect this is due to some changes in the hosting / web server profile settings (codepage) and probably the browser expects UTF code for some reason. Normally it should be detected automatically but is detected wrong since more than 2 years now.
Also some of the links, for example, to images, do not work. The link to them is corrupted, but can be corrected manualy:
- replace '/' by '\' (or vice versa) within their URL
- adapt the URL (lower/uppercase letters) according to the URL of the parent page.

Wilf's pages are also at archive.org. If you choose a page here 2 or 3 years back, it will be shown with correct code page as well.

Thanks to the Sinclair ZX World forum members PokeMon and Siggi for the details of the issues with Wilf's web site, and thanks to Martin (mrtinb) for setting up the mirror.

Mark
For a "Complete BBC Games Archive" visit www.bbcmicro.co.uk NOW!
BeebWiki‬ - for answers to many questions...

RobC
Posts: 1818
Joined: Sat Sep 01, 2007 9:41 pm

Re: ZX81 emulator

Postby RobC » Wed Nov 22, 2017 9:12 am

tricky wrote:Is there a 16 palette 1bpp 4pixel per byte mode?
Or room for one?

It's not there but was one of the things I considered adding before the current attribute modes were put in.

Unfortunately, I don't think there's enough room to add it in now but, as the CPLD can be reprogrammed, I could do a custom build and replace some of the existing functionality if someone wanted one with it in.

As an aside, a Beeb with VideoNuLA can mimic the Amstrad CPC's graphics as its palette is just a subset of what VideoNuLA can do and it also uses the 6845.

User avatar
Lardo Boffin
Posts: 670
Joined: Thu Aug 06, 2015 6:47 am

Re: ZX81 emulator

Postby Lardo Boffin » Wed Nov 22, 2017 9:28 am

If it could be done with the NuLA then it would certainly open up a large game catalogue for the Beeb. :D

To be honest I mainly suggested it as an excuse to use the rather poor SpectROM pun but if it was actually possible that would be something!
BBC model B 32k issue 4, 16k sideways RAM, Watford 12 ROM board, Retroclinic Datacentre + HDD, matchbox co-proc, Viglen twin 40/80 5.25" discs, acorn cassette
BBC model B 32k issue 7, turboMMC, Opus Challenger 3 512k, Pi 3 coproc, Acorn 6502 coproc

RobC
Posts: 1818
Joined: Sat Sep 01, 2007 9:41 pm

Re: ZX81 emulator

Postby RobC » Wed Nov 22, 2017 7:13 pm

Lardo Boffin wrote:To be honest I mainly suggested it as an excuse to use the rather poor SpectROM pun but if it was actually possible that would be something!

As I mentioned at the ABUG weekend, there was a hardware Spectrum emulator for the Tatung Einstein called the "Speculator"! I think SpectROM is a better name than that :D

ThomasHarte
Posts: 363
Joined: Sat Dec 23, 2000 5:56 pm

Re: ZX81 emulator

Postby ThomasHarte » Wed Nov 22, 2017 7:21 pm

RobC wrote:
Lardo Boffin wrote:To be honest I mainly suggested it as an excuse to use the rather poor SpectROM pun but if it was actually possible that would be something!

As I mentioned at the ABUG weekend, there was a hardware Spectrum emulator for the Tatung Einstein called the "Speculator"! I think SpectROM is a better name than that :D

I read about that, and think it was available for a few other TMS machines too. The details were frustratingly vague, other than that "a bus quirk" is used to give the emulator box sufficient bandwidth to transfer one third of the display every frame, and then which parts it shoots across are configured per game so that areas with action are sent more often. Given that relatively little updates at 50Hz, the authors argue that to be sufficient. Just don't try to play Cobra on it.

User avatar
1024MAK
Posts: 6786
Joined: Mon Apr 18, 2011 4:46 pm
Location: Looking forward to summer in Somerset, UK...

Re: ZX81 emulator

Postby 1024MAK » Wed Nov 22, 2017 8:56 pm

RobC wrote:
Lardo Boffin wrote:To be honest I mainly suggested it as an excuse to use the rather poor SpectROM pun but if it was actually possible that would be something!

As I mentioned at the ABUG weekend, there was a hardware Spectrum emulator for the Tatung Einstein called the "Speculator"! I think SpectROM is a better name than that :D

There was also a Speculator (part hardware and part software) for the Memotech MTX computers that enabled a MTX to load and run various ZX Spectrum games.

There are some details and some pictures here and here :wink:

Mark
For a "Complete BBC Games Archive" visit www.bbcmicro.co.uk NOW!
BeebWiki‬ - for answers to many questions...

RobC
Posts: 1818
Joined: Sat Sep 01, 2007 9:41 pm

Re: ZX81 emulator

Postby RobC » Wed Nov 22, 2017 9:42 pm

That's awesome - I knew that there were Speculators for machines other than the Einstein but had never seen details before.

Many thanks.

User avatar
fordp
Posts: 919
Joined: Sun Feb 12, 2012 9:08 pm
Location: Kent, England

Re: ZX81 emulator

Postby fordp » Thu Nov 23, 2017 1:53 pm

This has inspired me to try and compile BrandyBasic for the Native Pi Copro. Do you have any plans to make your ZX81 emulator available as that would be a great starting point to prove any toolchain. I will start a Github project for compiling on the PiCopro given the chance :D
FordP (Simon Ellwood)
Time is an illusion. Lunchtime, doubly so!

RobC
Posts: 1818
Joined: Sat Sep 01, 2007 9:41 pm

Re: ZX81 emulator

Postby RobC » Thu Nov 23, 2017 2:51 pm

fordp wrote:Do you have any plans to make your ZX81 emulator available as that would be a great starting point to prove any toolchain

Yes - once I've got it in a good state, I'll put it up here. If you want to PM me in the meantime, I'll send you things as they stand now.

I have now added loading and rough instruction timings. It's enough to get 3D Monster Maze working :D :

IMG_20171123_144540.jpg

IMG_20171123_144521.jpg



I know it's a cliché but this game scared my sister and me witless back in the day. Playing it now on the Beeb, it doesn't seem so bad! And there's no RAM pack wobble as you frantically hit the keyboard to escape from Rex!

User avatar
Lardo Boffin
Posts: 670
Joined: Thu Aug 06, 2015 6:47 am

Re: ZX81 emulator

Postby Lardo Boffin » Thu Nov 23, 2017 3:01 pm

Awesome game to start with!

Rex lies in wait.... (shivers...)
BBC model B 32k issue 4, 16k sideways RAM, Watford 12 ROM board, Retroclinic Datacentre + HDD, matchbox co-proc, Viglen twin 40/80 5.25" discs, acorn cassette
BBC model B 32k issue 7, turboMMC, Opus Challenger 3 512k, Pi 3 coproc, Acorn 6502 coproc


Return to “hardware”

Who is online

Users browsing this forum: Bing [Bot] and 16 guests