ColdWater0

Snake_evo on R216K8B (Keyboard)

Oct 18th, 2020
1,131
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;Use R2TERM's keyboard.
  2. ;There is quite a delay in the encoding circuit of the keyboard.
  3. ;Use the controller version of Qweryntino for actual gameplay.
  4.  
  5. ;r0,r1,r2 : used temporarily
  6. ;r4 : position of head
  7. ;r5 : position of tail
  8. ;r6 : position of apple
  9. ;r10 : terminal port address = 0
  10. ;r11 : random source = 1
  11. start:
  12.     mov r10, 0          ;Specify the terminal port number.
  13.     mov r11, 1          ;the port addr of input device
  14.     mov r12, 2          ;the port addr of RNG
  15.     mov r13, 'd'            ;When the game starts for the first time, the keystrokes should be fixed to the right.
  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.         wait r0
  59.         js .right
  60.         bump r11
  61.         nop
  62.         nop
  63.         nop
  64.         recv r0, r11
  65.         .chk:
  66.         cmp r0, 'd'
  67.         jne .right
  68.     send r10, 0x200F
  69.     jmp game
  70.  
  71. game:
  72.     mov r4, map         ;Set the snake's head position.
  73.     add r4, 86
  74.     mov r5, map         ;Set the snake's tail position.
  75.     add r5, 84
  76.     jmp apple           ;Jump to the apple creation routine to generate the first apple.
  77.  
  78. MAIN_loop:              ;This is the main loop of the game.
  79.     .speed_regulator:       ;This is the part that does nothing to control the speed of the game.
  80.     sub r9, 1
  81.     jnz .speed_regulator
  82.     wait r0
  83.     js key_empty
  84.     bump r11            ;Request input from the keyboard
  85.     mov r0, keymap     
  86.     nop
  87.     nop
  88.     recv r1, r11
  89.     add r0, r1          ;Jump to the designated place according to the keymap.
  90.     jmp [r0]
  91.  
  92. ;Specify the speed of the game.
  93. ;should be tuned with the frequency of R2.
  94. n_speed: dw 6               ;Timer when proceeding normally without eating an apple
  95. a_speed: dw 2               ;Timer when eating an apple and creating an apple
  96.  
  97. key_empty:
  98.     mov r0, r13
  99.     add r0, keymap
  100.     jmp [r0]
  101. key_up:
  102.     mov r13, 'w'
  103.     sub r4, 16          ;Change the head position.
  104.     send r10, r4            ;Draw the head.
  105.     send r10, 0x7F
  106.     mov r1, [r4]            ;Check if the snake hit a wall or body.
  107.     jnz gameover
  108.     mov [r4+16], 3          ;Place the snake's body on the game map.
  109.     jmp chkapple            ;Jump to the routine to check if an apple has been eaten.
  110. key_down:
  111.     mov r13, 's'
  112.     add r4, 16
  113.     send r10, r4
  114.     send r10, 0x7F
  115.     mov r1, [r4]
  116.     jnz gameover
  117.     mov [r4-16], 4
  118.     jmp chkapple
  119. key_left:
  120.     mov r13, 'a'
  121.     sub r4, 1
  122.     send r10, r4
  123.     send r10, 0x7F
  124.     mov r1, [r4]
  125.     jnz gameover
  126.     mov [r4+1], 1
  127.     jmp chkapple
  128. key_right:
  129.     mov r13, 'd'
  130.     add r4, 1
  131.     send r10, r4
  132.     send r10, 0x7F
  133.     mov r1, [r4]
  134.     jnz gameover
  135.     mov [r4-1], 2
  136.     jmp chkapple
  137.  
  138. ;Check if the snake's head touches the apple.
  139. chkapple:
  140.     cmp r4, r6
  141.     jne tail
  142.  
  143. ;If the head touches the apple, run apple creation routine.
  144. apple:
  145.     recv r6, r12            ;Receive a random number from RNG.
  146.     and r6, 0x00FF          ;Trim random number so they can be placed on the game map.
  147.     add r6, 0x1000
  148.     cmp r6, r4          ;If the apple is generated in the same place
  149.     je apple            ;as the current head position, it receives a random number again.
  150.  
  151.     cmp [r6], 0         ;Make sure it doesn't spawn on the snake's body or walls.
  152.     jne apple
  153.     mov r9, [a_speed]       ;Set the game timer.
  154.     add r8, 1           ;increase score
  155.     cmp r8, 0x3A            ;If r8 goes beyond 9, we increase r7.
  156.     jne .output_score       ;If not, the score is printed immediately.
  157.     add r7, 1      
  158.     mov r8, 0x30
  159.     .output_score:
  160.     send r10, 0x1007
  161.     send r10, 0x20F0
  162.     send r10, r7
  163.     send r10, r8
  164.     send r10, 0x200F
  165.     .show_apple:       
  166.     send r10, r6
  167.     send r10, 0x30
  168.     jmp MAIN_loop
  169.  
  170. ;It is a routine to draw the tail.
  171. ;When scored, this part is skipped to stretch the tail.
  172. tail:
  173.     mov r9, [n_speed]       ;Set the game timer.
  174.     mov r0, [r5]            ;Read where to move the tail position.
  175.     add r0, .t_table
  176.     jmp [r0]            ;Jumps to the direction routine where the tail needs to be moved.
  177.     .t_table:
  178.         dw 0, t_left, t_right, t_up, t_down
  179. t_up:
  180.     send r10, r5            ;Draw the tail.
  181.     send r10, 0x20
  182.     mov [r5], 0         ;Records that there is no snake in the previous tail position.
  183.     sub r5, 16          ;Change the position of the tail.
  184.     jmp MAIN_loop
  185. t_down:
  186.     send r10, r5
  187.     send r10, 0x20
  188.     mov [r5], 0
  189.     add r5, 16
  190.     jmp MAIN_loop
  191. t_left:
  192.     send r10, r5
  193.     send r10, 0x20
  194.     mov [r5], 0
  195.     sub r5, 1
  196.     jmp MAIN_loop
  197. t_right:
  198.     send r10, r5
  199.     send r10, 0x20
  200.     mov [r5], 0
  201.     add r5, 1
  202.     jmp MAIN_loop
  203.  
  204. ;If the head touches the wall or the body of a snake, output the game over and halt.
  205. gameover:
  206.     send r10, 0x1006
  207.     send r10, 0x20F0
  208.     send r10, 'X'
  209.     send r10, r7
  210.     send r10, r8
  211.     send r10, 'X'
  212.     hlt
  213.  
  214.  
  215. keymap:
  216.     dw key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty     ;
  217.     dw key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty     ;
  218.     dw key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty     ;
  219.     dw key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty     ;   !"#$%&'
  220.     dw key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty     ;()*+,-./01
  221.     dw key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty     ;23456789:;
  222.     dw key_empty, key_empty, key_empty, key_empty, key_empty, key_left, key_empty, key_empty, key_right, key_empty      ;<=>?@ABCDE
  223.     dw key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty     ;FGHIJKLMNO
  224.     dw key_empty, key_empty, key_empty, key_down, key_empty, key_empty, key_empty, key_up, key_empty, key_empty         ;PQRSTUVWXY
  225.     dw key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_left, key_empty, key_empty      ;Z[\]^_`abc
  226.     dw key_right, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty     ;defghijklm
  227.     dw key_empty, key_empty, key_empty, key_empty, key_empty, key_down, key_empty, key_empty, key_empty, key_up         ;nopqrstuvw
  228.     dw key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty, key_empty     ;xyz{|}~  
  229. org 0x1000
  230.  
  231. map:
  232.     dw 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
  233.     dw 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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,2,2,2,0,0,0,0,0,0,0,0,1
  238.     dw 1,0,0,0,0,0,0,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,1,1,1,1,1,1,1,1,1,1,1,1,1,1,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.  
  249.  
  250.  
  251.     error:
  252.         send r10, 'E'
  253.         send r10, 'r'
  254.         send r10, 'r'
  255.         hlt
  256.  
RAW Paste Data