Advertisement
Guest User

Untitled

a guest
May 11th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. define queueH $10
  2. define queueB $11
  3. define queueT $12
  4. define qLength $02
  5. define prevBlock $05
  6. define currentBlock $06
  7.  
  8. ; Use the monitor to see visually what the queue is doing.
  9. ; Step through the program to see how the queue changes.
  10. main:
  11.   ; Initialize the memory for our Queue structure.
  12.   jsr init
  13.  
  14.   ; Insert 1 2 3 4 into our queue.
  15.   lda #$01
  16.   jsr insert
  17.   lda #$02
  18.   jsr insert
  19.   lda #$03
  20.   jsr insert
  21.   lda #04
  22.   jsr insert
  23.  
  24.   ; pop 1 from the queue
  25.   jsr pop
  26.  
  27.   ; pop 2 from the queue
  28.   jsr pop
  29.  
  30.   ; pop 3 from the queue
  31.   jsr pop
  32.    
  33.   ; pop 4 from the queue
  34.   jsr pop
  35.  
  36.   ; insert 5 6 7 into the queue
  37.   lda #$05
  38.   jsr insert
  39.   lda #$06
  40.   jsr insert
  41.   lda #$07
  42.   jsr insert
  43.  
  44.  
  45.   brk
  46.  
  47.  
  48.  
  49. init:
  50.   lda #$0
  51.   sta queueH
  52.   sta queueB
  53.   sta queueT
  54.   sta qLength
  55.   rts
  56.  
  57. insert:
  58.   ; Check if we're storing nothing
  59.   cmp #$0
  60.   ; Exit the function if the A register is #$0
  61.   beq endEarly
  62.  
  63.   ; Insert whatever is stored on A
  64.   ; register to the end of the queue.
  65.   ldx qLength
  66.  
  67.   ; qLength offsets the insert position to the
  68.   ; end of the queue.
  69.   sta queueH, x
  70.  
  71.   ; Increase the length of the queue
  72.   ; after insertion.
  73.   inc qLength
  74.  
  75.   ; Zero out the A register.
  76.   lda #$0
  77.   rts
  78.  
  79. ; Removes the first element of the queue
  80. ; and shifts the remaining values forward.
  81. pop:
  82.   ; Check if the queue is empty.
  83.   ldx qLength
  84.   cpx #$0
  85.  
  86.   ; If the queue is empty rts and do nothing.
  87.   beq endEarly
  88.  
  89.   ; Zero out the X register that will be used
  90.   ; in the shiftForward subroutine.
  91.   ldx #$0
  92.   jsr shiftForward
  93.  
  94.   ; After the values have been shifted forward
  95.   ; in the queue decrease the length of the queue.
  96.   dec qLength
  97.  
  98.   rts
  99.  
  100. ; Shift the values forward in the queue.
  101. ; *Note: This subroutine is very similar to the
  102. ;        shift values subroutine in our snake game.
  103. shiftForward:
  104.   cpx qLength
  105.   beq endEarly
  106.   lda queueB, x
  107.   sta queueH, x
  108.   inx
  109.   jsr shiftForward
  110.  
  111. ; Ends a subroutine early if needed.
  112. endEarly:
  113.   rts
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement