1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-11-20 02:32:36 +00:00

Improved C64 KERNAL loading example.

This commit is contained in:
jespergravgaard 2019-12-25 20:26:03 +01:00
parent 1e9162ca9c
commit 32752eb4cb
5 changed files with 545 additions and 358 deletions

View File

@ -29,6 +29,7 @@ void main() {
char status = loadFileToMemory(8, "SPRITE", LOAD_SPRITE);
if(status!=0xff) {
*BORDERCOL = 0x02;
error(status);
}
// Show the loaded sprite on screen
*SPRITES_ENABLE = %00000001;
@ -38,6 +39,30 @@ void main() {
SPRITES_YPOS[0] = 0x33;
}
// Load a file to memory
// Returns a status:
// - 0xff: Success
// - other: Kernal Error Code (https://commodore.ca/manuals/pdfs/commodore_error_messages.pdf)
char loadFileToMemory( char device, char* filename, char* address) {
setnam(filename);
setlfs(device);
return load(address, false);
}
// Basic ERROR function
// ERROR. Show error.
void error(char err) {
const char* errCode = 0xff;
*errCode = err;
asm {
// Basic SHOWERR function
// Input: X = Error Code
ldx errCode
jsr $a437
}
}
// Kernal SETNAM function
// SETNAM. Set file name parameters.
void setnam(char* filename) {
@ -95,21 +120,6 @@ char load(char* address, bool verify) {
}
// Load a file to memory
// Returns a status:
// - 0xff: Success
// - other: Kernal Error Code
char loadFileToMemory( char device, char* filename, char* address) {
setnam(filename);
setlfs(device);
return load(address, false);
}
/*
; A = $05 (DEVICE NOT PRESENT)
; A = $04 (FILE NOT FOUND)
; A = $1D (LOAD ERROR)
; A = $00 (BREAK, RUN/STOP has been pressed during loading)
*/

View File

@ -30,10 +30,13 @@
main: {
.const toSpritePtr1_return = LOAD_SPRITE/$40
jsr loadFileToMemory
cmp #$ff
tax
cpx #$ff
beq __b1
lda #2
sta BORDERCOL
txa
jsr error
__b1:
// Show the loaded sprite on screen
lda #1
@ -52,10 +55,20 @@ main: {
.byte 0
}
.segment Code
// Basic ERROR function
// ERROR. Show error.
// error(byte register(A) err)
error: {
.label errCode = $ff
sta errCode
tax
jsr $a437
rts
}
// Load a file to memory
// Returns a status:
// - 0xff: Success
// - other: Kernal Error Code
// - other: Kernal Error Code (https://commodore.ca/manuals/pdfs/commodore_error_messages.pdf)
loadFileToMemory: {
.label device = 8
jsr setnam

View File

@ -20,94 +20,105 @@ main::@4: scope:[main] from main
to:main::@2
main::@2: scope:[main] from main::@4
[9] *((const byte*) BORDERCOL) ← (byte) 2
[10] (byte) error::err#0 ← (byte) main::status#0
[11] call error
to:main::@1
main::@1: scope:[main] from main::@2 main::@4
[10] *((const byte*) SPRITES_ENABLE) ← (byte) 1
[12] *((const byte*) SPRITES_ENABLE) ← (byte) 1
to:main::toSpritePtr1
main::toSpritePtr1: scope:[main] from main::@1
[11] phi()
[13] phi()
to:main::@3
main::@3: scope:[main] from main::toSpritePtr1
[12] *((const byte*) SPRITES_PTR) ← (const byte) main::toSpritePtr1_return#0
[13] *((const byte*) SPRITES_COLS) ← (const byte) GREEN
[14] *((const byte*) SPRITES_XPOS) ← (byte) $15
[15] *((const byte*) SPRITES_YPOS) ← (byte) $33
[14] *((const byte*) SPRITES_PTR) ← (const byte) main::toSpritePtr1_return#0
[15] *((const byte*) SPRITES_COLS) ← (const byte) GREEN
[16] *((const byte*) SPRITES_XPOS) ← (byte) $15
[17] *((const byte*) SPRITES_YPOS) ← (byte) $33
to:main::@return
main::@return: scope:[main] from main::@3
[16] return
[18] return
to:@return
(void()) error((byte) error::err)
error: scope:[error] from main::@2
[19] *((const byte*) error::errCode) ← (byte) error::err#0
asm { ldxerrCode jsr$a437 }
to:error::@return
error::@return: scope:[error] from error
[21] return
to:@return
(byte()) loadFileToMemory((byte) loadFileToMemory::device , (byte*) loadFileToMemory::filename , (byte*) loadFileToMemory::address)
loadFileToMemory: scope:[loadFileToMemory] from main
[17] phi()
[18] call setnam
[22] phi()
[23] call setnam
to:loadFileToMemory::@1
loadFileToMemory::@1: scope:[loadFileToMemory] from loadFileToMemory
[19] phi()
[20] call setlfs
[24] phi()
[25] call setlfs
to:loadFileToMemory::@2
loadFileToMemory::@2: scope:[loadFileToMemory] from loadFileToMemory::@1
[21] phi()
[22] call load
[23] (byte) load::return#2 ← (byte) load::return#0
[26] phi()
[27] call load
[28] (byte) load::return#0 ← (byte) load::return#1
to:loadFileToMemory::@3
loadFileToMemory::@3: scope:[loadFileToMemory] from loadFileToMemory::@2
[24] (byte) loadFileToMemory::return#1 ← (byte) load::return#2
[29] (byte) loadFileToMemory::return#1 ← (byte) load::return#0
to:loadFileToMemory::@return
loadFileToMemory::@return: scope:[loadFileToMemory] from loadFileToMemory::@3
[25] return
[30] return
to:@return
(byte()) load((byte*) load::address , (bool) load::verify)
load: scope:[load] from loadFileToMemory::@2
[26] *((const byte*) load::loadOrVerify) ← (byte) 0
[27] *((const byte**) load::loadAddress) ← (const byte*) LOAD_SPRITE
[31] *((const byte*) load::loadOrVerify) ← (byte) 0
[32] *((const byte**) load::loadAddress) ← (const byte*) LOAD_SPRITE
asm { ldxloadAddress ldyloadAddress+1 ldaloadOrVerify jsr$ffd5 bcserror lda#$ff error: stastatus }
[29] (byte) load::return#0 ← *((const byte*) load::status)
[34] (byte) load::return#1 ← *((const byte*) load::status)
to:load::@return
load::@return: scope:[load] from load
[30] return
[35] return
to:@return
(void()) setlfs((byte) setlfs::device)
setlfs: scope:[setlfs] from loadFileToMemory::@1
[31] *((const byte*) setlfs::deviceNum) ← (const byte) loadFileToMemory::device#0
[36] *((const byte*) setlfs::deviceNum) ← (const byte) loadFileToMemory::device#0
asm { ldxdeviceNum lda#1 ldy#0 jsr$ffba }
to:setlfs::@return
setlfs::@return: scope:[setlfs] from setlfs
[33] return
[38] return
to:@return
(void()) setnam((byte*) setnam::filename)
setnam: scope:[setnam] from loadFileToMemory
[34] phi()
[35] call strlen
[36] (word) strlen::return#2 ← (word) strlen::len#2
[39] phi()
[40] call strlen
[41] (word) strlen::return#2 ← (word) strlen::len#2
to:setnam::@1
setnam::@1: scope:[setnam] from setnam
[37] (word~) setnam::$0 ← (word) strlen::return#2
[38] (byte~) setnam::$1 ← (byte)(word~) setnam::$0
[39] *((const byte*) setnam::filename_len) ← (byte~) setnam::$1
[40] *((const byte**) setnam::filename_ptr) ← (const string) main::filename
[42] (word~) setnam::$0 ← (word) strlen::return#2
[43] (byte~) setnam::$1 ← (byte)(word~) setnam::$0
[44] *((const byte*) setnam::filename_len) ← (byte~) setnam::$1
[45] *((const byte**) setnam::filename_ptr) ← (const string) main::filename
asm { ldafilename_len ldxfilename_ptr ldyfilename_ptr+1 jsr$ffbd }
to:setnam::@return
setnam::@return: scope:[setnam] from setnam::@1
[42] return
[47] return
to:@return
(word()) strlen((byte*) strlen::str)
strlen: scope:[strlen] from setnam
[43] phi()
[48] phi()
to:strlen::@1
strlen::@1: scope:[strlen] from strlen strlen::@2
[44] (word) strlen::len#2 ← phi( strlen/(byte) 0 strlen::@2/(word) strlen::len#1 )
[44] (byte*) strlen::str#2 ← phi( strlen/(const string) main::filename strlen::@2/(byte*) strlen::str#0 )
[45] if((byte) 0!=*((byte*) strlen::str#2)) goto strlen::@2
[49] (word) strlen::len#2 ← phi( strlen/(byte) 0 strlen::@2/(word) strlen::len#1 )
[49] (byte*) strlen::str#2 ← phi( strlen/(const string) main::filename strlen::@2/(byte*) strlen::str#0 )
[50] if((byte) 0!=*((byte*) strlen::str#2)) goto strlen::@2
to:strlen::@return
strlen::@return: scope:[strlen] from strlen::@1
[46] return
[51] return
to:@return
strlen::@2: scope:[strlen] from strlen::@1
[47] (word) strlen::len#1 ← ++ (word) strlen::len#2
[48] (byte*) strlen::str#0 ← ++ (byte*) strlen::str#2
[52] (word) strlen::len#1 ← ++ (word) strlen::len#2
[53] (byte*) strlen::str#0 ← ++ (byte*) strlen::str#2
to:strlen::@1

File diff suppressed because it is too large Load Diff

View File

@ -16,14 +16,19 @@
(const byte*) SPRITES_XPOS = (byte*) 53248
(const byte*) SPRITES_YPOS = (byte*) 53249
(const word) SPRITE_PTRS = (number) $3f8
(void()) error((byte) error::err)
(label) error::@return
(byte) error::err
(byte) error::err#0 reg byte a 4.0
(const byte*) error::errCode = (byte*) 255
(byte()) load((byte*) load::address , (bool) load::verify)
(label) load::@return
(byte*) load::address
(const byte**) load::loadAddress = (byte**) 254
(const byte*) load::loadOrVerify = (byte*) 253
(byte) load::return
(byte) load::return#0 reg byte a 1.3333333333333333
(byte) load::return#2 reg byte a 4.0
(byte) load::return#0 reg byte a 4.0
(byte) load::return#1 reg byte a 1.3333333333333333
(const byte*) load::status = (byte*) 253
(bool) load::verify
(byte()) loadFileToMemory((byte) loadFileToMemory::device , (byte*) loadFileToMemory::filename , (byte*) loadFileToMemory::address)
@ -46,7 +51,7 @@
(label) main::@return
(const string) main::filename[] = (string) "SPRITE"
(byte) main::status
(byte) main::status#0 reg byte a 4.0
(byte) main::status#0 reg byte x 2.0
(label) main::toSpritePtr1
(byte) main::toSpritePtr1_return
(const byte) main::toSpritePtr1_return#0 toSpritePtr1_return = (byte)(word)(const byte*) LOAD_SPRITE/(byte) $40
@ -79,8 +84,9 @@
zp[2]:2 [ strlen::str#2 strlen::str#0 ]
zp[2]:4 [ strlen::len#2 strlen::len#1 strlen::return#2 setnam::$0 ]
reg byte a [ loadFileToMemory::return#0 ]
reg byte a [ main::status#0 ]
reg byte a [ load::return#2 ]
reg byte a [ loadFileToMemory::return#1 ]
reg byte x [ main::status#0 ]
reg byte a [ error::err#0 ]
reg byte a [ load::return#0 ]
reg byte a [ loadFileToMemory::return#1 ]
reg byte a [ load::return#1 ]
reg byte a [ setnam::$1 ]