📄 list_ch16_03_uart_rom.psm
字号:
load s7, 20 ;set mask to 0010_0000
tx_loop:
;loop body
load tx_data, ASCII_1 ;load default ASCII 1
test s7, s4 ;check the bit
jump nz, tx_01 ;the bit is 1
load tx_data, ASCII_0; ;the bit is 0, load ASCII 0
tx_01:
call tx_one_byte ;transmit the ASCII 1 or 0
;update loop index and mask
sr0 s7 ;shift mask bit
sub i, 01 ;dec loop index
jump nz, tx_loop ;loop not reach 0 yet
;done with loop, send ASCII space
load tx_data, ASCII_SP ;load ASCII SP
call tx_one_byte ;transmit SP
return
;=========================================================
;routine: disp_ram_data
; function: 8-byte data in form of
; 00 11 22 33 44 55 66 77 88
; input register:
; s4: ram base address (xxx000)
; tmp register: i, addr, data
;=========================================================
disp_ram_data:
;initialize the loop index and mask
load i, 08 ;addr used as loop index
d_ram_loop:
;loop body
load addr, s4
add addr, i
sub addr, 01 ;calculate addr offset
;send upper nibble
fetch data, (addr)
call get_upper_nibble
call hex_to_ascii ;convert to ascii
load tx_data, data
call tx_one_byte
;send lower nibble
fetch data, (addr)
call get_lower_nibble
call hex_to_ascii ;convert to ascii
load tx_data, data
call tx_one_byte
;send a space
load tx_data, ASCII_SP;
call tx_one_byte ;transmit SP
sub i, 01 ;dec loop index
jump nz, d_ram_loop ;loop not reach 0 yet
return
;=========================================================
;routine: hex_to_ascii
; function: convert a hex number to ascii code
; add 30 for 0-9, add 37 for A-F
; input register: data
;=========================================================
hex_to_ascii:
compare data, 0a
jump c, add_30 ;0 to 9, offset 30
add data, 07 ;a to f, extra offset 07
add_30:
add data, 30
return
;=========================================================
;routine: tx_one_byte
; function: wait until uart tx fifo not full;
; then write a byte to fifo
; input register: tx_data
; tmp register:
; s6: read port flag
;=========================================================
tx_one_byte:
input s6, rd_flag_port
test s6, 08 ;check uart_tx_full
jump nz, tx_one_byte ;yes, keep on waiting
output tx_data, uart_tx_port ;no, write to uart tx fifo
return
;=========================================================
;routine: load_led_pttn
; function: read 3 LSBs of switch input and convert the
; desired values to four led patterns and
; load them to ram
; switch: 000:a; 001:b; 010:a^2; 011:b^2;
; others: a^2 + b^2
; tmp register used: data, addr
; s6: data from sw input port
;=========================================================
load_led_pttn:
input s6, sw_port ;get switch
sl0 s6 ;*2 to obtain addr offset
compare s6, 08 ;sw>100?
jump c, sw_ok ;no
load s6, 08 ;yes, sw error, make default
sw_ok:
;process byte 0, lower nibble
load addr, a_lsb
add addr, s6 ;get lower addr
fetch data, (s6) ;get lower byte
call get_lower_nibble ;get lower nibble
call hex_to_led ;convert to led pattern
store data, led0
;process byte 0, upper nibble
fetch data, (addr)
call get_upper_nibble
call hex_to_led
store data, led1
;process byte 1, lower nibble
add addr, 01 ;get upper addr
fetch data, (addr)
call get_lower_nibble
call hex_to_led
store data, led2
;process byte 1, upper nibble
fetch data, (addr)
call get_upper_nibble
call hex_to_led
;check for sw=100 to process carry as led dp
compare s6, 08 ;display final result?
jump nz, led_done ;no
add addr, 01 ;get carry addr
fetch s6, (addr) ;s6 to store carry
test s6, 01 ;carry=1?
jump z, led_done ;no
and data, 7F ;yes, assert msb (dp) to 0
led_done:
store data, led3
return
;=========================================================
;routine: disp_led
; function: output four led patterns
; tmp register used: data
;=========================================================
disp_led:
fetch data, led0
output data, sseg0_port
fetch data, led1
output data, sseg1_port
fetch data, led2
output data, sseg2_port
fetch data, led3
output data, sseg3_port
return
;=========================================================
;routine: hex_to_led
; function: convert a hex digit to 7-seg led pattern
; input register: data
; output register: data
;=========================================================
hex_to_led:
compare data, 00
jump nz, comp_hex_1
load data, 81 ;7seg pattern 0
jump hex_done
comp_hex_1:
compare data, 01
jump nz, comp_hex_2
load data, CF ;7seg pattern 1
jump hex_done
comp_hex_2:
compare data, 02
jump nz, comp_hex_3
load data, 92 ;7seg pattern 2
jump hex_done
comp_hex_3:
compare data, 03
jump nz, comp_hex_4
load data, 86 ;7seg pattern 3
jump hex_done
comp_hex_4:
compare data, 04
jump nz, comp_hex_5
load data, CC ;7seg pattern 4
jump hex_done
comp_hex_5:
compare data, 05
jump nz, comp_hex_6
load data, A4 ;7seg pattern 5
jump hex_done
comp_hex_6:
compare data, 06
jump nz, comp_hex_7
load data, A0 ;7seg pattern 6
jump hex_done
comp_hex_7:
compare data, 07
jump nz, comp_hex_8
load data, 8F ;7seg pattern 7
jump hex_done
comp_hex_8:
compare data, 08
jump nz, comp_hex_9
load data, 80 ;7seg pattern 8
jump hex_done
comp_hex_9:
compare data, 09
jump nz, comp_hex_a
load data, 84 ;7seg pattern 9
jump hex_done
comp_hex_a:
compare data, 0A
jump nz, comp_hex_b
load data, 88 ;7seg pattern a
jump hex_done
comp_hex_b:
compare data, 0B
jump nz, comp_hex_c
load data, E0 ;7seg pattern b
jump hex_done
comp_hex_c:
compare data, 0C
jump nz, comp_hex_d
load data, B1 ;7seg pattern C
jump hex_done
comp_hex_d:
compare data, 0D
jump nz, comp_hex_e
load data, C2 ;7seg pattern d
jump hex_done
comp_hex_e:
compare data, 0E
jump nz, comp_hex_f
load data, B0 ;7seg pattern E
jump hex_done
comp_hex_f:
load data, B8 ;7seg pattern f
hex_done:
return
;=========================================================
;routine: get_lower_nibble
; function: get lower 4-bit of data
; input register: data
; output register: data
;=========================================================
get_lower_nibble:
and data, 0F ;clear upper nibble
return
;=========================================================
;routine: get_lupper_nible
; function: get upper 4-bit of in_data
; input register: data
; output register: data
;=========================================================
get_upper_nibble:
sr0 data ;right shift 4 times
sr0 data
sr0 data
sr0 data
return
;=========================================================
;routine: square
; function: calculate a*a + b*b
; data/result stored in ram started w/ SQ_BASE_ADDR
; temp register: s3, s4, s5, s6, data
;=========================================================
square:
;calculate a*a
fetch s3, a_lsb ;load a
fetch s4, a_lsb ;load a
call mult_hard ;calculate a*a
store s6, aa_lsb ;store lower byte of a*a
store s5, aa_msb ;store upper byte of a*a
;calculate b*b
fetch s3, b_lsb ;load b
fetch s4, b_lsb ;load b
call mult_hard ;calculate b*b
store s6, bb_lsb ;store lower byte of b*b
store s5, bb_msb ;store upper byte of b*b
;calculate a*a+b*b
fetch data, aa_lsb ;get lower byte of a*a
add data, s6 ;add lower byte of a*a+b*b
store data, aabb_lsb ;store lower byte of a*a+b*b
fetch data, aa_msb ;get upper byte of a*a
addcy data, s5 ;add upper byte of a*a+b*b
store data, aabb_msb ;store upper byte of a*a+b*b
load data, 00 ;clear data, but keep carry
addcy data, 00 ;get carry from previous +
store data, aabb_cout ;store carry of a*a+b*b
return
;=========================================================
;routine: mult_hard
; function: 8-bit unsigned multiplication using
; external combinational multilplier;
; input register:
; s3: multiplicand
; s4: multiplier
; output register:
; s5: upper byte of product
; s6: lower byte of product
; temp register:
;=========================================================
mult_hard:
output s3, mult_src0_port
output s4, mult_src1_port
input s5, mult_prod1_port
input s6, mult_prod0_port
return
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -