c64: Small refactor

This commit is contained in:
Sijmen 2020-12-09 03:13:51 +01:00
parent dc0afd27d2
commit c2b7852374
Signed by: vijfhoek
GPG Key ID: DAF7821E067D9C48
4 changed files with 120 additions and 82 deletions

View File

@ -1,122 +1,160 @@
// vim: filetype=kickass // vim: filetype=kickass
*=* "Day 01" * = * "Day 01"
//
// day01
//
// Destroys:
// a, x, y, $02..$04, $10..$1c, $20..$25, $fd..$fe, everything you've ever loved
//
.const input_pointer = $03
.const buffer_pointer = $20
.const atoi_result = $22
.const outer_pointer = $20
.const inner_pointer = $22
.const remainder = $24
// Need to be $03 and $fd due to multiply_16bit_unsigned
.const outer_value = $03
.const inner_value = $fd
day01: day01:
move_16_imm($03, $04, str_title) move_16_imm($03, str_title)
jsr write_string jsr write_string
move_16_imm($03, $04, day01_input) //
move_16_imm($25, $26, buffer) // Parse the input at day01_input into an array of integers.
//
move_16_imm(input_pointer, day01_input)
move_16_imm(buffer_pointer, buffer)
ldy #0 ldy #0
!line: !line:
lda #0 // Create a temporary 16-bit integer to store the result in
sta $22 // in between digits.
sta $23 move_16_imm(atoi_result, 0)
!digit: !digit:
lda ($03), y // Check for the newline character at the end of the line.
lda (input_pointer), y
cmp #'\n' cmp #'\n'
beq !break+ beq !newline+
i16_mul10($22, $23) // Multiply the stored number by 10.
i16_mul10(atoi_result)
lda ($03), y // Subtract '0' from the current digit to convert it to "binary".
lda (input_pointer), y
sec sec
sbc #'0' sbc #'0'
i16_i8_add_a($22, $23) // Add the binary digit to the stored number
i16_i8_add_a(atoi_result)
// Increment y.
iny iny
bne !digit- bne !digit-
inc $04 // If y overflows, increment the MSB of the input pointer as well,
// for a 16-bit increment.
inc input_pointer + 1
jmp !digit- jmp !digit-
!break: !newline:
sty $24 sty zp_temp
// Append the result of the conversion to the array.
ldy #0 ldy #0
lda $22 lda atoi_result + 0
sta ($25), y sta (buffer_pointer), y
iny iny
lda $23 lda atoi_result + 1
sta ($25), y sta (buffer_pointer), y
// Increment the buffer pointer by 2.
lda #2 lda #2
i16_i8_add_a($25, $26) i16_i8_add_a(buffer_pointer)
ldy $24 ldy zp_temp
iny iny
bne !+ bne !+
inc $04 inc input_pointer + 1
!: !:
lda ($03), y
// Check for the null terminator.
lda (input_pointer), y
bne !line- bne !line-
move_16_imm($25, $26, buffer) move_16_imm($03, str_parse_done)
move_16_imm($03, $04, str_parse_done)
jsr write_string jsr write_string
move_16_imm(outer_pointer, buffer)
// Add every number to every other number // Add every number to every other number
!outer: !outer:
move_16_imm($27, $28, buffer) move_16_imm(inner_pointer, buffer)
// $03..$04 = buffer[i] // $03..$04 = buffer[i]
ldy #0 ldy #0
lda ($25), y lda (outer_pointer), y
sta $03 sta outer_value
iny iny
lda ($25), y lda (outer_pointer), y
sta $04 sta outer_value + 1
dey dey
lda ($25), y lda (outer_pointer), y
bne !not_zero+ bne !not_zero+
iny iny
lda ($25), y lda (outer_pointer), y
bne !not_zero+ bne !not_zero+
.break .break
jmp !error+ jmp !error+
!not_zero: !not_zero:
// Subtract from 2020 to see what number we need // Subtract from 2020 to see what number we need
i16_imm_i16_sub($fb, $fc, 2020, $03, $04) i16_imm_i16_sub(remainder, 2020, outer_value)
!inner: !inner:
// $fd..$fe = buffer[j] // $fd..$fe = buffer[j]
ldy #0 ldy #0
lda ($27), y lda (inner_pointer), y
sta $fd sta inner_value
iny iny
lda ($27), y lda (inner_pointer), y
sta $fe sta inner_value + 1
dey dey
lda ($27), y lda (inner_pointer), y
bne !not_zero+ bne !not_zero+
iny iny
lda ($27), y lda (inner_pointer), y
bne !not_zero+ bne !not_zero+
i16_inc($25, $26) i16_inc(outer_pointer)
jmp !outer- jmp !outer-
!not_zero:
!not_zero:
// j++ // j++
lda #2 lda #2
i16_i8_add_a($27, $28) i16_i8_add_a(inner_pointer)
i16_i16_cmp_bne($fb, $fc, $fd, $fe, !inner-)
// Continue the loop if the number didn't match the needed number.
i16_i16_cmp_bne(remainder, inner_value, !inner-)
!done: !done:
// Multiply the results together // Multiply the results together
sec sec
jsr multiply_16bit_unsigned jsr multiply_16bit_unsigned
move_16_imm($03, $04, str_calc_done) move_16_imm($03, str_calc_done)
jsr write_string jsr write_string
// Print the 32-bit result as decimal.
lda $25 lda $25
sta udivmod32_dividend + 3 sta udivmod32_dividend + 3
lda $24 lda $24
@ -130,7 +168,7 @@ day01:
rts rts
!error: !error:
move_16_imm($03, $04, str_no_result) move_16_imm($03, str_no_result)
jsr write_string jsr write_string
rts rts

