Add compiler

This commit is contained in:
Asraelite 2024-05-17 18:37:51 +02:00
parent 476972f85a
commit 3f3125ef43
31 changed files with 2625 additions and 3 deletions

View file

@ -0,0 +1,184 @@
$0:
hlt
ret
rti
eni
dsi
nop
rem x
rem y
rem z
clr
$1:
jmp 5
jmp 5, x
jmp 5, y
jmp 5, z
jsr 5
jsr 5
jsr 5, x
jsr 5, y
jsr 5, z
swp x, y
swp y,x
swp x,z
swp z,x
swp z,y
swp y,z
$2:
jmpez x, 5
jmpez z, 5
jmpgt x,y, 5
jmpeq x,y, 5
jsrez x, 5
jsrez z, 5
jsrgt x, y, 5
jsreq x, y, 5
lod y, x
lod z, x
lod y, x
lod y, z
lod x, z
lod y, z
$3:
inc x
inc y
inc z
inc 5
dec x
dec y
dec z
dec 5
sub x, #5
sub y, #5
sub z, #5
div x, #5
div y, #5
div z, #5
$4:
add x, #5
sub x, #5
mul x, #5
div x, #5
add x, y
add x, z
sub x, y
sub x,z
mul x,y
mul x,z
div x,y
div x,z
mod x,y
mod x,z
$5:
add y, #5
sub y, #5
mul y, #5
div y, #5
add y, z
sub y,x
sub y,z
mul y,z
div y,x
div y,z
mod y,x
mod y,z
$6:
add z, #5
sub z,#5
mul z,#5
div z,#5
sub z,x
sub z,y
div z,x
div z,y
mod z,x
mod z,y
$7:
lsl x
lsl y
lsl z
lsr x
lsr y
lsr z
not x
not y
not z
lod x, #5
lod y, #5
lod z, #5
$8:
and x,y
and x,z
and y,z
xor x,z
xor x,y
xor y,z
or x,y
or x,z
or y,z
$9:
and x, #5
and y, #5
and z, #5
or x, #5
or y, #5
or z, #5
xor x, #5
xor y, #5
xor z, #5
mod x, #5
mod y, #5
mod z, #5
$a:
lod x, 5
lod x, 5, y
lod x, 5, z
$b:
lod y, 5
lod y, 5, x
lod y, 5, z
$c:
lod z, 5
lod z, 5, x
lod z, 5, y
lod z, 5, yx
$d:
str x, 5
str x, 5, y
str x, 5, z
str #5, 3, x
str #5, 3, y
str #5, 3, z
str #5, 3
$e:
str y, 5
str y, 5, x
str y, 5, z
$f:
str z, 5
str z, 5, x
str z, 5, y
str z, 5, yx
str #5, 3, yx

View file

@ -0,0 +1,61 @@
# cat
.extern f_open, f_close, f_read, write
.string_encoding system
main: # fn (args: [cstr8]) -> int
let argv = %a0;
let argc = %a1;
# mv s0, a0
# push a1
let arg_index = 0;
arg_loop:
beq arg_index, argc, end;
let filename = lw [argv + arg_index];
%a0 = filename;
call f_open
let fd = %a0;
bltz fd, error_open;
%a0 = fd;
call f_read;
let file_contents = %a0;
arg_loop:
lw t0, 1(sp)
beq s1, t0, end
lw a0, 0(s0)
call f_open # a0 = file descriptor
bltz a0, error_open
push a0
call f_read # a0 = pointer to contents
bltz a0, error_read
mv a1, a0
li a0, 1 # stdout
call write
pop a0
call f_close
addi s1, s1, 1
b arg_loop
error_read:
pop zero
mv s0, a1
call f_close
mv a1, s0
error_open:
mv s0, a1
li a0, 1
li a1, error_message
call write
mv a1, s0
call write
b end
end:
li a0, 0
ret
error_message:
.string "\fr" # color red
.string "Error: \0"

View file

@ -0,0 +1,3 @@
# file

View file

@ -0,0 +1,87 @@
# x8: pointer to current char in input string
# I think supporting semi-fast unaligned reads is cheaper than having this alignment logic.
# andi x0, x8, 0b11
# li x1, 4
# sub x0, x1, x0
# add x0, x0, x0
# addi x0, x0, 1
# b x0
# lw x0, [x8 + 0]
# beq x0, x2, (split + 3)
# lw x0, [x8 + 1]
# beq x0, x2, (split + 2)
# lw x0, [x8 + 1]
# beq x0, x2, (split + 1)
# Frame 1
loop_0:
sw x8, [x9]
addi x9, x9, 1
li x2, ' '
lq x4:7, x8
b loop_1_no_align
loop_1:
andi x8, x8, 0b11
# Frame 2
loop_1_no_align:
vindexof.6 x0, x1, x4
bgez x0, split
addi x8, x8, 1
vindexof.6 x0, x1, x5
bgez x0, split
addi x8, x8, 1
vindexof.6 x0, x1, x6
bgez x0, split
addi x8, x8, 1
vindexof.6 x0, x1, x7
bgez x0, split
addi x8, x8, 1
b loop_1
# Frame 8
split:
slli x1, x0, 2
add x1, x1, x0
add x1, x1, x0
sll x1, x2, x1
lw x0, [x8]
xor x0, x0, x1
addi x8, x8, 1
# etc.
# Frame 15
hash:
li x0, 5381
lw x2, [x3]
loop_2:
slli x1, x0, 5
add x0, x0, x1
add x0, x0, x1
addi x3, x3, 1
lw x2, [x3]
andi x1, x2, 0b111111
bnez x1, loop_2

View file

@ -0,0 +1,45 @@
let input_string: *word;
let space: char;
li space, ' ';
let parts: quad;
lq parts, input_string;
b loop_1_no_align
loop_1:
andi input_string, input_string, 0b11
# Frame 2
loop_1_no_align:
vindexof.6 x0, x1, x4
bgez x0, split
addi x8, x8, 1
vindexof.6 x0, x1, x5
bgez x0, split
addi x8, x8, 1
vindexof.6 x0, x1, x6
bgez x0, split
addi x8, x8, 1
vindexof.6 x0, x1, x7
bgez x0, split
addi x8, x8, 1
b loop_1
# Frame 8
split:
@mul char_index, 6
sll x1, x2, x1
lw x0, [x8]
xor x0, x0, x1
addi x8, x8, 1

View file

@ -0,0 +1,189 @@
.arch parva_0_1
# 1(sp): score
# 2(sp): swap_block
# x4: block lines 0/1
# x5: block lines 2/3
# x6: field lines 0/1
# x7: field lines 2/3
.include consts
.include sys
.string_encoding sys-6
.data.ro
srs_kick_table:
# XY, 0 = 0, 1 = +1, 2 = +2, 7 = -1, 6 = -2
# I block
0o60_10_67_12
0o70_20_72_27
# TODO
# J, L, T, S, Z blocks
0o70_71_06_76
0o10_17_02_12
# TODO
block_table:
# I 0
0b_0_0000_000000_0_0_1111_000000_0
0b_0_1111_000000_0_0_0000_000000_0
# I 1
0b_0_0010_000000_0_0_0010_000000_0
0b_0_0010_000000_0_0_0010_000000_0
# TODO
hi_score_filename: .string "tet_hiscr.dat\0"
hi_score_file_error_message: .string .line_break 10 "could not open or create hi score file\0"
score_string: .string "score\0"
game_over_string: .string "game over\0"
.scores_button_string: .string "scores\0"
.start_button_string: .string "start\0"
.next_string: .string "next\0"
.data.rw
.align const.cache_line_size
swap_block: 0
score: 0
next_air_drop_time: 0
next_floor_drop_time: 0
stack: .repeat 0, 20
hi_scores:
.repeat 0, 20
.eq score_string_x 30
.eq score_string_y 10
.text
_start:
push ra
load_hi_scores:
li a0, hi_score_filename
li a1, hi_scores
li x0, 20
call sys.read_file
li x0, -1
beq a0, x0, create_hi_score_file
create_hi_score_file:
li a0, hi_score_filename
call sys.open_file
bltz a0, error_hi_score_file
wait:
wait_loop:
stub.kb.rdchar x0
beqi x0, const.char.left_arrow, move_left
beqi x0, const.char.right_arrow, move_right
beqi x0, const.char.up_arrow, rotate
beqi x0, const.char.space, hard_drop
beqi x0, const.char.backspace, swap
b wait_loop
rotate:
# x2 = current pos / block / rotation, [0 * 8, 4 bits for position x, 0 * 7, 3 bits for block type, 2 bits for rotation]
addi x3, x2, 1
andi x3, x3, 0b00011
andi x2, x2, 0b11100
or x2, x2, x3
ld d0, [x2 + block_table]
.align 4
move_left:
slli x0, x4, 1
and x0, x0, x6
bnez x0, fail
slli x0, x5, 1
and x0, x0, x7
bnez x0, fail
slli x4, x4, 1
slli x5, x5, 1
b move_successful
.align 4
move_right:
srli x0, x4, 1
and x0, x0, x6
bnez x0, fail
srli x0, x5, 1
and x0, x0, x7
bnez x0, fail
srli x4, x4, 1
srli x5, x5, 1
b move_successful
move_failed:
# TODO
move_successful:
# TODO
# decimal addition
# input: x0 = score change
.align 2
add_score:
lw x1, score
add x0, x0, x1
li x1, 0o1166
and x2, x0, 0o77
li x3, 10
blt x2, x3, 2
add x0, x0, x1
slli x1, x1, 6
srli x2, x0, 6
andi x2, x0, 0o77
blt x2, x3, end_score_change
add x0, x0, x1
slli x1, x1, 6
srli x2, x0, 6
andi x2, x0, 0o77
blt x2, x3, end_score_change
add x0, x0, x1
slli x1, x1, 6
srli x2 x0, 6
andi x2, x0, 0o77
blt x2, x3, end_score_change
add x0, x0, x1
.align 2
end_score_change:
sw x0, score
quit:
pop ra