2021-06-19 20:11:26 +00:00
|
|
|
/// @file
|
2021-06-19 19:26:05 +00:00
|
|
|
/// A lightweight library for printing on the C64.
|
2021-06-19 20:28:44 +00:00
|
|
|
///
|
2021-06-19 19:26:05 +00:00
|
|
|
/// Printing with this library is done by calling print_ function for each element
|
2020-12-21 07:57:41 +00:00
|
|
|
// Commodore 64 PRG executable file
|
|
|
|
.file [name="constants.prg", type="prg", segments="Program"]
|
|
|
|
.segmentdef Program [segments="Basic, Code, Data"]
|
|
|
|
.segmentdef Basic [start=$0801]
|
|
|
|
.segmentdef Code [start=$80d]
|
|
|
|
.segmentdef Data [startAfter="Code"]
|
|
|
|
.segment Basic
|
2017-12-26 16:24:30 +00:00
|
|
|
:BasicUpstart(main)
|
|
|
|
.const GREEN = 5
|
|
|
|
.const RED = 2
|
2020-05-02 09:38:51 +00:00
|
|
|
.label BG_COLOR = $d021
|
2020-06-27 18:32:09 +00:00
|
|
|
.label print_screen = $400
|
2021-09-23 06:24:56 +00:00
|
|
|
.label print_char_cursor = 2
|
|
|
|
.label print_line_cursor = 6
|
2020-12-21 07:57:41 +00:00
|
|
|
.segment Code
|
2017-12-26 16:24:30 +00:00
|
|
|
main: {
|
2020-02-23 08:44:36 +00:00
|
|
|
// print_cls()
|
2017-12-26 16:24:30 +00:00
|
|
|
jsr print_cls
|
2020-05-02 09:38:51 +00:00
|
|
|
// *BG_COLOR = GREEN
|
2017-12-26 16:24:30 +00:00
|
|
|
lda #GREEN
|
2020-05-02 09:38:51 +00:00
|
|
|
sta BG_COLOR
|
2020-02-23 08:44:36 +00:00
|
|
|
// test_bytes()
|
2017-12-26 16:24:30 +00:00
|
|
|
jsr test_bytes
|
2020-02-23 08:44:36 +00:00
|
|
|
// test_sbytes()
|
2017-12-26 16:24:30 +00:00
|
|
|
jsr test_sbytes
|
2020-02-23 08:44:36 +00:00
|
|
|
// }
|
2017-12-26 16:24:30 +00:00
|
|
|
rts
|
|
|
|
}
|
2020-06-27 21:26:57 +00:00
|
|
|
// Clear the screen. Also resets current line/char cursor.
|
|
|
|
print_cls: {
|
|
|
|
// memset(print_screen, ' ', 1000)
|
|
|
|
jsr memset
|
|
|
|
// }
|
|
|
|
rts
|
|
|
|
}
|
|
|
|
// Test different byte constants
|
|
|
|
test_bytes: {
|
|
|
|
.const bb = 0
|
|
|
|
.const bc = 2
|
|
|
|
.const bd = bc-4
|
|
|
|
// assert_byte("0=0", bb, 0)
|
|
|
|
lda #<print_screen
|
|
|
|
sta.z print_line_cursor
|
|
|
|
lda #>print_screen
|
|
|
|
sta.z print_line_cursor+1
|
|
|
|
lda #0
|
|
|
|
sta.z assert_byte.c
|
|
|
|
ldx #bb
|
|
|
|
lda #<print_screen
|
|
|
|
sta.z print_char_cursor
|
|
|
|
lda #>print_screen
|
|
|
|
sta.z print_char_cursor+1
|
|
|
|
lda #<msg
|
|
|
|
sta.z assert_byte.msg
|
|
|
|
lda #>msg
|
|
|
|
sta.z assert_byte.msg+1
|
|
|
|
jsr assert_byte
|
|
|
|
lda.z print_line_cursor
|
|
|
|
sta.z print_char_cursor
|
|
|
|
lda.z print_line_cursor+1
|
|
|
|
sta.z print_char_cursor+1
|
|
|
|
// assert_byte("0+2=2", bc, 2)
|
|
|
|
lda #2
|
|
|
|
sta.z assert_byte.c
|
|
|
|
ldx #bc
|
|
|
|
lda #<msg1
|
|
|
|
sta.z assert_byte.msg
|
|
|
|
lda #>msg1
|
|
|
|
sta.z assert_byte.msg+1
|
|
|
|
jsr assert_byte
|
|
|
|
lda.z print_line_cursor
|
|
|
|
sta.z print_char_cursor
|
|
|
|
lda.z print_line_cursor+1
|
|
|
|
sta.z print_char_cursor+1
|
|
|
|
// assert_byte("0+2-4=254", bd, 254)
|
|
|
|
lda #$fe
|
|
|
|
sta.z assert_byte.c
|
|
|
|
ldx #bd
|
|
|
|
lda #<msg2
|
|
|
|
sta.z assert_byte.msg
|
|
|
|
lda #>msg2
|
|
|
|
sta.z assert_byte.msg+1
|
|
|
|
jsr assert_byte
|
|
|
|
// }
|
|
|
|
rts
|
2020-12-21 07:57:41 +00:00
|
|
|
.segment Data
|
2020-06-27 21:26:57 +00:00
|
|
|
msg2: .text "0+2-4=254"
|
|
|
|
.byte 0
|
|
|
|
}
|
2020-12-21 07:57:41 +00:00
|
|
|
.segment Code
|
2019-02-17 23:12:29 +00:00
|
|
|
// Test different signed byte constants
|
2017-12-26 16:24:30 +00:00
|
|
|
test_sbytes: {
|
2018-02-18 18:39:08 +00:00
|
|
|
.const bb = 0
|
2020-12-06 03:26:41 +00:00
|
|
|
.const bf = -$7f-$7f
|
2019-11-03 23:39:09 +00:00
|
|
|
.const bc = 2
|
2019-05-18 19:37:34 +00:00
|
|
|
.const bd = bc-4
|
2018-02-18 18:39:08 +00:00
|
|
|
.const be = -bd
|
2020-02-23 08:44:36 +00:00
|
|
|
// assert_sbyte("0=0", bb, 0)
|
2017-12-26 16:24:30 +00:00
|
|
|
lda #0
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z assert_sbyte.c
|
2017-12-30 16:24:30 +00:00
|
|
|
ldx #bb
|
2017-12-26 16:24:30 +00:00
|
|
|
lda #<msg
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z assert_sbyte.msg
|
2017-12-26 16:24:30 +00:00
|
|
|
lda #>msg
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z assert_sbyte.msg+1
|
2017-12-26 16:24:30 +00:00
|
|
|
jsr assert_sbyte
|
2020-02-23 08:44:36 +00:00
|
|
|
// assert_sbyte("0+2=2", bc, 2)
|
2017-12-26 16:24:30 +00:00
|
|
|
lda #2
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z assert_sbyte.c
|
2017-12-30 16:24:30 +00:00
|
|
|
ldx #bc
|
2017-12-26 16:24:30 +00:00
|
|
|
lda #<msg1
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z assert_sbyte.msg
|
2017-12-26 16:24:30 +00:00
|
|
|
lda #>msg1
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z assert_sbyte.msg+1
|
2017-12-26 16:24:30 +00:00
|
|
|
jsr assert_sbyte
|
2020-02-23 08:44:36 +00:00
|
|
|
// assert_sbyte("0+2-4=-2", bd, -2)
|
2017-12-26 16:24:30 +00:00
|
|
|
lda #-2
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z assert_sbyte.c
|
2017-12-30 16:24:30 +00:00
|
|
|
ldx #bd
|
2017-12-26 16:24:30 +00:00
|
|
|
lda #<msg2
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z assert_sbyte.msg
|
2017-12-26 16:24:30 +00:00
|
|
|
lda #>msg2
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z assert_sbyte.msg+1
|
2017-12-26 16:24:30 +00:00
|
|
|
jsr assert_sbyte
|
2020-02-23 08:44:36 +00:00
|
|
|
// assert_sbyte("-(0+2-4)=2", be, 2)
|
2017-12-26 16:24:30 +00:00
|
|
|
lda #2
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z assert_sbyte.c
|
2017-12-30 16:24:30 +00:00
|
|
|
ldx #be
|
2017-12-26 16:24:30 +00:00
|
|
|
lda #<msg3
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z assert_sbyte.msg
|
2017-12-26 16:24:30 +00:00
|
|
|
lda #>msg3
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z assert_sbyte.msg+1
|
2017-12-26 16:24:30 +00:00
|
|
|
jsr assert_sbyte
|
2020-02-23 08:44:36 +00:00
|
|
|
// assert_sbyte("-127-127=2", bf, 2)
|
2017-12-26 16:24:30 +00:00
|
|
|
lda #2
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z assert_sbyte.c
|
2017-12-30 16:24:30 +00:00
|
|
|
ldx #bf
|
2017-12-26 16:24:30 +00:00
|
|
|
lda #<msg4
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z assert_sbyte.msg
|
2017-12-26 16:24:30 +00:00
|
|
|
lda #>msg4
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z assert_sbyte.msg+1
|
2017-12-26 16:24:30 +00:00
|
|
|
jsr assert_sbyte
|
2020-02-23 08:44:36 +00:00
|
|
|
// }
|
2017-12-26 16:24:30 +00:00
|
|
|
rts
|
2020-12-21 07:57:41 +00:00
|
|
|
.segment Data
|
2019-08-07 17:36:19 +00:00
|
|
|
msg2: .text "0+2-4=-2"
|
|
|
|
.byte 0
|
|
|
|
msg3: .text "-(0+2-4)=2"
|
|
|
|
.byte 0
|
|
|
|
msg4: .text "-127-127=2"
|
|
|
|
.byte 0
|
2017-12-26 16:24:30 +00:00
|
|
|
}
|
2020-12-21 07:57:41 +00:00
|
|
|
.segment Code
|
2020-06-27 21:26:57 +00:00
|
|
|
// Copies the character c (an unsigned char) to the first num characters of the object pointed to by the argument str.
|
2021-08-10 15:48:55 +00:00
|
|
|
// void * memset(void *str, char c, unsigned int num)
|
2020-06-27 21:26:57 +00:00
|
|
|
memset: {
|
|
|
|
.const c = ' '
|
|
|
|
.const num = $3e8
|
|
|
|
.label str = print_screen
|
|
|
|
.label end = str+num
|
2021-09-23 06:24:56 +00:00
|
|
|
.label dst = 4
|
2020-06-27 21:26:57 +00:00
|
|
|
lda #<str
|
|
|
|
sta.z dst
|
|
|
|
lda #>str
|
|
|
|
sta.z dst+1
|
|
|
|
__b1:
|
|
|
|
// for(char* dst = str; dst!=end; dst++)
|
|
|
|
lda.z dst+1
|
|
|
|
cmp #>end
|
|
|
|
bne __b2
|
|
|
|
lda.z dst
|
|
|
|
cmp #<end
|
|
|
|
bne __b2
|
|
|
|
// }
|
|
|
|
rts
|
|
|
|
__b2:
|
|
|
|
// *dst = c
|
|
|
|
lda #c
|
|
|
|
ldy #0
|
|
|
|
sta (dst),y
|
|
|
|
// for(char* dst = str; dst!=end; dst++)
|
|
|
|
inc.z dst
|
|
|
|
bne !+
|
|
|
|
inc.z dst+1
|
|
|
|
!:
|
|
|
|
jmp __b1
|
|
|
|
}
|
2021-09-23 06:24:56 +00:00
|
|
|
// void assert_byte(__zp(4) char *msg, __register(X) char b, __zp(8) char c)
|
2020-06-27 21:26:57 +00:00
|
|
|
assert_byte: {
|
2021-09-23 06:24:56 +00:00
|
|
|
.label msg = 4
|
|
|
|
.label c = 8
|
2020-06-27 21:26:57 +00:00
|
|
|
// print_str(msg)
|
|
|
|
jsr print_str
|
|
|
|
// print_str(" ")
|
|
|
|
lda #<str
|
|
|
|
sta.z print_str.str
|
|
|
|
lda #>str
|
|
|
|
sta.z print_str.str+1
|
|
|
|
jsr print_str
|
|
|
|
// if(b!=c)
|
|
|
|
cpx.z c
|
|
|
|
bne __b1
|
|
|
|
// print_str("ok")
|
|
|
|
lda #<str2
|
|
|
|
sta.z print_str.str
|
|
|
|
lda #>str2
|
|
|
|
sta.z print_str.str+1
|
|
|
|
jsr print_str
|
|
|
|
__b2:
|
|
|
|
// print_ln()
|
|
|
|
jsr print_ln
|
|
|
|
// }
|
|
|
|
rts
|
|
|
|
__b1:
|
|
|
|
// *BG_COLOR = RED
|
|
|
|
lda #RED
|
|
|
|
sta BG_COLOR
|
|
|
|
// print_str("fail!")
|
|
|
|
lda #<str1
|
|
|
|
sta.z print_str.str
|
|
|
|
lda #>str1
|
|
|
|
sta.z print_str.str+1
|
|
|
|
jsr print_str
|
|
|
|
jmp __b2
|
|
|
|
}
|
2021-09-23 06:24:56 +00:00
|
|
|
// void assert_sbyte(__zp(4) char *msg, __register(X) signed char b, __zp(8) signed char c)
|
2017-12-26 16:24:30 +00:00
|
|
|
assert_sbyte: {
|
2021-09-23 06:24:56 +00:00
|
|
|
.label msg = 4
|
|
|
|
.label c = 8
|
2020-02-23 08:44:36 +00:00
|
|
|
// print_str(msg)
|
2019-08-07 19:00:19 +00:00
|
|
|
lda.z print_line_cursor
|
|
|
|
sta.z print_char_cursor
|
|
|
|
lda.z print_line_cursor+1
|
|
|
|
sta.z print_char_cursor+1
|
2020-02-23 08:44:36 +00:00
|
|
|
// print_str(msg)
|
2017-12-26 16:24:30 +00:00
|
|
|
jsr print_str
|
2020-02-23 08:44:36 +00:00
|
|
|
// print_str(" ")
|
2017-12-26 16:24:30 +00:00
|
|
|
lda #<str
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z print_str.str
|
2017-12-26 16:24:30 +00:00
|
|
|
lda #>str
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z print_str.str+1
|
2017-12-26 16:24:30 +00:00
|
|
|
jsr print_str
|
2020-02-23 08:44:36 +00:00
|
|
|
// if(b!=c)
|
2019-08-07 19:00:19 +00:00
|
|
|
cpx.z c
|
2019-09-29 21:13:37 +00:00
|
|
|
bne __b1
|
2020-02-23 08:44:36 +00:00
|
|
|
// print_str("ok")
|
2017-12-26 16:24:30 +00:00
|
|
|
lda #<str2
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z print_str.str
|
2017-12-26 16:24:30 +00:00
|
|
|
lda #>str2
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z print_str.str+1
|
2017-12-26 16:24:30 +00:00
|
|
|
jsr print_str
|
2019-09-29 21:13:37 +00:00
|
|
|
__b2:
|
2020-02-23 08:44:36 +00:00
|
|
|
// print_ln()
|
2017-12-26 16:24:30 +00:00
|
|
|
jsr print_ln
|
2020-02-23 08:44:36 +00:00
|
|
|
// }
|
2017-12-26 16:24:30 +00:00
|
|
|
rts
|
2019-09-29 21:13:37 +00:00
|
|
|
__b1:
|
2020-05-02 09:38:51 +00:00
|
|
|
// *BG_COLOR = RED
|
2018-04-21 19:38:30 +00:00
|
|
|
lda #RED
|
2020-05-02 09:38:51 +00:00
|
|
|
sta BG_COLOR
|
2020-02-23 08:44:36 +00:00
|
|
|
// print_str("fail!")
|
2017-12-26 16:24:30 +00:00
|
|
|
lda #<str1
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z print_str.str
|
2017-12-26 16:24:30 +00:00
|
|
|
lda #>str1
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z print_str.str+1
|
2017-12-26 16:24:30 +00:00
|
|
|
jsr print_str
|
2019-09-29 21:13:37 +00:00
|
|
|
jmp __b2
|
2017-12-26 16:24:30 +00:00
|
|
|
}
|
2019-02-17 23:12:29 +00:00
|
|
|
// Print a zero-terminated string
|
2021-09-23 06:24:56 +00:00
|
|
|
// void print_str(__zp(4) char *str)
|
2017-12-26 16:24:30 +00:00
|
|
|
print_str: {
|
2021-09-23 06:24:56 +00:00
|
|
|
.label str = 4
|
2019-09-29 21:13:37 +00:00
|
|
|
__b1:
|
2020-02-23 08:44:36 +00:00
|
|
|
// while(*str)
|
2017-12-26 16:24:30 +00:00
|
|
|
ldy #0
|
|
|
|
lda (str),y
|
2019-08-07 17:48:56 +00:00
|
|
|
cmp #0
|
2019-09-29 21:13:37 +00:00
|
|
|
bne __b2
|
2020-02-23 08:44:36 +00:00
|
|
|
// }
|
2017-12-26 16:24:30 +00:00
|
|
|
rts
|
2019-09-29 21:13:37 +00:00
|
|
|
__b2:
|
2020-04-18 18:54:39 +00:00
|
|
|
// print_char(*(str++))
|
2017-12-26 16:24:30 +00:00
|
|
|
ldy #0
|
|
|
|
lda (str),y
|
2020-04-18 18:54:39 +00:00
|
|
|
jsr print_char
|
|
|
|
// print_char(*(str++));
|
2019-08-07 19:00:19 +00:00
|
|
|
inc.z str
|
2017-12-26 16:24:30 +00:00
|
|
|
bne !+
|
2019-08-07 19:00:19 +00:00
|
|
|
inc.z str+1
|
2017-12-26 16:24:30 +00:00
|
|
|
!:
|
2019-09-29 21:13:37 +00:00
|
|
|
jmp __b1
|
2017-12-26 16:24:30 +00:00
|
|
|
}
|
2019-02-17 23:12:29 +00:00
|
|
|
// Print a newline
|
2017-12-26 16:24:30 +00:00
|
|
|
print_ln: {
|
2019-09-29 21:13:37 +00:00
|
|
|
__b1:
|
2024-01-02 18:45:16 +00:00
|
|
|
// print_line_cursor + 0x28
|
2019-03-18 01:23:29 +00:00
|
|
|
lda #$28
|
2017-12-26 16:24:30 +00:00
|
|
|
clc
|
2019-08-07 19:00:19 +00:00
|
|
|
adc.z print_line_cursor
|
|
|
|
sta.z print_line_cursor
|
2017-12-26 16:24:30 +00:00
|
|
|
bcc !+
|
2019-08-07 19:00:19 +00:00
|
|
|
inc.z print_line_cursor+1
|
2017-12-26 16:24:30 +00:00
|
|
|
!:
|
2020-02-23 08:44:36 +00:00
|
|
|
// while (print_line_cursor<print_char_cursor)
|
2019-08-07 19:00:19 +00:00
|
|
|
lda.z print_line_cursor+1
|
|
|
|
cmp.z print_char_cursor+1
|
2019-09-29 21:13:37 +00:00
|
|
|
bcc __b1
|
2017-12-26 16:24:30 +00:00
|
|
|
bne !+
|
2019-08-07 19:00:19 +00:00
|
|
|
lda.z print_line_cursor
|
|
|
|
cmp.z print_char_cursor
|
2019-09-29 21:13:37 +00:00
|
|
|
bcc __b1
|
2017-12-26 16:24:30 +00:00
|
|
|
!:
|
2020-02-23 08:44:36 +00:00
|
|
|
// }
|
2017-12-26 16:24:30 +00:00
|
|
|
rts
|
|
|
|
}
|
2020-06-27 21:26:57 +00:00
|
|
|
// Print a single char
|
2021-08-10 15:48:55 +00:00
|
|
|
// void print_char(__register(A) char ch)
|
2020-06-27 21:26:57 +00:00
|
|
|
print_char: {
|
|
|
|
// *(print_char_cursor++) = ch
|
2017-12-30 20:59:23 +00:00
|
|
|
ldy #0
|
2020-06-27 21:26:57 +00:00
|
|
|
sta (print_char_cursor),y
|
|
|
|
// *(print_char_cursor++) = ch;
|
|
|
|
inc.z print_char_cursor
|
2017-12-26 16:24:30 +00:00
|
|
|
bne !+
|
2020-06-27 21:26:57 +00:00
|
|
|
inc.z print_char_cursor+1
|
2017-12-26 16:24:30 +00:00
|
|
|
!:
|
2020-06-27 21:26:57 +00:00
|
|
|
// }
|
|
|
|
rts
|
2017-12-26 16:24:30 +00:00
|
|
|
}
|
2020-12-21 07:57:41 +00:00
|
|
|
.segment Data
|
2019-08-07 17:36:19 +00:00
|
|
|
msg: .text "0=0"
|
|
|
|
.byte 0
|
|
|
|
msg1: .text "0+2=2"
|
|
|
|
.byte 0
|
|
|
|
str: .text " "
|
|
|
|
.byte 0
|
|
|
|
str1: .text "fail!"
|
|
|
|
.byte 0
|
|
|
|
str2: .text "ok"
|
|
|
|
.byte 0
|