1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2025-01-01 13:30:50 +00:00

Updated helloworld.c to use <stdio.h> and printf()

This commit is contained in:
jespergravgaard 2020-05-02 15:38:02 +02:00
parent f3e27c1e3d
commit 3948ffd3df
5 changed files with 2217 additions and 846 deletions

View File

@ -1,6 +1,5 @@
#include <print.h>
#include <stdio.h>
void main() {
print_str("hello world!");
print_ln();
printf("hello world!\n");
}

View File

@ -1,88 +1,225 @@
.pc = $801 "Basic"
:BasicUpstart(main)
:BasicUpstart(__bbegin)
.pc = $80d "Program"
.label print_line_cursor = 2
.label print_char_cursor = 6
.label printf_cursor_x = 8
.label printf_cursor_y = 9
.label printf_cursor_ptr = $a
__bbegin:
// printf_cursor_x = 0
// X-position of cursor
lda #0
sta.z printf_cursor_x
// printf_cursor_y = 0
// Y-position of cursor
sta.z printf_cursor_y
// printf_cursor_ptr = PRINTF_SCREEN_ADDRESS
// Pointer to cursor address
lda #<$400
sta.z printf_cursor_ptr
lda #>$400
sta.z printf_cursor_ptr+1
jsr main
rts
main: {
// print_str("hello world!")
jsr print_str
// print_ln()
jsr print_ln
// printf("hello world!\n")
jsr printf_str
// }
rts
str: .text "hello world!"
str: .text @"hello world!\n"
.byte 0
}
// Print a newline
print_ln: {
lda #<$400
sta.z print_line_cursor
lda #>$400
sta.z print_line_cursor+1
__b1:
// print_line_cursor + $28
lda #$28
clc
adc.z print_line_cursor
sta.z print_line_cursor
bcc !+
inc.z print_line_cursor+1
!:
// while (print_line_cursor<print_char_cursor)
lda.z print_line_cursor+1
cmp.z print_char_cursor+1
bcc __b1
bne !+
lda.z print_line_cursor
cmp.z print_char_cursor
bcc __b1
!:
// }
rts
}
// Print a zero-terminated string
// print_str(byte* zp(4) str)
print_str: {
.label str = 4
lda #<$400
sta.z print_char_cursor
lda #>$400
sta.z print_char_cursor+1
// Handles escape codes such as newline
// printf_str(byte* zp(2) str)
printf_str: {
.label str = 2
lda #<main.str
sta.z str
lda #>main.str
sta.z str+1
__b1:
// while(*str)
ldy #0
lda (str),y
cmp #0
bne __b2
// }
rts
__b2:
// print_char(*(str++))
// ch = *str++
ldy #0
lda (str),y
jsr print_char
// print_char(*(str++));
inc.z str
bne !+
inc.z str+1
!:
jmp __b1
// if(ch==0)
cmp #0
bne __b3
// }
rts
__b3:
// if(ch=='\n')
cmp #'\n'
beq __b4
// printf_char(ch)
jsr printf_char
jmp __b2
__b4:
// printf_ln()
jsr printf_ln
jmp __b2
}
// Print a single char
// print_char(byte register(A) ch)
print_char: {
// *(print_char_cursor++) = ch
ldy #0
sta (print_char_cursor),y
// *(print_char_cursor++) = ch;
inc.z print_char_cursor
bne !+
inc.z print_char_cursor+1
// Print a newline
printf_ln: {
.label __0 = $a
.label __1 = $a
// printf_cursor_ptr - printf_cursor_x
sec
lda.z __0
sbc.z printf_cursor_x
sta.z __0
bcs !+
dec.z __0+1
!:
// printf_cursor_ptr - printf_cursor_x + PRINTF_SCREEN_WIDTH
lda #$28
clc
adc.z __1
sta.z __1
bcc !+
inc.z __1+1
!:
// printf_cursor_ptr = printf_cursor_ptr - printf_cursor_x + PRINTF_SCREEN_WIDTH
// printf_cursor_x = 0
lda #0
sta.z printf_cursor_x
// printf_cursor_y++;
inc.z printf_cursor_y
// printf_scroll()
jsr printf_scroll
// }
rts
}
// Scroll the entire screen if the cursor is on the last line
printf_scroll: {
.label __4 = $a
// if(printf_cursor_y==PRINTF_SCREEN_HEIGHT)
lda #$19
cmp.z printf_cursor_y
bne __breturn
// memcpy(PRINTF_SCREEN_ADDRESS, PRINTF_SCREEN_ADDRESS+PRINTF_SCREEN_WIDTH, PRINTF_SCREEN_BYTES-PRINTF_SCREEN_WIDTH)
jsr memcpy
// memset(PRINTF_SCREEN_ADDRESS+PRINTF_SCREEN_BYTES-PRINTF_SCREEN_WIDTH, ' ', PRINTF_SCREEN_WIDTH)
jsr memset
// printf_cursor_ptr-PRINTF_SCREEN_WIDTH
lda.z __4
sec
sbc #<$28
sta.z __4
lda.z __4+1
sbc #>$28
sta.z __4+1
// printf_cursor_ptr = printf_cursor_ptr-PRINTF_SCREEN_WIDTH
// printf_cursor_y--;
dec.z printf_cursor_y
__breturn:
// }
rts
}
// Copies the character c (an unsigned char) to the first num characters of the object pointed to by the argument str.
memset: {
.const c = ' '
.const num = $28
.label str = $400+$28*$19-$28
.label end = str+num
.label dst = 4
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
}
// Copy block of memory (forwards)
// Copies the values of num bytes from the location pointed to by source directly to the memory block pointed to by destination.
memcpy: {
.const num = $28*$19-$28
.label destination = $400
.label source = $400+$28
.label src_end = source+num
.label dst = 6
.label src = 4
lda #<destination
sta.z dst
lda #>destination
sta.z dst+1
lda #<source
sta.z src
lda #>source
sta.z src+1
__b1:
// while(src!=src_end)
lda.z src+1
cmp #>src_end
bne __b2
lda.z src
cmp #<src_end
bne __b2
// }
rts
__b2:
// *dst++ = *src++
ldy #0
lda (src),y
sta (dst),y
// *dst++ = *src++;
inc.z dst
bne !+
inc.z dst+1
!:
inc.z src
bne !+
inc.z src+1
!:
jmp __b1
}
// Print a single char
// If the end of the screen is reached scroll it up one char and place the cursor at the
// printf_char(byte register(A) ch)
printf_char: {
// *(printf_cursor_ptr++) = ch
ldy #0
sta (printf_cursor_ptr),y
// *(printf_cursor_ptr++) = ch;
inc.z printf_cursor_ptr
bne !+
inc.z printf_cursor_ptr+1
!:
// if(++printf_cursor_x==PRINTF_SCREEN_WIDTH)
inc.z printf_cursor_x
lda #$28
cmp.z printf_cursor_x
bne __breturn
// printf_cursor_x = 0
lda #0
sta.z printf_cursor_x
// ++printf_cursor_y;
inc.z printf_cursor_y
// printf_scroll()
jsr printf_scroll
__breturn:
// }
rts
}

View File

@ -2,63 +2,134 @@
[0] phi()
to:@1
@1: scope:[] from @begin
[1] phi()
[2] call main
[1] (byte) printf_cursor_x ← (byte) 0
[2] (byte) printf_cursor_y ← (byte) 0
[3] (byte*) printf_cursor_ptr ← (byte*) 1024
to:@2
@2: scope:[] from @1
[4] phi()
[5] call main
to:@end
@end: scope:[] from @1
[3] phi()
@end: scope:[] from @2
[6] phi()
(void()) main()
main: scope:[main] from @1
[4] phi()
[5] call print_str
to:main::@1
main::@1: scope:[main] from main
[6] phi()
[7] call print_ln
main: scope:[main] from @2
[7] phi()
[8] call printf_str
to:main::@return
main::@return: scope:[main] from main::@1
[8] return
main::@return: scope:[main] from main
[9] return
to:@return
(void()) print_ln()
print_ln: scope:[print_ln] from main::@1
[9] phi()
to:print_ln::@1
print_ln::@1: scope:[print_ln] from print_ln print_ln::@1
[10] (byte*) print_line_cursor#6 ← phi( print_ln/(byte*) 1024 print_ln::@1/(byte*) print_line_cursor#1 )
[11] (byte*) print_line_cursor#1 ← (byte*) print_line_cursor#6 + (byte) $28
[12] if((byte*) print_line_cursor#1<(byte*) print_char_cursor#12) goto print_ln::@1
to:print_ln::@return
print_ln::@return: scope:[print_ln] from print_ln::@1
[13] return
(void()) printf_str((byte*) printf_str::str)
printf_str: scope:[printf_str] from main
[10] phi()
to:printf_str::@1
printf_str::@1: scope:[printf_str] from printf_str printf_str::@4 printf_str::@5
[11] (byte*) printf_str::str#2 ← phi( printf_str/(const byte*) main::str printf_str::@4/(byte*) printf_str::str#0 printf_str::@5/(byte*) printf_str::str#0 )
to:printf_str::@2
printf_str::@2: scope:[printf_str] from printf_str::@1
[12] (byte) printf_str::ch#0 ← *((byte*) printf_str::str#2)
[13] (byte*) printf_str::str#0 ← ++ (byte*) printf_str::str#2
[14] if((byte) printf_str::ch#0!=(byte) 0) goto printf_str::@3
to:printf_str::@return
printf_str::@return: scope:[printf_str] from printf_str::@2
[15] return
to:@return
printf_str::@3: scope:[printf_str] from printf_str::@2
[16] if((byte) printf_str::ch#0==(byte) '
') goto printf_str::@4
to:printf_str::@5
printf_str::@5: scope:[printf_str] from printf_str::@3
[17] (byte) printf_char::ch#0 ← (byte) printf_str::ch#0
[18] call printf_char
to:printf_str::@1
printf_str::@4: scope:[printf_str] from printf_str::@3
[19] phi()
[20] call printf_ln
to:printf_str::@1
(void()) printf_ln()
printf_ln: scope:[printf_ln] from printf_str::@4
[21] (byte*~) printf_ln::$0 ← (byte*) printf_cursor_ptr - (byte) printf_cursor_x
[22] (byte*~) printf_ln::$1 ← (byte*~) printf_ln::$0 + (byte) $28
[23] (byte*) printf_cursor_ptr ← (byte*~) printf_ln::$1
[24] (byte) printf_cursor_x ← (byte) 0
[25] (byte) printf_cursor_y ← ++ (byte) printf_cursor_y
[26] call printf_scroll
to:printf_ln::@return
printf_ln::@return: scope:[printf_ln] from printf_ln
[27] return
to:@return
(void()) print_str((byte*) print_str::str)
print_str: scope:[print_str] from main
[14] phi()
to:print_str::@1
print_str::@1: scope:[print_str] from print_str print_str::@3
[15] (byte*) print_char_cursor#12 ← phi( print_str/(byte*) 1024 print_str::@3/(byte*) print_char_cursor#16 )
[15] (byte*) print_str::str#2 ← phi( print_str/(const byte*) main::str print_str::@3/(byte*) print_str::str#0 )
[16] if((byte) 0!=*((byte*) print_str::str#2)) goto print_str::@2
to:print_str::@return
print_str::@return: scope:[print_str] from print_str::@1
[17] return
(void()) printf_scroll()
printf_scroll: scope:[printf_scroll] from printf_char::@1 printf_ln
[28] if((byte) printf_cursor_y!=(byte) $19) goto printf_scroll::@return
to:printf_scroll::@1
printf_scroll::@1: scope:[printf_scroll] from printf_scroll
[29] phi()
[30] call memcpy
to:printf_scroll::@2
printf_scroll::@2: scope:[printf_scroll] from printf_scroll::@1
[31] phi()
[32] call memset
to:printf_scroll::@3
printf_scroll::@3: scope:[printf_scroll] from printf_scroll::@2
[33] (byte*~) printf_scroll::$4 ← (byte*) printf_cursor_ptr - (byte) $28
[34] (byte*) printf_cursor_ptr ← (byte*~) printf_scroll::$4
[35] (byte) printf_cursor_y ← -- (byte) printf_cursor_y
to:printf_scroll::@return
printf_scroll::@return: scope:[printf_scroll] from printf_scroll printf_scroll::@3
[36] return
to:@return
print_str::@2: scope:[print_str] from print_str::@1
[18] (byte) print_char::ch#0 ← *((byte*) print_str::str#2)
[19] call print_char
to:print_str::@3
print_str::@3: scope:[print_str] from print_str::@2
[20] (byte*) print_str::str#0 ← ++ (byte*) print_str::str#2
to:print_str::@1
(void()) print_char((byte) print_char::ch)
print_char: scope:[print_char] from print_str::@2
[21] *((byte*) print_char_cursor#12) ← (byte) print_char::ch#0
[22] (byte*) print_char_cursor#16 ← ++ (byte*) print_char_cursor#12
to:print_char::@return
print_char::@return: scope:[print_char] from print_char
[23] return
(void*()) memset((void*) memset::str , (byte) memset::c , (word) memset::num)
memset: scope:[memset] from printf_scroll::@2
[37] phi()
to:memset::@1
memset::@1: scope:[memset] from memset memset::@2
[38] (byte*) memset::dst#2 ← phi( memset/(byte*)(const void*) memset::str#0 memset::@2/(byte*) memset::dst#1 )
[39] if((byte*) memset::dst#2!=(const byte*) memset::end#0) goto memset::@2
to:memset::@return
memset::@return: scope:[memset] from memset::@1
[40] return
to:@return
memset::@2: scope:[memset] from memset::@1
[41] *((byte*) memset::dst#2) ← (const byte) memset::c#0
[42] (byte*) memset::dst#1 ← ++ (byte*) memset::dst#2
to:memset::@1
(void*()) memcpy((void*) memcpy::destination , (void*) memcpy::source , (word) memcpy::num)
memcpy: scope:[memcpy] from printf_scroll::@1
[43] phi()
to:memcpy::@1
memcpy::@1: scope:[memcpy] from memcpy memcpy::@2
[44] (byte*) memcpy::dst#2 ← phi( memcpy/(byte*)(const void*) memcpy::destination#0 memcpy::@2/(byte*) memcpy::dst#1 )
[44] (byte*) memcpy::src#2 ← phi( memcpy/(byte*)(const void*) memcpy::source#0 memcpy::@2/(byte*) memcpy::src#1 )
[45] if((byte*) memcpy::src#2!=(const byte*) memcpy::src_end#0) goto memcpy::@2
to:memcpy::@return
memcpy::@return: scope:[memcpy] from memcpy::@1
[46] return
to:@return
memcpy::@2: scope:[memcpy] from memcpy::@1
[47] *((byte*) memcpy::dst#2) ← *((byte*) memcpy::src#2)
[48] (byte*) memcpy::dst#1 ← ++ (byte*) memcpy::dst#2
[49] (byte*) memcpy::src#1 ← ++ (byte*) memcpy::src#2
to:memcpy::@1
(void()) printf_char((byte) printf_char::ch)
printf_char: scope:[printf_char] from printf_str::@5
[50] *((byte*) printf_cursor_ptr) ← (byte) printf_char::ch#0
[51] (byte*) printf_cursor_ptr ← ++ (byte*) printf_cursor_ptr
[52] (byte) printf_cursor_x ← ++ (byte) printf_cursor_x
[53] if((byte) printf_cursor_x!=(byte) $28) goto printf_char::@return
to:printf_char::@1
printf_char::@1: scope:[printf_char] from printf_char
[54] (byte) printf_cursor_x ← (byte) 0
[55] (byte) printf_cursor_y ← ++ (byte) printf_cursor_y
[56] call printf_scroll
to:printf_char::@return
printf_char::@return: scope:[printf_char] from printf_char printf_char::@1
[57] return
to:@return

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,5 @@
(label) @1
(label) @2
(label) @begin
(label) @end
(const byte) RADIX::BINARY = (number) 2
@ -6,33 +7,90 @@
(const byte) RADIX::HEXADECIMAL = (number) $10
(const byte) RADIX::OCTAL = (number) 8
(void()) main()
(label) main::@1
(label) main::@return
(const byte*) main::str[(byte) $d] = (byte*) "hello world!"
(void()) print_char((byte) print_char::ch)
(label) print_char::@return
(byte) print_char::ch
(byte) print_char::ch#0 reg byte a 11002.0
(byte*) print_char_cursor
(byte*) print_char_cursor#12 print_char_cursor zp[2]:6 2000.363636363636
(byte*) print_char_cursor#16 print_char_cursor zp[2]:6 2750.5
(byte*) print_line_cursor
(byte*) print_line_cursor#1 print_line_cursor zp[2]:2 1501.5
(byte*) print_line_cursor#6 print_line_cursor zp[2]:2 2002.0
(void()) print_ln()
(label) print_ln::@1
(label) print_ln::@return
(byte*) print_screen
(void()) print_str((byte*) print_str::str)
(label) print_str::@1
(label) print_str::@2
(label) print_str::@3
(label) print_str::@return
(byte*) print_str::str
(byte*) print_str::str#0 str zp[2]:4 2002.0
(byte*) print_str::str#2 str zp[2]:4 1001.0
(const byte*) main::str[(byte) $e] = (byte*) "hello world!
"
(void*()) memcpy((void*) memcpy::destination , (void*) memcpy::source , (word) memcpy::num)
(label) memcpy::@1
(label) memcpy::@2
(label) memcpy::@return
(void*) memcpy::destination
(const void*) memcpy::destination#0 destination = (void*) 1024
(byte*) memcpy::dst
(byte*) memcpy::dst#1 dst zp[2]:6 1.00000001E8
(byte*) memcpy::dst#2 dst zp[2]:6 1.00000001E8
(word) memcpy::num
(const word) memcpy::num#0 num = (word)(number) $28*(number) $19-(number) $28
(void*) memcpy::return
(void*) memcpy::source
(const void*) memcpy::source#0 source = (void*)(number) $400+(number) $28
(byte*) memcpy::src
(byte*) memcpy::src#1 src zp[2]:4 2.00000002E8
(byte*) memcpy::src#2 src zp[2]:4 1.00000001E8
(byte*) memcpy::src_end
(const byte*) memcpy::src_end#0 src_end = (byte*)(const void*) memcpy::source#0+(const word) memcpy::num#0
(void*()) memset((void*) memset::str , (byte) memset::c , (word) memset::num)
(label) memset::@1
(label) memset::@2
(label) memset::@return
(byte) memset::c
(const byte) memset::c#0 c = (byte) ' '
(byte*) memset::dst
(byte*) memset::dst#1 dst zp[2]:4 2.00000002E8
(byte*) memset::dst#2 dst zp[2]:4 1.3333333466666667E8
(byte*) memset::end
(const byte*) memset::end#0 end = (byte*)(const void*) memset::str#0+(const word) memset::num#0
(word) memset::num
(const word) memset::num#0 num = (byte) $28
(void*) memset::return
(void*) memset::str
(const void*) memset::str#0 str = (void*)(number) $400+(number) $28*(number) $19-(number) $28
(const byte*) printf_buffer_number::digits[(number) $b] = { fill( $b, 0) }
(byte) printf_buffer_number::sign
(void()) printf_char((byte) printf_char::ch)
(label) printf_char::@1
(label) printf_char::@return
(byte) printf_char::ch
(byte) printf_char::ch#0 reg byte a 11002.0
(byte*) printf_cursor_ptr loadstore zp[2]:10 7353.205882352942
(byte) printf_cursor_x loadstore zp[1]:8 2222.5185185185182
(byte) printf_cursor_y loadstore zp[1]:9 8947.605263157893
(byte) printf_format_number::justify_left
(byte) printf_format_number::min_length
(byte) printf_format_number::radix
(byte) printf_format_number::sign_always
(byte) printf_format_number::upper_case
(byte) printf_format_number::zero_padding
(byte) printf_format_string::justify_left
(byte) printf_format_string::min_length
(void()) printf_ln()
(byte*~) printf_ln::$0 zp[2]:10 20002.0
(byte*~) printf_ln::$1 zp[2]:10 20002.0
(label) printf_ln::@return
(void()) printf_scroll()
(byte*~) printf_scroll::$4 zp[2]:10 200002.0
(label) printf_scroll::@1
(label) printf_scroll::@2
(label) printf_scroll::@3
(label) printf_scroll::@return
(void()) printf_str((byte*) printf_str::str)
(label) printf_str::@1
(label) printf_str::@2
(label) printf_str::@3
(label) printf_str::@4
(label) printf_str::@5
(label) printf_str::@return
(byte) printf_str::ch
(byte) printf_str::ch#0 reg byte a 1001.0
(byte*) printf_str::str
(byte*) printf_str::str#0 str zp[2]:2 429.0
(byte*) printf_str::str#2 str zp[2]:2 2002.0
zp[2]:2 [ print_line_cursor#6 print_line_cursor#1 ]
zp[2]:4 [ print_str::str#2 print_str::str#0 ]
zp[2]:6 [ print_char_cursor#12 print_char_cursor#16 ]
reg byte a [ print_char::ch#0 ]
zp[2]:2 [ printf_str::str#2 printf_str::str#0 ]
zp[2]:4 [ memcpy::src#2 memcpy::src#1 memset::dst#2 memset::dst#1 ]
zp[2]:6 [ memcpy::dst#2 memcpy::dst#1 ]
zp[1]:8 [ printf_cursor_x ]
zp[1]:9 [ printf_cursor_y ]
zp[2]:10 [ printf_cursor_ptr printf_scroll::$4 printf_ln::$0 printf_ln::$1 ]
reg byte a [ printf_str::ch#0 ]
reg byte a [ printf_char::ch#0 ]