How to inject programs into zx81 memory?

Any discussions related to the creation of new hardware or software for the ZX80 or ZX81
Harbaum
Posts: 9
Joined: Mon Jun 23, 2014 10:42 pm

How to inject programs into zx81 memory?

Post by Harbaum »

Hi,

I am working on a FPGA zx81 core based on the zx01 code. Now i'd like to load programs. I do have memory on my board which I can write from an external controller into. So I simply tried to load a .p file directly to address $4009. This crashes the zx81.

I found that the Unix xz81 emulator does something similar although it hooks into the loasd routine and not just randomly loads a program at a random time.

Is this possible ast all? How do I loads a .p program into memory if I can write the zx81 main memory at will?

Regards,
Till
sirmorris
Posts: 2811
Joined: Thu May 08, 2008 5:45 pm

Re: How to inject programs into zx81 memory?

Post by sirmorris »

Hi Till, welcome aboard.

2 things, off the top of my head that I learned when writing the LOAD routines for ZXpand.

1. You need to be in fast mode otherwise you risk corrupting something which is needed to keep the tricksy video display in one piece.

2. Re-start the zeddy after loading memory by jumping back to the video display process at $207, the stack should be set up in a special way but I can't tell you what that is right now. A few minutes with an emulator will show it up though.

C
Harbaum
Posts: 9
Joined: Mon Jun 23, 2014 10:42 pm

Re: How to inject programs into zx81 memory?

Post by Harbaum »

Hi,

thanks for the quick reply. The zxpand actually was a thing where would have looked next as I hoped that it would have solved the same problems.

It's not trivial to control the CPU directly in the FPGA. But i'll see if I can force the device to fast mode by writing to memory only. And forcing a jump may also be possible. I'll see ...

Thanks again,
Till
poglad
Posts: 133
Joined: Mon Mar 24, 2014 3:11 pm
Location: Aberdeen, Scotland

Re: How to inject programs into zx81 memory?

Post by poglad »

Don't forget that a ZX81 tape image begins with the filename. The final character of the filename has bit 7 set. Any bytes after that in the tape image can then be loaded to memory.
User avatar
PokeMon
Posts: 2264
Joined: Sat Sep 17, 2011 6:48 pm

Re: How to inject programs into zx81 memory?

Post by PokeMon »

Yes - SirMorris is right.
You have to take control the program execution of ZX81 - best way stop, write into memory and then make initialisation at $0207.
This routine is directly called after a LOAD command.

See ROM disassembly, LOAD/SAVE and L0207:
http://www.wearmouth.demon.co.uk/zx81.htm

You can not write into memory when ZX81 is running, even not in FAST mode.
Normally the ZX81 does a LOAD command, which reads into memory and makes a new initialisation.
When Z80 CPU is doing something, how could this correspond when a third party is writing into memory ? :roll:
Please take note that from $4009 there are first the complete system variables overwritten which crashes your ZX81 when writing values and the rest of data the variables are pointing to is missing.

And yes, Paul is right about the filename in the .p file coded. You have to omit this.
You should refer to cassette file specification in the ZX specs for more information, see here:
http://problemkaputt.de/zxdocs.htm
Harbaum
Posts: 9
Joined: Mon Jun 23, 2014 10:42 pm

Re: How to inject programs into zx81 memory?

Post by Harbaum »

PokeMon wrote: And yes, Paul is right about the filename in the .p file coded. You have to omit this.
You should refer to cassette file specification in the ZX specs for more information, see here:
http://problemkaputt.de/zxdocs.htm
According to exactly that page the filename is _not_ coded into the .p nor the .z81 file. That page states that explicitely.

And it's not just my idea to do that. Both the xz81 and sz81 for unix do exactly that. But they do this in virtually no time (so the cpu doesn't advance) and the force the cpu and also some parts of the stack and other memory in a certain state afterwards.

This is a) pretty tricky to do in a fpga and b) would require changes to the cpu core and other internal parts which i'd like to avoid.

I think it may be easier to implement a real tape emulation. This will be slower and a little less comfortable to use. But i'd really prefer not to change the zx01 core itself in any way. I need to think about this a little more.