View File

@ -38,11 +38,11 @@ day01_input:
.const cursor_pointer_lo = $05 .const cursor_pointer_lo = $05
.const cursor_pointer_hi = $06 .const cursor_pointer_hi = $06
.macro move_16_imm(dst_lo, dst_hi, src) { .macro move_16_imm(dst, imm) {
lda #<src lda #<imm
sta dst_lo sta dst
lda #>src lda #>imm
sta dst_hi sta dst + 1
} }
// //

View File

@ -86,17 +86,17 @@
i16_mul2(lo, hi) i16_mul2(lo, hi)
} }
// Destroys: a, lo, hi, $fd, $fe // Destroys: a, $fd, $fe
.macro i16_mul10(lo, hi) { .macro i16_mul10(dst) {
i16_mul2(lo, hi) i16_mul2(dst, dst + 1)
lda lo lda dst
sta $fd sta $fd
lda hi lda dst + 1
sta $fe sta $fe
i16_mul4(lo, hi) i16_mul4(dst, dst + 1)
i16_i16_add(lo, hi, $fd, $fe) i16_i16_add(dst, dst + 1, $fd, $fe)
} }
.macro u32_mul2(lsb) { .macro u32_mul2(lsb) {
@ -140,13 +140,13 @@
sta dst + 3 sta dst + 3
} }
// Destroys: a, dst_lo, dst_hi // Destroys: a
.macro i16_i8_add_a(dst_lo, dst_hi) { .macro i16_i8_add_a(dst) {
clc clc
adc dst_lo adc dst
sta dst_lo sta dst
bcc !cc+ bcc !cc+
inc dst_hi inc dst + 1
!cc: !cc:
} }
@ -162,9 +162,9 @@
} }
// Destroys: a, dst_lo, dst_hi // Destroys: a, dst_lo, dst_hi
.macro i16_inc(lo, hi) { .macro i16_inc(dst) {
lda #1 lda #1
i16_i8_add_a(lo, hi) i16_i8_add_a(dst)
} }
.macro i16_i16_sub_imm(dst_lo, dst_hi, src_lo, src_hi, imm) { .macro i16_i16_sub_imm(dst_lo, dst_hi, src_lo, src_hi, imm) {
@ -191,24 +191,24 @@
sta dst_hi sta dst_hi
} }
.macro i16_imm_i16_sub(dst_lo, dst_hi, imm, src_lo, src_hi) { .macro i16_imm_i16_sub(dst, imm, src) {
lda src_lo lda src
eor #$ff eor #$ff
sec sec
adc #<imm adc #<imm
sta dst_lo sta dst
lda src_hi lda src + 1
eor #$ff eor #$ff
adc #>imm adc #>imm
sta dst_hi sta dst + 1
} }
.macro i16_i16_cmp_bne(a_lo, a_hi, b_lo, b_hi, target) { .macro i16_i16_cmp_bne(a, b, target) {
lda a_lo lda a
cmp b_lo cmp b
bne target bne target
lda a_hi lda a + 1
cmp b_hi cmp b + 1
bne target bne target
} }

View File

@ -101,7 +101,7 @@ print_carriage_return:
// - $03-$04 - Pointer to null-terminated string. // - $03-$04 - Pointer to null-terminated string.
// - $05-$06 - Cursor pointer. // - $05-$06 - Cursor pointer.
// //
// Destroys: a, x, y // Destroys: a, x, y, $02
// //
write_string: write_string:
ldx #0 ldx #0
@ -118,7 +118,7 @@ write_string:
sta (cursor_pointer_lo, x) sta (cursor_pointer_lo, x)
i16_inc(cursor_pointer_lo, cursor_pointer_hi) i16_inc(cursor_pointer_lo)
iny iny
jmp !loop- jmp !loop-
@ -151,11 +151,11 @@ print_hex_0x:
lda #'0' lda #'0'
sta (cursor_pointer_lo, x) sta (cursor_pointer_lo, x)
i16_inc(cursor_pointer_lo, cursor_pointer_hi) i16_inc(cursor_pointer_lo)
lda #'x' lda #'x'
sta (cursor_pointer_lo, x) sta (cursor_pointer_lo, x)
i16_inc(cursor_pointer_lo, cursor_pointer_hi) i16_inc(cursor_pointer_lo)
lda zp_temp lda zp_temp
@ -173,7 +173,7 @@ print_hex:
lda !hex_chars+, y lda !hex_chars+, y
sta (cursor_pointer_lo, x) sta (cursor_pointer_lo, x)
i16_inc(cursor_pointer_lo, cursor_pointer_hi) i16_inc(cursor_pointer_lo)
lda zp_temp lda zp_temp
and #$0f and #$0f
@ -181,7 +181,7 @@ print_hex:
lda !hex_chars+, y lda !hex_chars+, y
sta (cursor_pointer_lo, x) sta (cursor_pointer_lo, x)
i16_inc(cursor_pointer_lo, cursor_pointer_hi) i16_inc(cursor_pointer_lo)
rts rts
@ -205,7 +205,7 @@ print_decimal_u16:
lda $25 lda $25
sta $23 sta $23
i16_mul10($22, $23) i16_mul10($22)
i16_i16_sub($22, $23, $03, $04, $22, $23) i16_i16_sub($22, $23, $03, $04, $22, $23)