Loading And Saving ROMs

By Derek Walker

Originally published in EUG #14

Loading

I decided to give myself a challenge and write a BASIC program to load ROM images into Sideways RAM. I know there are plenty of ROM utilities out there with this function on them (e.g. Pres' AP2, Slogger's Expansion 2.0 and the ADVANCED ROM MANAGER) but I was thinking some Electron users might not be aware that their Electrons have the capability for sideways RAM. If you are the proud owner of a Pres AP3 or AP4 then you can put a Static RAM chip in the spare ROM socket, giving you 16K of very useful memory. BBC B+ and Master 128 computers have similar routines built into their DFS ROMs.

My first attempt, program 1, works very well as long as you're using DFS (PAGE=&E00 or &1900) but with ADFS (PAGE=&1D00 or &1F00) I ran out of user memory. The easiest way to get more memory was to use some screen memory but this proved very untidy. At this point I decided to see how small I could make this program - the real challenge!!

Program 1

       10REM SIDEWAYS RAM LOADER v1.00                                    
       20REM D.WALKER                                                     
       30ONERROR GOTO10                                                   
       40MODE6
       50PRINTTAB(8,5)"SIDEWAYS RAM LOADER"
       60PRINTTAB(5,8)"Enter Sideways RAM Number ";:REPEAT:P.TAB(33,8)SP
   C3:INPUTTAB(33,8)ramnum:UNTILramnum>=0 AND ramnum<16
       70INPUTTAB(5,10)"Enter ROM image filename";NAME$
       80PRINTTAB(10,12)CHR$(136)CHR$(133);"Now LOADING"
       90OSCLI("LOAD "+NAME$+" 2000")
      100PROCass                                                          
      110CALLload                                                         
      120VDU7:PRINTTAB(10,12)CHR$134;"Complete"TAB(0,14)CHR$(134);"Now p
   ress <CTRL BREAK> to initialise"''
      130END
      140DEFPROCass                                                      
      150DIMD% 100                                                        
      160FOR I%=0 TO 2 STEP 2                                             
      170P%=D%                                                            
      180[OPTI%                                                           
      190.load                                                            
      200LDX&F4                                                          
      205LDA#&0C:STA&F4:STA&FE05                                           
      210LDA#ramnum:STA&F4:STA&FE05                                       
      220LDY#0:STY&70:STY&72                                               
      230LDA#&80:STA&71                                                    
      240LDA#&20:STA&73                                                   
      250.loop                                                             
      260LDA(&72),Y:STA(&70),Y                                            
      270INY:BNEloop                                                       
      280INC&71:INC&73                                                    
      290LDA&73:CMP#&60:BNEloop                                            
      300STX&F4:STX&FE05                                                   
      310RTS                                                              
      320]                                                                 
      330NEXT                                                              
      340ENDPROC                                                          

The program works by *LOADing the file into user RAM between &2000 and &6000, then transferring it up to the selected RAM bank at &8000. On the Electron with ADFS this leaves 512 bytes for the program to reside. Program 1's length worked out to be about 740 bytes along with 100 additional bytes required for the assembly of the machine code. It was quite easy to reduce the byte count by cutting the comment text to a minimum and removing the REM statements. I also changed the memory area the machine code the machine code assembled in to &A00, away from the main memory.

At this stage (with a program length slightly less than 512 bytes), I thought I would try it on a BBC B fitted with both DFS and ADFS. Crash! The BBC's PAGE is &1F00 with both filing systems installed and the ROM select is mapped into a different location, BBC &FF30 and the ELECTRON &FF05.

I decided to try to reduce the program to under 256 bytes and, if I could, then it should be possible to make it a one liner. To achieve this, I used some unorthodox programming techniques - I thought "Why assemble the code every time the program is run? I ran the program and, using Slogger's Click memory editor I saved the assembled machine code to disk - the area of memory was between &A00 and &A34. I then prepared the BASIC program to poke the m/code into the appropriate locations in RAM as shown below.

Program 2

      1V%=&A00:$V%="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
   X":MODE7:PRINT'"LOAD ROM":INPUT'"Filename "N$"RAM No.";:REPEAT:INPUTT
   AB(9,4)rn:UNTILrn>=0ANDrn<16:OSCLI"LOAD""+N$+"" 2000":?&A0A=rn:CALL&A
   00:PRINT"Done"

Code 1

      0A00 A6 F4 A9 0C 85 F4 8D 05                               
      0A08 FE A9 00 85 F4 8D 05 FE                              
      0A10 A0 00 84 70 84 72 A9 80                          
      0A18 85 91 A9 20 85 73 B1 72                              
      0A20 91 70 C8 D0 F9 E6 71 E6                              
      0A28 73 A5 73 C9 60 D0 EF 86                               
      0A30 F4 8E 05 FE 60                                         

Next I loaded the BASIC program as normal then *LOADed the Code file on top of the Xs near the beginning of the line.

With this technique the program compaction has reduced the program to just one line and 171 bytes in length. The unfortunate side effect is that it can't be listed but can be loaded and chained like any BASIC program.

To round up, the file "U.ELKLO" will load ROM images into Electron RAM banks and the file "U.BEEBLO" will load images into the BBC B or Master RAM banks using any disk filing system.

Saving

With the writing of this program, I started with the Loader program and made the necessary changes, to read the ROM and then save it to disk. One extra facility added is that the program can save any size of ROM image up to 16K e.g. If you know that the program in sideways ROM or RAM is only 4K in length then when asked for the length enter &1000, it is variable from &0001 to &4000.

The same compression techniques were used as last time to reduce the code to under 256 bytes but unfortunately, I couldn't get it all onto one line and the memory mapped ROM select address has to be changed for the BBC and Master computers from &FE05 to &FE30.

Program 3

          rn:P%=&A3C:$P%=N$:CALL&A00:PRINT"Done"

Code 2

                                                         XXXXXXXXXXXXXXX
   XXX":MODE7:PRINT'"SAVE ROM":PRINT'"ROM No. ";REPEAT;INPUTTAB(9,3)rn:U
   NTILrn>=0 AND rn<16:INPUT"Size in HEX & l$ Filename "N$:N$="SAVE "+N$
   +" 2000+"+"l$+" D9CD 8000"
   7?&A0A=rn:P%=&A3C:$P%=N$:CALL&A00:PRINT"Done"

Code 2

      0A00 A6 F4 A9 0C 85 F4 8D 30                                          
      0A08 FE A9 09 85 F4 8D 30 FE                                          
      0A10 A0 00 84 70 84 72 A9 80                                          
      0A18 85 71 A9 20 85 73 B1 70                                          
      0A20 91 72 C8 D0 F9 E6 71 E6                                          
      0A28 73 A5 73 C9 60 D0 EF 86                                          
      0A30 F4 8E 30 FE A2 3C A0 0A                                          
      0A38 20 F7 FF 60                                                      
Again to round up, the files to use are "U.ELKSA" for the Electron and U.BEEBSA" for the BBC B/B+ and Master 128.

Derek Walker, EUG #14