On a side note: I found autobasic (http://www.user.dccnet.com/wrigter/inde ... oBasic.htm) which does something similar. Stopping and resetting the cpu is rather easy in vhdl. Patching the os at runtime also. So this might be another way to solve this problem.

Hell, would i have known that it's this tricky to place code into these old machines i'd never have started with the zx81/zx01. Also the orignal authors of this fpga code all seem to have moved on ...
User avatar
1024MAK
Posts: 5101
Joined: Mon Sep 26, 2011 10:56 am
Location: Looking forward to summer in Somerset, UK...

Re: How to inject programs into zx81 memory?

Post by 1024MAK »

First off:- Welcome to our forum Till :D :D :D

Second: Stopping a Z80 CPU if you can control what would be real signals on a real Z80 CPU is easy:- assert /BUSRQ and wait until the Z80 acknowledges with /BUSAK. The Z80 is now paused and no longer drives the address, data or control buses. Other "hardware" can access and change memory. It's a bit harder to make the Z80 restart execution at a different address, but it is possible (once you release /BUSRQ the Z80 normally continues at the address in the program counter). You just have to disable the RAM for the time being and instead feed the Z80 the correct data for a jump instruction. Then enable the RAM and you are back to normal.

So if the FPGA version includes /BUSRQ and /BUSAK, no CPU core changes required.

Mark
ZX81 Variations
ZX81 Chip Pin-outs
ZX81 Video Transistor Buffer Amp

:!: Standby alert :!:
There are four lights!
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :!:
Looking forward to summer later in the year.
User avatar
PokeMon
Posts: 2264
Joined: Sat Sep 17, 2011 6:48 pm

Re: How to inject programs into zx81 memory?

Post by PokeMon »

Harbaum wrote:According to exactly that page the filename is _not_ coded into the .p nor the .z81 file. That page states that explicitely.
Yes - sorry you are right. This is used only in the LOAD routine to choose the "right" program to load.
Anyway most people use LOAD "" to load any program and are happy if any program is loaded at all. :mrgreen:

Yes - if you have control, /BUSRQ could be used. Or you stop the clock of CPU. This could cause problems in real hardware but not in your FPGA I think.
You have to do an init after loading. Why don't you modify the firmware, skip the memory check, load program in memory, set all other variables from $4000-$4008 (RAMTOP important) and then jump directly to $0207 ?

I think you could just change the address at byte 6 and 7 to $0207 when preparing memory.
Just change JP L03CB to L0207. That' all you need. 8-)

Code: Select all

;; START
L0000:  OUT     ($FD),A         ; Turn off the NMI generator if this ROM is 
                                ; running in ZX81 hardware. This does nothing 
                                ; if this ROM is running within an upgraded
                                ; ZX80.
        LD      BC,$7FFF        ; Set BC to the top of possible RAM.
                                ; The higher unpopulated addresses are used for
                                ; video generation.
        JP      L03CB           ; Jump forward to RAM-CHECK.
Harbaum
Posts: 9
Joined: Mon Jun 23, 2014 10:42 pm

Re: How to inject programs into zx81 memory?

Post by Harbaum »

1024MAK wrote:First off:- Welcome to our forum Till :D :D :D

So if the FPGA version includes /BUSRQ and /BUSAK, no CPU core changes required.

Mark
Hi! It's a pleasure to be here.

Yeah, stopping the CPU is possible. I can even just gate the CPU clock. Still i need to fake addresses and data and the like

I think i'll have a closer look at actually implementing a tape drive inside the FPGA. That sounds easier and less intrusive with respect to the zx01. And since it's real hardware and not just emulation it makes sense to have genuine load times and some distorted video. Although I think most VGA screens will simply go into power save mode during load ...
poglad
Posts: 133
Joined: Mon Mar 24, 2014 3:11 pm
Location: Aberdeen, Scotland

Re: How to inject programs into zx81 memory?

Post by poglad »

Harbaum wrote:According to exactly that page the filename is _not_ coded into the .p nor the .z81 file. That page states that explicitely.
It's not encoded as part of the .P format, because it doesn't need to be - it's already present in the data block saved by the ZX81. The .P format only specifies the additional bytes contained in the .P file header by the emulator.

Once you've parsed the .P file format, you'll be left with a block of data which matches exactly what the ZX81 originally output - which includes the filename. See attached pic.
Attachments
adventd.png
adventd.png (92.62 KiB) Viewed 4600 times
Post Reply