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
*=* "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:
move_16_imm($03, $04, str_title)
move_16_imm($03, str_title)
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
!line:
lda #0
sta $22
sta $23
// Create a temporary 16-bit integer to store the result in
// in between digits.
move_16_imm(atoi_result, 0)
!digit:
lda ($03), y
// Check for the newline character at the end of the line.
lda (input_pointer), y
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
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
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-
!break:
sty $24
!newline:
sty zp_temp
// Append the result of the conversion to the array.
ldy #0
lda $22
sta ($25), y
lda atoi_result + 0
sta (buffer_pointer), y
iny
lda $23
sta ($25), y
lda atoi_result + 1
sta (buffer_pointer), y
// Increment the buffer pointer by 2.
lda #2
i16_i8_add_a($25, $26)
i16_i8_add_a(buffer_pointer)
ldy $24
ldy zp_temp
iny
bne !+
inc $04
inc input_pointer + 1
!:
lda ($03), y
// Check for the null terminator.
lda (input_pointer), y
bne !line-
move_16_imm($25, $26, buffer)
move_16_imm($03, $04, str_parse_done)
move_16_imm($03, str_parse_done)
jsr write_string
move_16_imm(outer_pointer, buffer)
// Add every number to every other number
!outer:
move_16_imm($27, $28, buffer)
move_16_imm(inner_pointer, buffer)
// $03..$04 = buffer[i]
ldy #0
lda ($25), y
sta $03
lda (outer_pointer), y
sta outer_value
iny
lda ($25), y
sta $04
lda (outer_pointer), y
sta outer_value + 1
dey
lda ($25), y
lda (outer_pointer), y
bne !not_zero+
iny
lda ($25), y
lda (outer_pointer), y
bne !not_zero+
.break
jmp !error+
!not_zero:
// 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:
// $fd..$fe = buffer[j]
ldy #0
lda ($27), y
sta $fd
lda (inner_pointer), y
sta inner_value
iny
lda ($27), y
sta $fe
lda (inner_pointer), y
sta inner_value + 1
dey
lda ($27), y
lda (inner_pointer), y
bne !not_zero+
iny
lda ($27), y
lda (inner_pointer), y
bne !not_zero+
i16_inc($25, $26)
i16_inc(outer_pointer)
jmp !outer-
!not_zero:
!not_zero:
// j++
lda #2
i16_i8_add_a($27, $28)
i16_i16_cmp_bne($fb, $fc, $fd, $fe, !inner-)
i16_i8_add_a(inner_pointer)
// Continue the loop if the number didn't match the needed number.
i16_i16_cmp_bne(remainder, inner_value, !inner-)
!done:
// Multiply the results together
sec
jsr multiply_16bit_unsigned
move_16_imm($03, $04, str_calc_done)
move_16_imm($03, str_calc_done)
jsr write_string
// Print the 32-bit result as decimal.
lda $25
sta udivmod32_dividend + 3
lda $24
@ -130,7 +168,7 @@ day01:
rts
!error:
move_16_imm($03, $04, str_no_result)
move_16_imm($03, str_no_result)
jsr write_string
rts

View File

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

View File

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

View File

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