Advertisement
Kitomas

kit-8 program that moves a tiny crewmate around

Apr 13th, 2023 (edited)
2,348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "std/pseudoOps.asm"
  2. ;anti-clobbering single-depth subroutine technology
  3. #define L1ARG 0xea
  4. #define L1IRG 0xeb
  5. #define L1CRG 0xec
  6. #define L1RTA 0xee
  7. #define l1call_abs(_addr) /* only saves L&H */\
  8.     stc_abs(L1CRG)          /* save C (L&H) */                      &&\
  9.     ldc_imm(_addr) && jpr   /* load subroutine address && jpr */    &&\
  10.     ldc_abs(L1CRG)          /* restore C */
  11. #define l1call_abs_s(_addr) \
  12.     sta $,L1ARG                     /* save registers to L1_RG */           &&\
  13.     sti $,L1IRG                     /* ^^ */                                &&\
  14.     stc_abs(L1CRG)                  /* ^^ */                                &&\
  15.     ldc_imm(_addr) && jpr        /* load subroutine address && jpr there */ &&\
  16.     ldi #,0                         /* restore registers after returning */ &&\
  17.     lda @,L1ARG                     /* ^^ */                                &&\
  18.     ldl @,L1CRG && ldh @,1+L1CRG    /* ^^ */                                &&\
  19.     ldi @,L1IRG                     /* ^^ */                           
  20. #define l1prep stc_abs(L1RTA)       //save return address for later
  21. #define l1rts ldc_abs(L1RTA) && jpr //retrieve return address and return
  22.  
  23. ;used to actually call these subroutines
  24. #define adsb8s_16(_d,_val, _ptrA) \
  25.     lda _d,(_val) && ldc_imm(_ptrA) &&\
  26.     stc_abs(math_ptrA)              &&\
  27.     l1call_abs_s(_adsb8s_16)
  28. #define add16_16(_ptrA,_ptrB) \
  29.     ldc_imm(_ptrA) && stc_abs(math_ptrA)    &&\
  30.     ldc_imm(_ptrB)                          &&\
  31.     stc_abs(math_ptrB)                      &&\
  32.     l1call_abs_s(_add16_16)
  33.  
  34. ;.1*256 = 25.6 ~= 26
  35. ;vel and pos are fixed point numbers with 8 bits of fraction
  36. #define accel (.1*256)
  37. #define current_canvas 3
  38. ;pitch is how many pixels across the current canvas is
  39. #define pitch (16*2**current_canvas)
  40. #define vram_start (0x8000-pitch**2)
  41.  
  42. ;program start
  43.     set_canvas(#,current_canvas)
  44.     jmp 0x200
  45. !@= 0x200 ;loop start
  46.    
  47.     ;key event velocity change
  48.            get_key(#,'w') && bzs no_w   ;if 'w' key pressed,
  49.         adsb8s_16(#,-accel, vel_y)      ;^^vel_y -= accel
  50.     :no_w: get_key(#,'a') && bzs no_a   ;if 'a' key pressed,
  51.         adsb8s_16(#,-accel, vel_x)      ;^^vel_x -= accel
  52.     :no_a: get_key(#,'s') && bzs no_s   ;if 's' key pressed,
  53.         adsb8s_16(#,accel, vel_y)       ;^^vel_y += accel
  54.     :no_s: get_key(#,'d') && bzs no_d   ;if 'd' key pressed,
  55.         adsb8s_16(#,accel, vel_x)       ;^^vel_x += accel
  56.     :no_d:
  57.    
  58.     add16_16(pos_x_low, vel_x)  ;add x velocity to x position
  59.     add16_16(pos_y_low, vel_y)  ;add y velocity to y position
  60.    
  61.     ldi #,0                             ;set index register to 0
  62.     lda @,pos_x                         ;put x&y into sprites' x&y
  63.     sta $,amoog && sta $,amoog_flip     ;^^
  64.     lda @,pos_y                         ;^^
  65.     sta $,amoog+1 && sta $,amoog_flip+1 ;^^
  66.    
  67.     lda #,0                     ;set every pixel of canvas to palette color 0
  68.     sys #,0x05                  ;^^
  69.     lda @,vel_x+1 && and #,0x80 ;if x velocity negative,
  70.     bnz .f                      ;^^blit the horizontally-flipped sprite instead
  71.     ldc_imm(amoog) && jmp .r    ;^^
  72. .f: ldc_imm(amoog_flip)         ;^^
  73. .r: sys #,0x06                  ;^^(sys #6 blits the sprite at address C (L&H))
  74.    
  75.     ldh @,pos_x         ;this doesn't do anything other than put
  76.     ldl @,pos_y         ;^^the position into L&H as a sort of debug print
  77.     wait_frame
  78.    
  79.     jmp 0x200 ;go to start of loop
  80.  
  81.  
  82. :math_ptrA: !16 0 ;pointers that hold locations to operands
  83. :math_ptrB: !16 0 ;^^
  84.  
  85. ;*ptrA += (signed)A
  86. :_adsb8s_16:
  87.     ldi #,0                             ;set index to 0 for low byte
  88.     and #,0xff && bns .sub              ;update n flag && branch to .sub if negative
  89.     lda @,L1ARG                         ;restore original contents of A
  90.     add *,math_ptrA && sta *,math_ptrA  ;add A to low byte && store result
  91.     ldi #,1 && lda *,math_ptrA          ;set index to 1 for high byte && load high byte
  92.     adc #,0 && sta *,math_ptrA          ;if carry set, increment && store result
  93.     jpr                                 ;return from subroutine
  94. .sub: ;(remember, it's A-memory, not memory-A)
  95.     lda @,L1ARG                         ;get two's complement of A
  96.     xor #,0xff && add #,1               ;^^(to make sure we're not subtracting
  97.     sta $,_SCRATCH_A                    ;^^ by a negative number)
  98.     sec                                 ;preemtively set carry flag so borrow isn't factored in
  99.     lda *,math_ptrA                     ;get low byte of 16-bit number
  100.     sbc @,_SCRATCH_A && sta *,math_ptrA ;subtract low byte by A && store result
  101.     bcs .r                              ;if no borrow, then skip decrement
  102.     ldi #,1 && lda *,math_ptrA          ;set index to 1 for high byte && load high byte
  103.     add #,-1 && sta *,math_ptrA         ;decrement high byte && store result
  104.                                         ;^^'add -1' instead of 'sec && sbc 1' so carry flag is ignored
  105. .r: jpr                                 ;return from subroutine
  106.  
  107. ;*ptrA += *ptrB
  108. :_add16_16:
  109.     ldi #,0 && lda *,math_ptrA          ;set index to 0 for low byte && load low byte
  110.     add *,math_ptrB && sta *,math_ptrA  ;add low bytes && store result
  111.     ldi #,1 && lda *,math_ptrA          ;set index to 1 for high byte && load high byte
  112.     adc *,math_ptrB && sta *,math_ptrA  ;add high bytes (+carry bit) && store result
  113.     jpr                                 ;return from subroutine
  114.  
  115. ;pitch/2-(w or h)/2 = centered
  116. :pos_x_low: !8 0 && :pos_x: !8 pitch/2-4
  117. :pos_y_low: !8 0 && :pos_y: !8 pitch/2-4
  118. :vel_x: !16 0  &&  :vel_y: !16 0
  119. #define rd 196 //palette color red
  120. #define bl 80  //palette color blue
  121. #define wh 231 //palette color white
  122. ;x,y, w,h, pixel data...
  123. :amoog: !8  0,0, 8,8
  124. !8   0, 0,rd,rd,rd,rd,rd,rd
  125. !8   0, 0,rd,rd,rd,rd,rd,rd
  126. !8  rd,rd,rd,rd,bl,wh,wh,wh
  127. !8  rd,rd,rd,rd,bl,bl,bl,bl
  128. !8  rd,rd,rd,rd,rd,rd,rd,rd
  129. !8  rd,rd,rd,rd,rd,rd,rd,rd
  130. !8   0, 0,rd,rd, 0, 0,rd,rd
  131. !8   0, 0,rd,rd, 0, 0,rd,rd
  132. :amoog_flip: !8  0,0, 8,8
  133. !8  rd,rd,rd,rd,rd,rd, 0, 0
  134. !8  rd,rd,rd,rd,rd,rd, 0, 0
  135. !8  wh,wh,wh,bl,rd,rd,rd,rd
  136. !8  bl,bl,bl,bl,rd,rd,rd,rd
  137. !8  rd,rd,rd,rd,rd,rd,rd,rd
  138. !8  rd,rd,rd,rd,rd,rd,rd,rd
  139. !8  rd,rd, 0, 0,rd,rd, 0, 0
  140. !8  rd,rd, 0, 0,rd,rd, 0, 0
  141.  
  142. !savlbl "labels.txt" && !to "program.bin" && !save
Tags: kit-8
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement