Skip to content

Commit

Permalink
finished implementing status bits and first rule + printing it
Browse files Browse the repository at this point in the history
  • Loading branch information
muellevin committed Oct 27, 2021
1 parent 60ea250 commit 470f336
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 24 deletions.
4 changes: 4 additions & 0 deletions src/constants_settings.asm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.data

.space 1048576 # Addressing for 512x512 Display addresses -> bug at 0x1040...
# it is disgusting but i guess the easiest solution for now
.eqv FPG_DISPLAY_ADDRESS 0xFF000000
.eqv FPG_DISPLAY_WIDTH 320
.eqv FPG_DISPLAY_HEIGHT 240
Expand Down Expand Up @@ -61,5 +62,8 @@ settings:

.word 50 # start density this is random so no UT #addresss by 28

state_bits:
.space 32768 # malloc for 512x512Pixel/32Bits of word x 4 addressing
# it is disgusting but i guess the easiest solution for now

.text
28 changes: 28 additions & 0 deletions src/exercise_solutions/draw_rectangle.asm
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,33 @@ draw_cell:
addi sp, sp, 8
ret

print_dead_cell:
# input
# a1 x cell pos
# a2 y cell pos
addi sp, sp, -4
sw ra, 0(sp)

li a7, 0 # dead cell
jal ra, draw_cell

lw ra, 0(sp)
addi sp, sp, 4
ret

print_living_cell:
# input
# a1 x cell pos
# a2 y cell pos
addi sp, sp, -4
sw ra, 0(sp)

li a7, ALIVE # dead cell
jal ra, draw_cell

lw ra, 0(sp)
addi sp, sp, 4
ret

.include "draw_pixel.asm"

72 changes: 69 additions & 3 deletions src/init_gamefield.asm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ecall


init_gamefield:
# sw ra, sp

addi sp, sp, -20
sw t0, 0(sp)
sw t1, 4(sp)
Expand All @@ -23,8 +23,6 @@ init_gamefield:
li t3, 0

init_living_cells:


# coords of cell
mv a1, t2
mv a2, t3
Expand Down Expand Up @@ -209,3 +207,71 @@ neighboor_status:
ret


# cropped from rules.asm next_generation
# -> looks even more weird from used registers
print_next_generation:
# saving all needed register those of rules as well
addi sp, sp, -24
sw t2, 0(sp)
sw t3, 4(sp)
sw s0, 8(sp)
sw s1, 12(sp)
sw s2, 16(sp)
sw ra, 20(sp)

la s0, state_bits # base adress for state bits
li s2, 31 # bits in one word +1 bit your bit; i changed the register at a later point
lw s1, 0(s0) # first value

li t2, 0
li t3, 0

print_through_gamefield:
# current cell:
mv a1, t2
mv a2, t3

get_status_bit:
srl a4, s1, s2 # getting cell status
andi a4, a4, 1 # only single bit is needed

beqz a4, continue_print_gamefiled

# i already have my cell coords::
jal ra, get_pixel
la ra, continue_print_gamefiled

bnez a3, print_dead_cell
j print_living_cell


continue_print_gamefiled:
addi s2, s2, -1
bgez s2, same_status_word_print

li s2, 31 # resetting bit count
addi s0, s0, 4 # going to next word adress
lw s1, 0(s0) # and loading the next word status bits

same_status_word_print:
# going to next cell
la a0, print_next_gemeration_finished # where to go if gamefield is finished
#current pos
mv a1, t2
mv a2, t3
jal ra, continue_gamefield
# load coords of next cell
mv t2, a1
mv t3, a2
j print_through_gamefield


print_next_gemeration_finished:
lw t2, 0(sp)
lw t3, 4(sp)
lw s0, 8(sp)
lw s1, 12(sp)
lw s2, 16(sp)
lw ra, 20(sp)
addi sp, sp, 24
ret
1 change: 1 addition & 0 deletions src/main.asm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

.include "constants_settings.asm"
# init

