Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @ Function Declaration : int mg_game_demo(int count, int delay)
- @
- @ Input: r0, r1, r2 (i.e. r0 holds delay, r1 holds pattern, r2 holds target)
- @ Returns: 0
- @
- @ Here is the actual function
- mg_game_demo:
- push {r4-r9, lr} @ Put aside registers we want to restore later
- mov r4, r0 @ Stores the value of delay into r4
- mov r5, r1 @ Stores the value of pattern into r5
- mov r6, r2 @ Stores the value of target into r6
- reset_pattern: @ Resets the LED counter so you can do led_toggle_loop from scratch
- mov r7, r5 @ r7 now holds the address of the value of the 1st LED to be toggled
- mov r9, #7 @ r9 acts like a down counter of times left to loop
- led_toggle_loop: @ Toggle each LED once
- ldrb r8, [r7], #1 @ Load LED number(as an 8bit value, so only one char) into r8 add skip to next LED number
- sub r8, #48 @ Convert ASCII value to decimal number
- mov r0, r8 @ r0 now holds the number of LED to be toggled. Will be passed as a parameter
- bl BSP_LED_Toggle @ call BSP C function using Branch with Link (bl)
- mov r0, r4 @ r0 now holds the delay time, to be used in check_button_delay
- mov r1, r6 @ r1 now holds the target LED value, to be used in check_button_delay
- mov r2, r8 @ r2 now holds the ON LED value, to be used in check_button_delay
- bl check_button_delay @ Calls busy_delay with r0,r1 and r2 as its arguments
- beq win @ If function returns 0, user pressed the button at the RIGHT time
- bmi lose @ If function returns -1, user pressed the button at the WRONG time
- mov r0,r8 @ r0 now holds the number of LED to be toggled. Will be passed as a parameter
- bl BSP_LED_Toggle @ call BSP C function using Branch with Link (bl)
- subs r9, #1 @ Count down until all the LEDs have been toggled
- bpl led_toggle_loop @ Loop until all LEDs are toggled
- b reset_pattern @ Loop until we user presses the button
- win: @ Toggle all LED at the same time twice
- mov r0, r8 @ r0 grabs the value of the LED currently ON...
- bl BSP_LED_Toggle @ and here we turn it OFF
- mov r5, #3 @ Store in r5 the number of times to toggle all LEDs
- reset_leds:
- mov r6, #7 @ Store in r6 the total number of LEDs to be toggled
- toggle_all_leds_loop:
- mov r0,r6 @ r0 holds the value of the LED to be toggled
- bl BSP_LED_Toggle @ Toggle LED
- subs r6, #1 @ Move to next LED
- bpl toggle_all_leds_loop
- mov r0, r4 @ r0 now holds the delay time, to be used in busy_delay
- bl busy_delay
- subs r5, #1 @ Subtract one from the number of times all LEDs need to be toggled
- bpl reset_leds
- b end
- lose:
- mov r0, r8 @ r0 grabs the value of the LED currently ON...
- bl BSP_LED_Toggle @ and here we turn it OFF
- mov r8, #5 @ Store in r8 number of times the LED will be toggled
- toggle_one_led_loop:
- mov r0,r6 @ r0 holds the value of target LED
- bl BSP_LED_Toggle
- mov r0, r4 @ r0 now holds the delay time, to be used in busy_delay
- bl busy_delay
- subs r8, #1 @ Subtract one from the number of times this one LED needs to be toggled
- bpl toggle_one_led_loop
- end:
- mov r0, #0 @ Return zero (always sucessful)
- pop {r4-r9,lr} @ Restore the orginal values of the registers
- bx lr @ Return (Branch eXchange) to the address in the link register (lr)
- .size mg_game_demo, .-mg_game_demo @@ - symbol size (not req)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement