From 7f72298c9919b5141d3e1297e2327ef6305b1a46 Mon Sep 17 00:00:00 2001 From: Sijmen Schoon Date: Wed, 9 Dec 2020 02:08:57 +0100 Subject: [PATCH] Make day 1 work --- c64/day01.asm | 127 ++++++++++++++++++++++++++++++++++++++++--------- c64/main.asm | 17 ++----- c64/math.asm | 2 + c64/screen.asm | 3 ++ 4 files changed, 115 insertions(+), 34 deletions(-) diff --git a/c64/day01.asm b/c64/day01.asm index f9d33be..d5c7b46 100644 --- a/c64/day01.asm +++ b/c64/day01.asm @@ -1,16 +1,12 @@ // vim: filetype=kickass +*=* "Day 01" day01: - move_16_imm($03, $04, str_input) - jsr write_string - - move_16_imm($03, $04, day01_input) - jsr write_string - - move_16_imm($03, $04, str_converted) + move_16_imm($03, $04, str_title) jsr write_string move_16_imm($03, $04, day01_input) + move_16_imm($25, $26, buffer) ldy #0 !line: @@ -26,45 +22,132 @@ day01: i16_mul10($22, $23) lda ($03), y - sec sbc #'0' i16_i8_add_a($22, $23) iny + bne !digit- + + inc $04 jmp !digit- !break: sty $24 - lda $23 - jsr print_hex_0x + ldy #0 lda $22 - jsr print_hex + sta ($25), y + iny + lda $23 + sta ($25), y - jsr print_newline - jsr print_carriage_return + lda #2 + i16_i8_add_a($25, $26) ldy $24 iny - + bne !+ + inc $04 +!: lda ($03), y bne !line- + move_16_imm($25, $26, buffer) + + move_16_imm($03, $04, str_parse_done) + jsr write_string + + // Add every number to every other number +!outer: + move_16_imm($27, $28, buffer) + + // $03..$04 = buffer[i] + ldy #0 + lda ($25), y + sta $03 + iny + lda ($25), y + sta $04 + + dey + lda ($25), y + bne !not_zero+ + iny + lda ($25), 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) + +!inner: + // $fd..$fe = buffer[j] + ldy #0 + lda ($27), y + sta $fd + iny + lda ($27), y + sta $fe + + dey + lda ($27), y + bne !not_zero+ + iny + lda ($27), y + bne !not_zero+ + + i16_inc($25, $26) + jmp !outer- +!not_zero: + + // j++ + lda #2 + i16_i8_add_a($27, $28) + i16_i16_cmp_bne($fb, $fc, $fd, $fe, !inner-) + !done: + // Multiply the results together + sec + jsr multiply_16bit_unsigned + + move_16_imm($03, $04, str_calc_done) + jsr write_string + + lda $25 + sta udivmod32_dividend + 3 + lda $24 + sta udivmod32_dividend + 2 + lda $23 + sta udivmod32_dividend + 1 + lda $22 + sta udivmod32_dividend + 0 + jsr print_decimal_u32 + + rts + +!error: + move_16_imm($03, $04, str_no_result) + jsr write_string rts -day01_input: - .import text "../rust/inputs/day01_example" - .byte 0 - -str_input: - .text "# Input" +str_title: + .text "# Day 01, part 1:" .byte '\n', 0 -str_converted: +str_parse_done: + .text "* Finished parsing" + .byte '\n', 0 + +str_calc_done: + .text "* Finished calculating" + .byte '\n', 0 + +str_no_result: .byte '\n' - .text "# Converted" + .text "! Could not find a result" .byte '\n', 0 diff --git a/c64/main.asm b/c64/main.asm index 6873104..aca4b8c 100644 --- a/c64/main.asm +++ b/c64/main.asm @@ -4,6 +4,7 @@ BasicUpstart2(main) +* = $8000 .align $100 * = * "Square table" square1_lo: @@ -15,16 +16,17 @@ square2_lo: square2_hi: .fill 512, 0 -* = * "Day 1 input" +* = * "Day 01 Input" day01_input: .import text "../rust/inputs/day01" .byte 0 +* = * "Day 01 Input End" //* = $8000 "Cartridge ROM" //* = $a000 "Basic ROM" -* = $c000 +* = $c000 "Main program" .const SCREEN_RAM = $0400 .const COLOR_RAM = $d800 @@ -56,17 +58,8 @@ main: jsr generate_multiplication_tables - // Test division by 10 - lda #$cc - sta udivmod32_dividend + 0 - lda #$28 - sta udivmod32_dividend + 1 - lda #$f9 - sta udivmod32_dividend + 2 - lda #$07 - sta udivmod32_dividend + 3 + jsr day01 - jsr print_decimal_u32 !loop: jmp !loop- diff --git a/c64/math.asm b/c64/math.asm index e4b6be8..bf8ecaf 100644 --- a/c64/math.asm +++ b/c64/math.asm @@ -1,5 +1,7 @@ // vim: filetype=kickass +*=* "Math Routines" + .const T1=$03 .const T2=$fd .const PRODUCT=$22 diff --git a/c64/screen.asm b/c64/screen.asm index d668586..eea294e 100644 --- a/c64/screen.asm +++ b/c64/screen.asm @@ -1,5 +1,7 @@ // vim: filetype=kickass +*=* "Screen Routines" + // // clear_screen // @@ -141,6 +143,7 @@ write_string: jsr print_hex } +// Destroys: a, x, y, $03 print_hex_0x: sta zp_temp