jal ra, init_user_questioning
jal ra, init_gamefield

Expand Down
113 changes: 92 additions & 21 deletions src/rules.asm
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@


next_generation:

# saving all needed register those of rules as well
addi sp, sp, -32
sw t0, 0(sp)
Expand All @@ -12,21 +11,14 @@ next_generation:
sw s1, 20(sp)
sw s2, 24(sp)
sw ra, 28(sp)

la t0, settings
lw t1, 20(t0) # rule
li s0, 1 # first rule

beq s0, t1, rule_1
la s0, state_bits # base adress for state bits
li s2, 31 # bits in one word +1 bit your bit; i changed the register at a later point
li s1, 0 # first value


rule_1:

lw t1, 28(t0) #density
lw s0, 12(t0) # widht size
lw s1, 16(t0) # height size
lw s2, 0(t0) # cell size

la t0, settings
lw t1, 20(t0)
li t0, 1 # min rule == 1

li t2, 0
li t3, 0
Expand All @@ -35,19 +27,39 @@ next_generation:
# current cell:
mv a1, t2
mv a2, t3
jal neighboor_status
mv a0, a4 # print status ehre to safe?
li a7, 1
ecall
jal neighboor_status # return with a4 count living neighboors
# current cell; incase i have done something stupid in neighboor_status
mv a1, t2
mv a2, t3

# now the magic begins
bne t0, t1, check_rule_2
jal ra, rule_1 # returning status bit -->1 -> change status (dead -> alive; alive ->dead)
j set_status_bit

check_rule_2:

# well shouldn't be possible
## switch rule call a4 -> next status of current cell is a4 as well


set_status_bit:
sll a4, a4, s2
add s1, s1, a4
addi s2, s2, -1
bgez s2, same_status_word

sw s1, 0(s0) # saving the status bits
li s2, 31 # resetting bit count
li s1, 0 # resetting saving word
addi s0, s0, 4 # going to next word adress

same_status_word:
# going to next cell
la a0, finished_rule # where to go if gamefield is finished
#current pos
mv a1, t2
mv a2, t3
mv a3, s2 # cell size
mv a4, s0 # max widht
mv a5, s1 # max height
jal ra, continue_gamefield
# load coords of next cell
mv t2, a1
Expand All @@ -56,6 +68,10 @@ next_generation:


finished_rule:
sw s1, 0(s0) # saving the status bits; incase of cells not divideable to 32
# now the fun begins:: print the next generation
jal ra, print_next_generation

lw t0, 0(sp)
lw t1, 4(sp)
lw t2, 8(sp)
Expand All @@ -66,3 +82,58 @@ next_generation:
lw ra, 28(sp)
addi sp, sp, 32
ret


rule_1:
# a1 x pos of cell
# a2 y pos of cell
# input a4 with number of living neighboors
# return a4 if status bit is set -> change cell status dead/alive

addi sp, sp, -8
sw t0, 0(sp)
sw ra, 4(sp)

li t0, 2
bne a4, t0, rule_1_3
# well this means no chances
# can not relieve dead cells
# alive cells can not be dead
jal ra, nochange_bit # ra is not set to finish yet
j back_rule_1

rule_1_3:
jal ra, get_pixel # return color code in a3 -> status of cell
# i need the status for the other rules so i am calling it now

# after that ra need to be the finished rule so...
la ra, back_rule_1

li t0, 3
bne a4, t0, dead_rule_1

# now i need the current status of the cell
bnez a3, nochange_bit
# this means the cell rebirth
j change_bit

dead_rule_1: # not all can survive...now die or stay dead

bnez a3, change_bit # cell dies
# well at this point the cell is dead and stays dead
j nochange_bit

back_rule_1:
lw t0, 0(sp)
lw ra, 4(sp)
addi sp, sp, 8
ret


change_bit:
li a4, 1
ret

nochange_bit:
li a4, 0
ret

0 comments on commit 470f336

Please sign in to comment.