Meneer_Jansen

How to automatically run a file after it's LOADed.

Mar 20th, 2022 (edited)
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. [pastebin.com/8K79NWwW]
  2.  
  3. How to automatically run a file after it's LOADed.
  4.  
  5.  
  6. o==========o
  7. | Contents |
  8. o==========o
  9.  
  10. A. The code
  11.  
  12. B. How it works
  13. 1. How LOAD works
  14. 2. How the code works
  15. 3. Remarks
  16.  
  17. X. References
  18.  
  19. -<>-<>-<>-<>-
  20.  
  21.  
  22.  
  23.  
  24.  
  25. o=============o
  26. | A. The code |
  27. o=============o
  28.  
  29. 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).
  30.  
  31. ;---------------------------------------
  32. ; Autostart prg after load
  33. ; Important: use the label 'StProg'!!!!!
  34. ;---------------------------------------
  35.  
  36. ;There are 89 free bytes @ $2A7 - $300
  37. ;point STOP vector here
  38. *= $02ed
  39. ;Repair stop check vector (it's altered
  40. ;furtheron in the code). Only high byte
  41. ;was altered
  42. lda #$f6
  43. sta $0329
  44. ;Hat trick: call load code @ get-next-
  45. ;byte-loop start
  46. jsr $f4f3
  47. jmp stprog
  48.  
  49. ;---------------------------------------
  50. ;System vectors will be overwritten with
  51. ;0's if we leave this file empty up to
  52. ;stprog. See i'net for default values.
  53. *= $0300
  54. .word $e38b,$a483,$a57c,$a71a
  55. .word $a7e4,$ae86
  56. .byte 0,0,0,0 ;Tmp storage CPU
  57. jmp $b248 ;Usr functon, jmp+adr
  58. .byte 0 ;$313 unused
  59. .word $ea31,$fe66,$fe47,$f34a
  60. .word $f291,$f20e,$f250,$f333
  61. .word $f157,$f1ca
  62. ;TRAP! Point STOP vector which is @ $328
  63. ;to $02ED. Default points to $F6ED
  64. .word $02ed
  65. .word $f13e,$f32f,$fe66,$f4a5
  66. .word $f5ed
  67.  
  68. ;---------------------------------------
  69. ;Start of actual prog
  70. ;stprog = $startaddress e.g. $800
  71. ; *= stprog
  72.  
  73.  
  74.  
  75. o=================o
  76. | B. How it works |
  77. o=================o
  78.  
  79. 1. How LOAD works
  80. ~~~~~~~~~~~~~~~~~
  81. 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].
  82.  
  83. 2. How the code works
  84. ~~~~~~~~~~~~~~~~~~~~~
  85. 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.
  86.  
  87. 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).
  88.  
  89. 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.
  90.  
  91. 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.
  92.  
  93. 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.
  94.  
  95. 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).
  96.  
  97. 3. Remarks
  98. ~~~~~~~~~~
  99. 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.
  100.  
  101.  
  102.  
  103. o===============o
  104. | X. References |
  105. o===============o
  106.  
  107. [1] Codebase article on autostart:
  108. https://codebase64.org/doku.php?id=base:autostarting_disk_files
  109.  
  110. [2] Listing of the source code of BASIC and the Kernal:
  111. http://unusedino.de/ec64/technical/aay/c64/
  112.  
  113. [3] Tech info on the LOAD command from BASIC:
  114. https://www.c64-wiki.com/wiki/LOAD
  115. Remark: the routine is in Kernal.
  116.  
  117. [4] HTML C64 memory map by Joe Forster:
  118. https://sta.c64.org/cbm64mem.html
  119.  
  120.  
Add Comment
Please, Sign In to add comment