add 2019 day 1

This commit is contained in:
Zack Buhman 2024-04-07 14:58:48 +08:00
parent bfbd6830a8
commit 3a933db011
9 changed files with 314 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
*.elf
*.o
*.bin
*.d

100
2019/day1/input.txt Normal file
View File

@ -0,0 +1,100 @@
98578
105016
93022
144768
80394
112379
121119
94660
126363
112893
102603
93967
77268
103649
70132
142499
143711
140554
104725
84738
70613
108746
111488
89944
67984
59613
80035
69350
134001
62115
104688
143033
109712
96194
90353
96899
131267
143909
96649
138803
140620
73931
118851
58910
92205
132615
83308
73807
146895
142622
56234
126672
79278
111589
57593
80856
76261
89204
110871
74731
68654
103148
89935
58596
89510
101248
86137
56176
78679
128987
73114
143844
69805
54820
99223
119668
79449
98890
64512
104946
126345
128346
112212
135582
108214
111077
75745
125934
52956
102036
108452
129232
97091
106975
92156
145892
66680
88452
75081
102811

124
2019/day1/part1_part2.s Normal file
View File

@ -0,0 +1,124 @@
.global _start
_start:
/* part1 */
mova _part_fuel,r0
mov r0,r12
mova _accumulate,r0
jsr @r0
nop
mov r10,r11 /* r11: part1 answer */
/* part2 */
mova _part2_fuel,r0
mov r0,r12
mova _accumulate,r0
jsr @r0
nop /* r10: part2 answer */
forever:
bra forever
nop
/* r12: fuel function */
.align 4
_accumulate:
mov.l start,r8
mov.l end,r9
mov #0,r10 /* r10: sum */
sts pr,r15 /* save PR */
accumulator_loop:
cmp/hs r9,r8 /* Rn Rm (unsigned) */
bt accumulator_rts
mova _parse_int,r0
jsr @r0
nop
jsr @r12
nop
bra accumulator_loop
add r1,r10
accumulator_rts:
lds r15,pr /* restore PR */
rts
nop
.align 4
start: .long _binary_2019_day1_input_txt_start
end: .long _binary_2019_day1_input_txt_end
.global _parse_int
.align 4
_parse_int:
/* r0 = pointer to text ; r1 = number */
mov #0,r1 /* r1: accumulated number */
parse_int_loop:
mov.b @r8+,r2
add #-48,r2 /* ascii '0' */
cmp/pz r2 /* Rn >= 0 (signed) (r2 >= 0 ) */
bf/s parse_int_rts
mov #9,r4 /* const 9 */
cmp/ge r2,r4 /* Rn >= Rm (signed) ( 9 >= r2) */
bf/s parse_int_rts
mov #10,r4 /* const 10 */
mulu.w r1,r4
sts macl,r1
bra parse_int_loop
add r2,r1
parse_int_rts:
rts
nop
.global _part2_fuel
.align 4
_part2_fuel:
/* r1 = mass */
sts pr,r13 /* save PR */
mov #0,r5 /* r5: fuel sum */
part2_fuel_for_fuel:
mova _part_fuel,r0
jsr @r0
nop
cmp/pl r1 /* signed Rn > 0 */
bf part2_rts
bra part2_fuel_for_fuel
add r1,r5
part2_rts:
lds r13,pr /* restore PR */
rts
mov r5,r1
.global _part_fuel
.align 4
_part_fuel:
/* r1 = mass */
sts pr,r14 /* save PR */
mova _div32_16,r0
jsr @r0
mov #3,r0
lds r14,pr /* restore PR */
rts
add #-2,r1
.global _div32_16
.align 4
_div32_16:
/* r1 (32 bits) ÷ r0 (16 bits) */
shll16 r0
cmp/hs r0,r1
bt over_div
div0u
.rept 16
div1 r0,r1
.endr
rotcl r1
rts
extu.w r1,r1
over_div:
bra over_div
nop
/* r1 = quotient */

4
2019/day1/sample.txt Normal file
View File

@ -0,0 +1,4 @@
12
14
1969
100756

2
2019/deps.mk Normal file
View File

@ -0,0 +1,2 @@
2019/day1/part1.elf: 2019/day1/part1_part2.o 2019/day1/sample.txt.o 2019/day1/input.txt.o
$(DEFAULT_LINK)

View File

@ -6,8 +6,10 @@ _start:
parse_lines:
_pointers:
/* mova sample_input_start,r0 */
mova input_start,r0
mov r0,r14 /* r14: pointer to input start */
/* mova sample_input_end,r0 */
mov.l input_end,r0
mov r0,r15 /* r15: pointer to input end */

77
Makefile Normal file
View File

@ -0,0 +1,77 @@
TARGET ?= sh4-unknown-linux-gnu-
AS=$(TARGET)as
LD=$(TARGET)ld
CC=$(TARGET)gcc
OBJCOPY=$(TARGET)objcopy
ENDIAN ?= little
ifeq ($(ENDIAN),little)
AS_ENDIAN = --little
LD_ENDIAN = -EL
CC_ENDIAN = -ml
OBJ_ENDIAN = -O elf32-sh-linux
else
AS_ENDIAN = --big
LD_ENDIAN = -EB
CC_ENDIAN = -mb
OBJ_ENDIAN = -O elf32-sh
endif
OPT = -Og
DEBUG = -g -gdwarf-4
AARCH = --isa=sh4
CARCH = -m4-single-only
OBJARCH = -B sh4
CFLAGS += -Wall -Werror -Wfatal-errors
CFLAGS += -falign-functions=4 -ffunction-sections -fdata-sections -fshort-enums
CFLAGS += -ffreestanding -nostdlib
DEPFLAGS = -MMD -MP
LDFLAGS += --no-warn-execstack
%.o: %.c
$(CC) $(CARCH) $(CC_ENDIAN) $(CFLAGS) $(OPT) $(DEBUG) $(DEPFLAGS) -MF ${<}.d -c $< -o $@
%.o: %.s
$(AS) $(AARCH) $(AS_ENDIAN) $(DEBUG) $< -o $@
define DEFAULT_LINK
$(LD) $(LD_ENDIAN) $(LDFLAGS) \
--section-start=.text=0x0 \
--entry=_start $^ -o $@
endef
define BUILD_BINARY_O
$(OBJCOPY) \
-I binary $(OBJARCH) $(OBJ_ENDIAN) \
$< $@
endef
%.txt.o: %.txt
$(BUILD_BINARY_O)
%.elf: %.o
$(DEFAULT_LINK)
%.bin: %.elf
$(OBJCOPY) -O binary --remove-section=.stack $< $@
clean:
find -P \
-regextype posix-egrep \
-regex '.*\.(o|bin|elf|d)$$' \
-exec rm {} \;
.SUFFIXES:
.INTERMEDIATE:
.SECONDARY:
.PHONY: all clean
%: RCS/%,v
%: RCS/%
%: %,v
%: s.%
%: SCCS/s.%
MAKEFLAGS += --no-builtin-rules
include 2019/deps.mk

View File

@ -1,5 +1,6 @@
target extended-remote localhost:1234
tui enable
tui layout src
tui layout regs
tui focus cmd
tui reg all