Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. *       = $0801
  3.         .word (+), 2005  ;pointer, line number
  4.         .null $9e, format("%d", start);will be sys 4096
  5. +   .word 0          ;basic line end
  6.        
  7. * = $1000  
  8.  
  9.                             ; CONSTANTS
  10. SCREEN_SCROLL = $D016
  11. WRITE_PTR = $BB
  12. READ_PTR = $FB
  13. SCREEN_PAGE_PTR = $0288
  14.    
  15. BORDERCOLOR = $D020
  16. BGCOLOR = $D021
  17. CHRCOLOR = $0286
  18. SPACE = #$20
  19.  
  20. SCREENSTART = $0400
  21. SCREENEND = $07FF
  22.    
  23. CIA_TIMER_ORA = $DC0E
  24. IO_MEM_TAPE_CTRL = $01
  25.  
  26. CHAR_ROM = $D000
  27. CHROUT = $FFD2 
  28.                            ; GLOBAL VARIABLES
  29.    
  30. colIndex .byte 0
  31. fillDone .byte 0
  32.  
  33. xLimit .byte ?
  34. screenIndex .byte ?
  35. byteToRead .byte ?
  36. frontLimit .byte ?
  37.  
  38. inter .word ?
  39.    
  40. fillBuffer
  41.     ldx #0
  42.     stx byteToRead
  43.  
  44. nextRow lda colIndex
  45.     cmp #0
  46.     bne skip0
  47.     sta screenIndex
  48.     lda #40
  49.     sta frontLimit
  50.     jmp fillFront
  51.    
  52. skip0
  53.     sec
  54.     lda #40
  55.     sbc colIndex
  56.     sta frontLimit
  57.     sta screenIndex
  58.    
  59. fillBack
  60.     ldy byteToRead
  61.     lda (READ_PTR),y    ;y starts always at 0
  62.     ldy screenIndex     ;y start always at 40 - colIndex
  63.     sta (WRITE_PTR),y
  64.     inc byteToRead
  65.     inc screenIndex
  66.     ldy screenIndex
  67.     cpy #40    
  68.     bne fillBack
  69.     lda colIndex
  70.     sta byteToRead
  71.     lda #0
  72.     sta screenIndex
  73.    
  74. fillFront
  75.     ldy byteToRead
  76.     lda (READ_PTR),y
  77.     ldy screenIndex
  78.     sta (WRITE_PTR),y
  79.     inc byteToRead
  80.     inc screenIndex
  81.     ldy screenIndex
  82.     cpy frontLimit
  83.     bcc fillFront
  84.     ;update pointers (change row)
  85.     clc
  86.     lda WRITE_PTR
  87.     adc #40
  88.     sta WRITE_PTR
  89.     lda WRITE_PTR+1
  90.     adc #00
  91.     sta WRITE_PTR+1
  92.     clc
  93.     lda READ_PTR
  94.     adc #40
  95.     sta READ_PTR
  96.     lda READ_PTR+1
  97.     adc #00
  98.     sta READ_PTR+1
  99.     ;check rownumber and reset offsets
  100.     lda #0
  101.     sta byteToRead
  102.         inx
  103.     cpx #25
  104.     bcc nextRow
  105. rts
  106.    
  107. notDefault
  108.     ora #128
  109.     sta $D018
  110.     lda #04
  111.     sta SCREEN_PAGE_PTR
  112.     jsr initPointers
  113. rts
  114.    
  115. setScreenColors
  116.     ; set color
  117.     lda #$0b
  118.     sta $0286
  119.     ; set border color
  120.     lda #$00
  121.     sta $D020
  122.     ; set background color
  123.     lda #$0c
  124.     sta $D021
  125. rts
  126.  
  127. initPointers
  128.     lda #00
  129.     sta WRITE_PTR
  130.     lda SCREEN_PAGE_PTR
  131.     sta WRITE_PTR+1
  132.     lda #<screen
  133.     sta READ_PTR
  134.     lda #>screen
  135.     sta READ_PTR+1
  136. rts
  137.  
  138. swapBuffers
  139.     lda $D018
  140.     tax
  141.     and #240
  142.     cmp #16
  143.     bne noDefault
  144.         txa
  145.     and #15         ; x%0000 1111
  146.     ora #128
  147.     sta $D018
  148.     lda #$04
  149.     sta SCREEN_PAGE_PTR
  150.         rts
  151.  
  152. noDefault
  153.     txa
  154.     and #15         ; x%0000 1111
  155.     ora #16
  156.     sta $D018
  157.     lda #$20
  158.     sta SCREEN_PAGE_PTR
  159.         rts
  160.  
  161. *       =           $1600
  162. start
  163.     lda #7
  164.     sta SCREEN_SCROLL
  165.                 ; clear the screen
  166.         jsr setScreenColors
  167.     jsr $e544
  168.     jsr initPointers
  169.     jsr fillBuffer
  170.     lda #$20
  171.     sta SCREEN_PAGE_PTR
  172.         jsr initPointers
  173.     inc colIndex
  174. ;   jmp endlessloop
  175.  
  176. sei        ;disable maskable IRQs
  177.  
  178. lda #$7f
  179. sta $dc0d  ;disable timer interrupts which can be generated by the two CIA chips
  180. sta $dd0d  ;the kernal uses such an interrupt to flash the cursor and scan the keyboard, so we better
  181.            ;stop it.
  182.  
  183. lda $dc0d  ;by reading this two registers we negate any pending CIA irqs.
  184. lda $dd0d  ;if we don't do this, a pending CIA irq might occur after we finish setting up our irq.
  185.            ;we don't want that to happen.
  186.  
  187. lda #$01   ;this is how to tell the VICII to generate a raster interrupt
  188. sta $d01a
  189.  
  190. lda #251   ;this is how to tell at which rasterline we want the irq to be triggered
  191. sta $d012
  192.  
  193. lda #$1b   ;as there are more than 256 rasterlines, the topmost bit of $d011 serves as
  194. sta $d011  ;the 9th bit for the rasterline we want our irq to be triggered.
  195.            ;here we simply set up a character screen, leaving the topmost bit 0.
  196.  
  197. lda #$35   ;we turn off the BASIC and KERNAL rom here
  198. sta $01    ;the cpu now sees RAM everywhere except at $d000-$e000, where still the registers of
  199.            ;SID/VICII/etc are visible
  200.  
  201. lda #<irq  ;this is how we set up
  202. sta $fffe  ;the address of our interrupt code
  203. lda #>irq
  204. sta $ffff
  205.  
  206. cli        ;enable maskable interrupts again
  207.  
  208. ;   lda $314
  209. ;   sta inter
  210. ;   lda $315
  211. ;   sta inter+1
  212. ;   sei
  213. ;   lda #<IRQ_loop
  214. ;   sta $314
  215. ;   lda #>IRQ_loop
  216. ;   sta $315
  217. ;   cli
  218.  
  219. endlessloop
  220. ;   lda #250
  221. ;       cmp $d012
  222. ;   bcc skip9
  223. ;   jsr IRQ_loop
  224. ;skip9
  225.     lda fillDone
  226.     cmp #1
  227.     beq endlessloop
  228.     jsr fillBuffer
  229.     inc fillDone
  230.     jmp endlessloop
  231.  
  232. IRQ_loop
  233.     dec SCREEN_SCROLL
  234.     lda SCREEN_SCROLL
  235.     cmp #$FF
  236.     bne out
  237.    
  238. resetScroll
  239.     lda #07
  240.     sta SCREEN_SCROLL
  241.     jsr swapbuffers
  242.     jsr initPointers
  243.     lda #0
  244.     sta fillDone
  245.     inc colIndex
  246.         lda colIndex
  247.     cmp #40
  248.     bne out
  249.     lda #0
  250.     sta colIndex
  251. out
  252. rts
  253.     jmp (inter)
  254.  
  255.  
  256. screen
  257. .byte 160,160,160,160,160,160,160,160,160,160,160,160,160,160,232,230,230,230,230,230,230,230,230,232,160,160,160,160,160,232,230,230,230,230,230,230,232,160,160,160
  258. .byte 230,230,230,230,232,232,160,160,160,232,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,232,160,232,230,230,230,230,230,230,230,230,230,230,230,230
  259. .byte 230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,104, 46, 46, 46, 46, 46,104,104,230,230,230,230,230,230,230,104, 46, 46, 46, 46,104,230,230,230,230
  260. .byte 104,104,104,104,104,104,104,104,104,104,104,104,104, 46, 46, 46, 46, 46, 46, 46, 46, 46,104,104,104,104,104,104, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46,104,104
  261. .byte  32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,100,100,100,100,100,100,100,100,100,100,100, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
  262. .byte 100,100,100,100,100,100,100, 32, 32, 32, 32, 32,100,100,100,100,100, 45, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 45,100,100,100,100,100,100,100,100,100,100
  263. .byte  32, 32, 32, 32, 32, 32, 32, 45,100,100,100, 45, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
  264. .byte  32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
  265. .byte  32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
  266. .byte  32, 32, 32, 32, 32,104,104,104,104,104,104,104,104, 32, 32, 32, 32, 32,104, 32, 32,104, 32, 32, 32, 32, 32,104, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
  267. .byte 102,102,102,102,102,102,102,102,102,102,102,102,104,104, 32,102,102,102,102, 32,102,104, 32, 32,102,102,102,102, 32,102, 32,102, 32,102,104, 32,104,102,102,102
  268. .byte 102,102,104, 32, 32, 32, 32, 32,104,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,104, 32,102,102,102,102,102,102,102,102,102,102,102
  269. .byte  32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,104, 32, 32, 32, 32, 32, 32, 32, 32
  270. .byte  32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,104,102,102,102,102,102,102,102,102,102,104, 32,104,102,102,102,104, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
  271. .byte  32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
  272. .byte  32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
  273. .byte  32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
  274. .byte  32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
  275. .byte  32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
  276. .byte  32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,230,230,230,230,230,230,230,230,230,230,230, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
  277. .byte  32, 32, 32, 32, 32, 32, 32, 32, 32,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230, 32, 32, 32, 32, 32, 32, 32, 32, 32
  278. .byte 230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230
  279. .byte 230,230,230,230,232,160,232,230,230,230,230,232,160,160,160,160,160,160,160,232,230,230,232,160,160,160,160,160,160,232,230,230,232,160,160,232,230,230,230,230
  280. .byte 160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160
  281. .byte 160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288. irq
  289.  
  290. ;Being all kernal irq handlers switched off we have to do more work by ourselves.
  291. ;When an interrupt happens the CPU will stop what its doing, store the status and return address
  292. ;into the stack, and then jump to the interrupt routine. It will not store other registers, and if
  293. ;we destroy the value of A/X/Y in the interrupt routine, then when returning from the interrupt to
  294. ;what the CPU was doing will lead to unpredictable results (most probably a crash). So we better
  295. ;store those registers, and restore their original value before reentering the code the CPU was
  296. ;interrupted running.
  297.  
  298. ;If you won't change the value of a register you are safe to not to store / restore its value.
  299. ;However, it's easy to screw up code like that with later modifying it to use another register too
  300. ;and forgetting about storing its state.
  301.  
  302. ;The method shown here to store the registers is the most orthodox and most failsafe.
  303.  
  304. pha        ;store register A in stack
  305. txa
  306. pha        ;store register X in stack
  307. tya
  308. pha        ;store register Y in stack
  309.  
  310. jsr IRQ_loop   
  311.  
  312. lda #$ff   ;this is the orthodox and safe way of clearing the interrupt condition of the VICII.
  313. sta $d019  ;if you don't do this the interrupt condition will be present all the time and you end
  314.            ;up having the CPU running the interrupt code all the time, as when it exists the
  315.            ;interrupt, the interrupt request from the VICII will be there again regardless of the
  316.            ;rasterline counter.
  317.  
  318.            ;it's pretty safe to use inc $d019 (or any other rmw instruction) for brevity, they
  319.            ;will only fail on hardware like c65 or supercpu. c64dtv is ok with this though.
  320.  
  321. pla
  322. tay        ;restore register Y from stack (remember stack is FIFO: First In First Out)
  323. pla
  324. tax        ;restore register X from stack
  325. pla        ;restore register A from stack
  326.  
  327. rti        ;Return From Interrupt, this will load into the Program Counter register the address
  328.            ;where the CPU was when the interrupt condition arised which will make the CPU continue
  329.            ;the code it was interrupted at also restores the status register of the CPU
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement