Hello ZX friends,

 

In previous articles I have referred to a ZX81 ROM enhancement to "auto-run"

a BASIC program from power up or reset. I have this rom patch installed on

both my ZX81 with NVM and the ZX97. Since I do most of my  "code and test"

programming on the ZX97, it is great to turn on the computer and instantly

have the assembler up and running with the source file in memory ready to

get to work. AUTOBASIC is essential for using a ZX81 as a controller (hothouse

HVAC, fire and burglar alarm, robot) which can auto-run a BASIC or machine code
 control programs to automatically recover from a reset caused by power fail,

watchdog timeout, etc. The ROM patch AUTOBASIC is compatible with a

nonvolatile 64K RAM-pack as described in previous articles and of course the

 ZX97. AUTOBASIC can also be used on the XTender emulator with NEWROM

installed. Although XTender won't auto-run on power up, it possible to develop

and test ROM enhancements and use RAND USR 0000 to simulate a reset.

I will write a little loader program for the emulator version if there is interest.

 So here then is the AUTOBASIC article which explains how it all works.

 

enjoy

 

wilf

 

                             ZX81 AUTOBASIC

                              wilf rigter -1997

 

You only have to have a keen interest in the ZX81 to read and enjoy these

articles and dream about the perfect ZX81!

 

Better yet if you have the technical resources to replace the ZX81 ROM with

an EPROM, or to build a 64K NVM or ZX97lite to try this software first hand.

 

AUTOBASIC is a ZX81 ROM enhancement and therefore needs some means of

altering the ZX81 ROM contents. One way is to replace the ROM with an

EPROM or secondly, use a RAM circuit which overlays the ROM  such as the

shadow RAM described in my 64K NVM article. AUTOBASIC also assumes that

the top 16K of the memory map is used as a RAMDISK with the auto-running

program formatted by that other ZX81 ROM enhancement: RAMDOS.

All of these features are present on the ZX97 or can be emulated in

XTender using the NEWROM program presented earlier.

Those hardy souls who roll their own ZX designs are welcome to pick and

choose some of the techniques presented here and use them in their own

designs (and give some credits of-course).

 

First of all I should say that you don't have to know the details of

AUTOBASIC to get the benefit of instant program execution on power-up.

Once installed the AUTOBASIC feature is completely transparent to the

normal user. Only a few directions are required to use AUTOBASIC:

 

1. The AUTOBASIC file must be saved using RAMDOS in the default bank (F) and

   the type letter should be changed to R to identify a properly formatted

   BASIC program. (If R is not found, the normal ZX81 0/0 screen appears)

2. On power-up or reset, the SHIFT key is tested and if held down, the RAMDOS

   directory is loaded instead of the auto running BASIC program.

3. The AUTOBASIC program loads the R file to system memory and executes RUN 1

   or if no R file is found, no program is loaded and the code executes the

   non-existing basic program ending with the familiar 0/0 error message.

 

For the technically inclined ZX81 user, the source listing may provide enough

information to understand how AUTOBASIC works (and modify it as desired:-)

but I have added an additional comments to highlight some of the finer points.

 

The old ZX81 ROM RESET code was designed for 1/2/16K system memories

and required a RAM test to set the correct value for RAMTOP.

However the new code is designed for expanded memory , with RAMTOP fixed

at 7FFF on reset and the space freed up by nuking the old RAMTEST code

is put to good use for auto running RAMDOS or AUTOBASIC. Like the old

ROM code, the new code sets up the system variables, the display file,

and the value of NEXTLIN but then the new code pushes 0676, the LINERUN

vector, on the STACK and continues with a three way branch to RAMDOS or

AUTOBASIC or the old ZX81 0/0 screen.

 

The new code tests if the carry flag is set which means the system reboot

was caused by NEW and a direct jump is made to RAMDOS. If the carry flag

is reset it means the system reboot was started from a RESET. If the SHIFT

key is held down, AUTOBASIC is skipped and like NEW, the system directly

executes RAMDOS to load a new program.

 

If no shift key is held down then the new code jumps to a third code

segment at A400 to test if the first file in BANK F has a file type R

indicating it should be loaded into system RAM and executed.

If it finds the R type AUTOBASIC file then the file length (up to 16K)

is loaded into BC and the required space is created between program start

407D and the start of the display file (400C). Finally the R file is

transferred to the newly created program space in system RAM and the

AUTOBASIC code returns via SLOW (02FB) to execute the BASIC program with

LINE RUN code at 0676. This vector was previously PUSHed on the stack and

the address of the first line saved to NEXTLIN. The BASIC interpreter

uses LINERUN to RUN the BASIC program lines. If there is no R file in the

first location in BANK F then no program is transferred and executing the

null BASIC program ends with the familiar ZX81 0/0 error message on screen.

 

Summarized comments and the source code follow:

 

The OLD ZX81 ROM is changed at 0000, 0025, and 03C3. The actual AUTOBASIC

code is added to the BASIC OS EXTENSION in EPROM at A400.

 

1. The old ZX81 ROM code segment at 0000 is slightly modified to load HL

instead of BC with RAMTOP=7FFF.  The carry flag is reset with SUB A to

differentiate between NEW and RESET later on. Note the tight space in

this memory area (for the RST routines) required a relative jump to three

spare bytes at 0025 to make the long jump to the next code segment at 03CA.

The code between 0007 and 0025 is unaltered.

 

      ;8 BYTES OF ROM CODE AT

      ;0000 ARE CHANGED JUST TO

      ;SETUP HL REGISTER AND CF

 

      ORG 0000

 

RESET OUT FD,A;TURN OFF NMI

      LD HL,7FFF;RAMTOP = 7FFF

      SUB A;RESET/NEW FLAG (CF)

      JR 1D;JP 03CA VIA RST1

 

      ;THREE FREE BYTES AT 0025

      ;ARE USED FOR A LONG JUMP

 

      ORG 0025

 

RST1  JP 03CA;NOW JUMP TO RST2

 

 

2. The old ZX81 RAMTEST code at 03C3 is replaced and it is assumed that

at least 16K of system memory is available. This greatly speeds up

initialization and gives an "instant" power-up screen.

The NEW and RESET commands use the same system variable initialization code

but NEW of course does not auto-run and instead directly starts RAMDOS to

select a NEW program. If the SHIFT key is held down during RESET it is

treated like a NEW command.

 

 

      ;A REBOOT FROM RESET OR

      ;POWER UP ENTERS AT RST2.

      ;SHIFT KEY DISABLES AUTORUN

 

      ORG 03C3

 

NEW   CALL 02E7;NEW ENTRY POINT

      LD HL,(4004);GET RAMTOP

      SCF;FLAG AS NEW,NOT RESET

RST2  LD (4004),HL;SAVE RAMTOP

      DEC HL;

      LD (HL),3E

      DEC HL

      LD SP,HL;NEW STACKPOINTER

      DEC HL

      DEC HL

      LD (4002),HL;ERR-SP

      LD A,1E;POINTER TO VIDEO

      LD I,A;FONT TABLE (MSB)

      IM1;SET INTERRUPT MODE 1

      LD IY,4000;SYSVAR POINTER

      LD HL,407D;

      LD (IY+59),H;INIT CDFLAG

      LD (400C),HL;DFILE=407D 

      LD (4029),HL;NXTLIN=407D

      LD B,19;COLLAPSED DISPLAY

NL    LD (HL),76;HAS 25 N/L CHR

      INC HL

      DJNZ NL

      LD (4010),HL;VARS=4096

      PUSH AF;NEW/RESET FLAG

      CALL 149A;CLEAR POINTERS

      CALL 0A2A;FULL DISPLAY

      POP AF;NEW/RESET FLAG

      LD HL,0676;LOAD LINERUN

      PUSH HL;AS RETURN ADDRESS

      JR C NEW1;JR IF NEW

      LD A,FE;TEST SHIFT KEY

      IN A,FE

      RRA

      CALL 0F4B;RESET DEBOUNCE

      JP C A400;JP TO AUTOBASIC

NEW1  JP A080;JUMP TO RAMDOS

 

 

3. If the SHIFT key is not held down a jump is made to A400. This code

sets up the 8255 bankswitch controller to select BANK 0F. However in

minimal systems with only one RAM/EPROM bank installed, the default

BANK is selected. The first file in BANK F is tested to see if it is a

R type file and the file is transferred to the system memory and auto

executed from line 1. If not found the sequence ends with error 0/0.

 

      ;AUTOBASIC SELECTS BANK F

      ;LOADS THE 1ST FILE TO

      ;SYSTEM RAM IF IT IS A

      ;TYPE R FILE. THEN SIMPLY

      ;RETURNS TO 0676 TO RUN

      ;THE R FILE. IF NO R FILE

      ;IS FOUND, ENDS WITH 0/0

 

      ORG A400

AUTO 

      LD A,99;SETUP BANKSWITCH

      OUT DF,A;TO SELECT DIR F

      LD A,0F

      OUT CF,A

      LD HL,C007;1ST FILE TYPE

      LD A,37;TEST FILE TYPE R

      CP (HL);IS IT AN R FILE?

      JR NZ SLOW ;IF NOT > 0/0

      INC HL;FILE LENGTH (LSB)

      LD C,(HL);C=LSB LENGTH

      INC HL;FILE LENGTH (MSB)

      LD B,(HL);B=MSB LENGTH

      INC HL;NOW POINT TO DATA

      PUSH HL;SAVE DATA POINTER

      PUSH BC;SAVE LENGTH

      LD HL,407D;BASIC START

      PUSH HL;SAVE START

      DEC HL;CORRECT START

      CALL F23;CALL FAST

      CALL 99E;MAKE BC SPACES

      POP DE;GET START

      POP BC;GET LENGTH

      POP HL;GET DATA

      LDIR;LOAD R FILE

      LD (IY+1),C0 ;RESET FLAG.

SLOW  JP 0F2B;RETURN VIA SLOW

 

 

This AUTOBASIC code has over the years saved me many hours of

waiting for the system to reboot. In a "code and test" session

especially when changing the system software, I use the RESET

button hundreds of times to recover from a "locked up" system.

To have the system reboot and the source code instantly ready

for editing gives a kind of dynamic fluency to the process that

has got me through some very frustrating programming sessions.

 

enjoy

 

wilf

 

 

 

 

     

www.000webhost.com