Advertisement
ColdWater0

Snake_evo on R216K8B

Mar 25th, 2019 (edited)
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;This does not use R2TERM's keyboard.
  2. ;Use Qweryntino's controller directly instead.
  3. ;Responsiveness is good due to low delay time.
  4.  
  5. ;r0,r1,r2: registers usable in any part of this code
  6. ;r4: head position
  7. ;r5: tail position
  8. ;r6: position of apple
  9. ;r9: game speed timer
  10. ;r10 : terminal port address = 0
  11. ;r11 : random number generator port address = 1
  12. start:
  13.     mov r10, 0      ;Specify the terminal port number.
  14.     mov r11, 1      ;the port addr of input device
  15.     mov r12, 2      ;the port addr of RNG
  16.     mov sp, 0x0200      ;the stack pointer
  17.     mov r7, 0x30        ;digits on the scoreboard.
  18.     mov r8, 0x2F
  19.     send r10, 0x1000
  20.     call draw_hori_wall ;Draw the border of the game screen.
  21.     send r10, 0x10B0
  22.     call draw_hori_wall
  23.     mov r0, 0x1000
  24.     call draw_vert_wall
  25.     mov r0, 0x100F
  26.     call draw_vert_wall
  27.     jmp setup       ;Jump to the game preparation routine.
  28.  
  29. draw_hori_wall:         ;Border drawing routine
  30.     send r10, 0x200F
  31.     mov r1, 16
  32.     .loop:
  33.         send r10, 0x7F
  34.         send r10, 0x7F
  35.         sub r1, 2
  36.         jnz .loop
  37.     ret
  38. draw_vert_wall:         ;Border drawing routine
  39.         send r10, r0
  40.         mov r1, 12
  41.         .loop:
  42.         send r10, r0
  43.         send r10, 0x7F
  44.         add r0, 0x10
  45.         send r10, r0
  46.         send r10, 0x7F
  47.         add r0, 0x10
  48.         sub r1, 2
  49.         jnz .loop
  50.         ret
  51.  
  52. ;This is a routine that prepares you for the game to begin.
  53. setup:
  54.     send r10, 0x1057        ;Before the game starts, the controller should point to the right..
  55.     send r10, 0x2004
  56.     send r10, 0x7F
  57.     .right:                    
  58.         recv r0, r11
  59.         .chk:
  60.         cmp r0, 'd'
  61.         jne .right
  62.     send r10, 0x200F
  63.     jmp game
  64. game:
  65.     mov r4, map         ;Set the snake's head position.
  66.     add r4, 86
  67.     mov r5, map         ;Set the snake's tail position.
  68.     add r5, 84
  69.     jmp apple           ;Jump to the apple creation routine to generate the first apple.
  70.  
  71. MAIN_loop:              ;This is the main loop of the game.
  72.     .speed_regulator:       ;This is the part that does nothing to control the speed of the game.
  73.     sub r9, 1
  74.     jnz .speed_regulator
  75.     recv r0, r11
  76.     add r0, keymap          ;Jump to the designated place according to the keymap.
  77.     jmp [r0]
  78.  
  79. ;Specify the speed of the game.
  80. ;should be tuned with the frequency of R2.
  81. n_speed: dw 6               ;Timer when proceeding normally without eating an apple
  82. a_speed: dw 2               ;Timer when eating an apple and creating an apple
  83.  
  84. ;This is a routine that is executed when each key is pressed.
  85. ;Draw the snake's head on the terminal and place the head on the game map.
  86. key_up:
  87.     sub r4, 16          ;Change the head position.
  88.     send r10, r4            ;Draw the head.
  89.     send r10, 0x7F
  90.     mov r1, [r4]            ;Check if the snake hit a wall or body.
  91.     jnz gameover
  92.     mov [r4+16], 3          ;Place the snake's body on the game map.
  93.     jmp chkapple            ;Jump to the routine to check if an apple has been eaten.
  94. key_down:
  95.     add r4, 16
  96.     send r10, r4
  97.     send r10, 0x7F
  98.     mov r1, [r4]
  99.     jnz gameover
  100.     mov [r4-16], 4
  101.     jmp chkapple
  102. key_left:
  103.     sub r4, 1
  104.     send r10, r4
  105.     send r10, 0x7F
  106.     mov r1, [r4]
  107.     jnz gameover
  108.     mov [r4+1], 1
  109.     jmp chkapple
  110. key_right:
  111.     add r4, 1
  112.     send r10, r4
  113.     send r10, 0x7F
  114.     mov r1, [r4]
  115.     jnz gameover
  116.     mov [r4-1], 2
  117.     jmp chkapple
  118.  
  119. ;If the controller is not properly installed
  120. ;and a value other than wasd is entered, the program stops.
  121. key_error:
  122.     send r10, 0x1005
  123.     send r10, 'E'
  124.     send r10, ':'
  125.     send r10, 'h'
  126.     send r10, 'a'
  127.     send r10, 'l'
  128.     send r10, 't'
  129.     hlt
  130.  
  131. ;Check if the snake's head touches the apple.
  132. chkapple:
  133.     cmp r4, r6
  134.     jne tail
  135.  
  136. ;If the head touches the apple, run apple creation routine.
  137. apple:
  138.     recv r6, r12            ;Receive a random number from RNG.
  139.     and r6, 0x00FF          ;Trim random number so they can be placed on the game map.
  140.     add r6, 0x1000
  141.     cmp r6, r4          ;If the apple is generated in the same place
  142.     je apple            ;as the current head position, it receives a random number again.
  143.  
  144.     cmp [r6], 0         ;Make sure it doesn't spawn on the snake's body or walls.
  145.     jne apple
  146.     mov r9, [a_speed]       ;Set the game timer.
  147.     add r8, 1           ;increase score
  148.     cmp r8, 0x3A            ;If r8 goes beyond 9, we increase r7.
  149.     jne .output_score       ;If not, the score is printed immediately.
  150.     add r7, 1
  151.     mov r8, 0x30
  152.     .output_score:
  153.     send r10, 0x1007
  154.     send r10, 0x20F0
  155.     send r10, r7
  156.     send r10, r8
  157.     send r10, 0x200F
  158.     .show_apple:   
  159.     send r10, r6
  160.     send r10, 0x30
  161.     jmp MAIN_loop
  162.  
  163. ;It is a routine to draw the tail.
  164. ;When scored, this part is skipped to stretch the tail.
  165. tail:
  166.     mov r9, [n_speed]       ;Set the game timer.
  167.     mov r0, [r5]            ;Read where to move the tail position.
  168.     add r0, .t_table
  169.     jmp [r0]            ;Jumps to the direction routine where the tail needs to be moved.
  170.     .t_table:
  171.         dw 0, t_left, t_right, t_up, t_down
  172. t_up:
  173.     send r10, r5            ;Draw the tail.
  174.     send r10, 0x20
  175.     mov [r5], 0         ;Records that there is no snake in the previous tail position.
  176.     sub r5, 16          ;Change the position of the tail.
  177.     jmp MAIN_loop
  178. t_down:
  179.     send r10, r5
  180.     send r10, 0x20
  181.     mov [r5], 0
  182.     add r5, 16
  183.     jmp MAIN_loop
  184. t_left:
  185.     send r10, r5
  186.     send r10, 0x20
  187.     mov [r5], 0
  188.     sub r5, 1
  189.     jmp MAIN_loop
  190. t_right:
  191.     send r10, r5
  192.     send r10, 0x20
  193.     mov [r5], 0
  194.     add r5, 1
  195.     jmp MAIN_loop
  196.  
  197.  
  198. ;If the head touches the wall or the body of a snake, output the game over and halt.
  199. gameover:
  200.     send r10, 0x1006
  201.     send r10, 0x20F0
  202.     send r10, 'X'
  203.     send r10, r7
  204.     send r10, r8
  205.     send r10, 'X'
  206.     hlt
  207.  
  208. ;This is a keymap that also exists in the keyboard version.
  209. ;Unlike the keyboard version, input other than the designated key is regarded as an error.
  210. keymap:
  211.     dw key_error, key_error, key_error, key_error, key_error, key_error, key_error, key_error, key_error, key_error     ;
  212.     dw key_error, key_error, key_error, key_error, key_error, key_error, key_error, key_error, key_error, key_error     ;
  213.     dw key_error, key_error, key_error, key_error, key_error, key_error, key_error, key_error, key_error, key_error     ;
  214.     dw key_error, key_error, key_error, key_error, key_error, key_error, key_error, key_error, key_error, key_error     ;   !"#$%&'
  215.     dw key_error, key_error, key_error, key_error, key_error, key_error, key_error, key_error, key_error, key_error     ;()*+,-./01
  216.     dw key_error, key_error, key_error, key_error, key_error, key_error, key_error, key_error, key_error, key_error     ;23456789:;
  217.     dw key_error, key_error, key_error, key_error, key_error, key_left, key_error, key_error, key_right, key_error      ;<=>?@ABCDE
  218.     dw key_error, key_error, key_error, key_error, key_error, key_error, key_error, key_error, key_error, key_error     ;FGHIJKLMNO
  219.     dw key_error, key_error, key_error, key_down, key_error, key_error, key_error, key_up, key_error, key_error         ;PQRSTUVWXY
  220.     dw key_error, key_error, key_error, key_error, key_error, key_error, key_error, key_left, key_error, key_error      ;Z[\]^_`abc
  221.     dw key_right, key_error, key_error, key_error, key_error, key_error, key_error, key_error, key_error, key_error     ;defghijklm
  222.     dw key_error, key_error, key_error, key_error, key_error, key_down, key_error, key_error, key_error, key_up         ;nopqrstuvw
  223.     dw key_error, key_error, key_error, key_error, key_error, key_error, key_error, key_error, key_error, key_error     ;xyz{|}~  
  224.  
  225.  
  226.  
  227. ;This is why this code should run on R216K8B.
  228. ;To run the game, the game map coordinates must be converted to terminal cursor commands.
  229. ;However, I was able to save resources to convert by placing the game map at the address that matches the 0x10XX command one-to-one.
  230. org 0x1000
  231.  
  232. map:
  233.     dw 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
  234.     dw 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
  235.     dw 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
  236.     dw 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
  237.     dw 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
  238.     dw 1,0,0,0,2,2,2,0,0,0,0,0,0,0,0,1
  239.     dw 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
  240.     dw 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
  241.     dw 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
  242.     dw 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
  243.     dw 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
  244.     dw 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
  245.     dw 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
  246.     dw 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
  247.     dw 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
  248.     dw 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement