How to reverse my stopwatch code to count down in Assembler?

雨燕双飞 提交于 2019-12-11 19:41:12

问题


I have written this code for stopwatch in Assembler. If you press button1, it will start counting up. If you press the same button again, it will pause, pressing it once more will resume the stopwatch. Pressing the button number 2 resets the stopwatch.

It is quite some time, since I was programming in PicoBlaze Assembler and this is a few years old code and since I did not program for a few years, I have forgotten a lot. But now I need to slightly alter it and I need your help.

So what I want now is to have the function of counting down. So when I press button1, it will start counting up, pressing it once more will pause it and once more to resume. But pressing the button2 will start counting it down, same with pausing and resuming with the same button and to move the reset from button2 to a new button3.

Can anybody help me with this? Thanks.

VHDL    "ROM_blank.vhd", "ProgMem.vhd", "ProgMem"
BUTTON      DSIN        $00
SWITCH      DSIN        $01
LED         DSOUT       $02
SEG0        DSOUT       $03
SEG1        DSOUT       $04
SEG2        DSOUT       $05
SEG3        DSOUT       $06

wait0 equ s0
wait1 equ s1
wait2 equ s2
cnt0 equ s3
cnt1 equ s4
cnt2 equ s5
cnt3 equ s6
state equ s7
segsel equ s8
temp equ sf

state_r0 equ 0
state_r1 equ 1
state_s0 equ 2
state_s1 equ 3

decoded_dp equ 254
decoded_0 equ 3
decoded_1 equ 159
decoded_2 equ 37
decoded_3 equ 13
decoded_4 equ 153
decoded_5 equ 73
decoded_6 equ 65
decoded_7 equ 27
decoded_8 equ 1
decoded_9 equ 25
decoded_A equ 5
decoded_B equ 193
decoded_C equ 229
decoded_D equ 133
decoded_E equ 97
decoded_F equ 113


init:
     load cnt0, 0
     load cnt1, 0
     load cnt2, 0
     load cnt3, 0
     load state, state_s0
start:

; read buttons
    in temp, BUTTON
; reset check
    and temp, 2
    jump z, startstop_check
    load cnt0, 0
    load cnt1, 0
    load cnt2, 0
    load cnt3, 0

startstop_check:
    in temp, BUTTON
    and temp, 1

; state machine
    comp state, state_r0
    jump z,run0
    comp state, state_r1
    jump z,run1
    comp state, state_s0
    jump z, stop0
    comp state, state_s1
    jump z, stop1
    load state, state_s0

stop0:
    comp temp, 1
    jump nz, fsm_end
    load state, state_r0
    jump fsm_end
run0:
    comp temp, 0
    jump nz, fsm_end
    load state, state_r1
    jump fsm_end
run1:
    comp temp, 1
    jump nz, fsm_end
    load state, state_s1
    jump fsm_end
stop1:
    comp temp, 0
    jump nz, fsm_end
    load state, state_s0

fsm_end:

;wait 0.01s     
    call waitfcn

    comp state, state_s0
    jump z, decode
    comp state, state_s1
    jump z, decode

    add cnt0, 1
    comp cnt0, 10
    jump nz, decode
    load cnt0, 0
    add cnt1, 1
    comp cnt1, 10
    jump nz, decode
    load cnt1, 0
    add cnt2, 1
    comp cnt2, 10
    jump nz, decode
    load cnt2, 0
    add cnt3, 1
    comp cnt3, 10
    jump nz, decode
    load cnt3, 0

decode:
;decode for output
    load temp, cnt0
    call decodefcn
    out temp, SEG0

    load temp, cnt1
    call decodefcn
    out temp, SEG1

    load temp, cnt2
    call decodefcn
;add decimal point
    and temp, 254
    out temp, SEG2

    load temp, cnt3
    call decodefcn
    out temp, SEG3

    jump start

decodefcn:
    comp temp, 0
    jump nz, dec1
    load temp, decoded_0
    ret
dec1:
    comp temp, 1
    jump nz, dec2
    load temp, decoded_1
    ret
dec2:
    comp temp, 2
    jump nz, dec3
    load temp, decoded_2
    ret
dec3:
    comp temp, 3
    jump nz, dec4
    load temp, decoded_3
    ret
dec4:
    comp temp, 4
    jump nz, dec5
    load temp, decoded_4
    ret
dec5:
    comp temp, 5
    jump nz, dec6
    load temp, decoded_5
    ret
dec6:
    comp temp, 6
    jump nz, dec7
    load temp, decoded_6
    ret
dec7:
    comp temp, 7
    jump nz, dec8
    load temp, decoded_7
    ret
dec8:
    comp temp, 8
    jump nz, dec9
    load temp, decoded_8
    ret
dec9:
    load temp, decoded_9
    ret

waitfcn:
    load wait2, 25
wt0:
    load wait1, 25
wt1 :   
    load wait0, 200
wt2 :
    sub wait0, 1
    jump nz, wt2
    sub wait1, 1
    jump nz, wt1
    sub wait2, 1
    jump nz, wt0
    ret
END

回答1:


Basically it could be done by adding states and copy-editing the code for the second button + moving the second button stuff to the third button. Then there is the editing of upcounting to down counting:

;wait 0.01s     
    call waitfcn

    comp state, state_s0
    jump z, decode
    comp state, state_s1
    jump z, decode

    sub  cnt0, 1
    comp cnt0, 255 ; -1
    jump nz, decode
    load cnt0, 9
    sub  cnt1, 1
    comp cnt1, 255
    ...


来源:https://stackoverflow.com/questions/20792185/how-to-reverse-my-stopwatch-code-to-count-down-in-assembler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!