mirror of
https://github.com/Asraelite/littlebigcomputer.git
synced 2025-07-18 00:26:50 +00:00
Add compiler
This commit is contained in:
parent
476972f85a
commit
3f3125ef43
31 changed files with 2625 additions and 3 deletions
61
programs/parva_0.2/cat.parvalang
Normal file
61
programs/parva_0.2/cat.parvalang
Normal 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"
|
3
programs/parva_0.2/file.parva
Normal file
3
programs/parva_0.2/file.parva
Normal file
|
@ -0,0 +1,3 @@
|
|||
# file
|
||||
|
||||
|
87
programs/parva_0.2/simd_ideas.parva
Normal file
87
programs/parva_0.2/simd_ideas.parva
Normal 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
|
||||
|
45
programs/parva_0.2/split_args
Normal file
45
programs/parva_0.2/split_args
Normal 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
|
||||
|
189
programs/parva_0.2/tetris.parva
Normal file
189
programs/parva_0.2/tetris.parva
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue