Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [pastebin.com/8K79NWwW]
- How to automatically run a file after it's LOADed.
- o==========o
- | Contents |
- o==========o
- A. The code
- B. How it works
- 1. How LOAD works
- 2. How the code works
- 3. Remarks
- X. References
- -<>-<>-<>-<>-
- o=============o
- | A. The code |
- o=============o
- Assembly code according to "Method 2" from Codebase64.org [1]. This code can be used as a "header file" so you can include it in any code with the '.include' pseudo-op. You just have to remember to set the label STPROG to the start of the program code that you want to execute (e.g. stprog = $0800) and to start your program's code there (i.e. *= stprog).
- ;---------------------------------------
- ; Autostart prg after load
- ; Important: use the label 'StProg'!!!!!
- ;---------------------------------------
- ;There are 89 free bytes @ $2A7 - $300
- ;point STOP vector here
- *= $02ed
- ;Repair stop check vector (it's altered
- ;furtheron in the code). Only high byte
- ;was altered
- lda #$f6
- sta $0329
- ;Hat trick: call load code @ get-next-
- ;byte-loop start
- jsr $f4f3
- jmp stprog
- ;---------------------------------------
- ;System vectors will be overwritten with
- ;0's if we leave this file empty up to
- ;stprog. See i'net for default values.
- *= $0300
- .word $e38b,$a483,$a57c,$a71a
- .word $a7e4,$ae86
- .byte 0,0,0,0 ;Tmp storage CPU
- jmp $b248 ;Usr functon, jmp+adr
- .byte 0 ;$313 unused
- .word $ea31,$fe66,$fe47,$f34a
- .word $f291,$f20e,$f250,$f333
- .word $f157,$f1ca
- ;TRAP! Point STOP vector which is @ $328
- ;to $02ED. Default points to $F6ED
- .word $02ed
- .word $f13e,$f32f,$fe66,$f4a5
- .word $f5ed
- ;---------------------------------------
- ;Start of actual prog
- ;stprog = $startaddress e.g. $800
- ; *= stprog
- o=================o
- | B. How it works |
- o=================o
- 1. How LOAD works
- ~~~~~~~~~~~~~~~~~
- One LOADs a file into RAM with command "LOAD" (Kernal routine $E168, [3]). During the routine a check is executed to see if the stop key is pressed. This proceeds via Kernal jump table address $FFD5 (Load RAM from device) which is a vector to $F49E (Load RAM). The 'Load RAM' routine does not jump to $F533 (Load file from tape) but proceeds on to routine $F4B8 (Load file from serial bus). At $F4F9 in this routine a check if the stop key was pressed is executed [2]. They say that this "stop key check" is executed at every loaded byte [1].
- 2. How the code works
- ~~~~~~~~~~~~~~~~~~~~~
- You as a user load the autoload file into RAM byte by byte from $02ED onwards (by using BASIC's LOAD "file" command). From $02A7 to $0300 (exclusive) there are 89 unused bytes [4] to use for a little routine like this.
- From $0300 to $0333 is the Vector Table for BASIC. BASIC uses this table to know where the instructions in the BASIC- and Kernal ROM chips are (or rather: to JuMP to them).
- If a file is written to RAM from our location ($2ED) to free RAM at $800 then it overwrites the Vector Table and screen memory with zero's. So our file must contain (a copy of) the Vector Table. No vectors have to be written from $334 to $400: it contains some unused bytes and the Datasette buffer. Screen memory (from $400 - $800) may be empty to start with.
- Nothing special happens until we reach the (re-)writing of the Stop Vector at $328/$329. It should point to $F6ED in the Kernal. However, we're gonna replace it with a vector to $02ED, which is the first part of our own program!. At this point in time the LOAD routine will not check if the Stop key was pressed but it'll start executing OUR code from $2ED onwards. Loading will stop and our code get's executed instead. First the code resets the Stop Vector to it's proper vale and then it jumps to a part of the Kernal's load routine at $F4F3. Now what does routine $F4F3 do? See [2]. Among others, it'll test the Stop key. No problem, we've restored the vector that points to it.
- Now, why has the first part of LOAD routine (from $F4B8 to $F4F3) to be skipped? Well, that part of the routine sets the start and end address of the file, the device from which to load etc. etc. This initialization has to be done only once. The actual loading starts at $F4F3.
- From now on our file will be loaded into RAM. When this is finished, undoubtedly, an RTS will be issued. Usually that brings us back to BASIC. In this case, however, it'll execute the JuMP to the start of the program we actually want to execute (e.g. a JuMP to 'stprog' or $800).
- 3. Remarks
- ~~~~~~~~~~
- If the C64 on which this code is executed has a custom Kernal then it might not work because the loading routine at $F4F3 may be somewhere else in Kernal ROM.
- o===============o
- | X. References |
- o===============o
- [1] Codebase article on autostart:
- https://codebase64.org/doku.php?id=base:autostarting_disk_files
- [2] Listing of the source code of BASIC and the Kernal:
- http://unusedino.de/ec64/technical/aay/c64/
- [3] Tech info on the LOAD command from BASIC:
- https://www.c64-wiki.com/wiki/LOAD
- Remark: the routine is in Kernal.
- [4] HTML C64 memory map by Joe Forster:
- https://sta.c64.org/cbm64mem.html
Add Comment
Please, Sign In to add comment