From http://www.jwz.org/xscreensaver/xscreensaver-5.38.tar.gz
[xscreensaver] / hacks / images / m6502 / dmsc.txt
1 ;
2 ; 6502 Assembler Demo.
3 ; Copyright (C) 2007  Daniel Serpell <daniel.serpell@gmail.com>
4 ;
5 ; Redistribution and use in source and binary forms, with or without
6 ; modification, are permitted provided that the following conditions are met:
7 ;
8 ; 1. Redistributions of source code must retain the above copyright notice,
9 ; this list of conditions and the following disclaimer.
10 ;
11 ; 2. Redistributions in binary form must reproduce the above copyright notice,
12 ; this list of conditions and the following disclaimer in the documentation
13 ; and/or other materials provided with the distribution.
14 ;
15 ; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 ; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 ; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 ; ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 ; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 ; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 ; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 ; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 ; CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 ; ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 ; POSSIBILITY OF SUCH DAMAGE.
26 ;
27
28 ; Variable definitions
29     temp0 = $20
30     temp1 = $21
31     temp2 = $22
32     temp3 = $23
33     temp4 = $24
34     temp5 = $25
35     
36     param0 = $10
37     param1 = $11
38     param2 = $12
39     param3 = $13
40
41
42 ; Output file
43     !to "all.bin", plain
44
45 ; Start of code
46     *= $600
47     jmp main
48
49 ; Used in "gira" and "circ"
50
51     gira_pos = $30
52     gira_scr = $200
53     circ_pos = $30
54     circ_scr = $210
55     show_pos = $31
56     show_source = $a000
57     show_dest = $500
58     text_state = $40
59     text_scr = $a000
60 ;    text_scr = $480
61     moire_buf = $a000
62     moire_scr = $200
63     moire_temp = $20
64
65 ; Include routines and macros
66
67 : ===========================================================================
68 :    !source "fillRect.asm"
69 : ===========================================================================
70
71     !zone {
72     
73 ; Uses the following:
74 ;  params: param0, param1
75 ;  temps: temp0
76
77
78 ; Locals
79     .color = temp0
80
81 ; Params
82     fillRect_buffer = param0
83     .buf = fillRect_buffer
84
85 ; fillRect:
86 ;   Fill a rectangle of 8x8 pixels with a color.
87 ; input: A  = color index
88 ;        .buffer = output buffer (address)
89     !macro fillRect .buffer {
90         ldx  #<.buffer
91         stx  fillRect_buffer
92         ldx  #>.buffer
93         stx  fillRect_buffer+1
94         jsr  fillRect_code
95     }
96
97 ; fillRect:
98 ;   Fill a rectangle of 8x8 pixels with a color.
99 ; input: .color  = color index
100 ;        .buffer = output buffer (address)    
101     !macro fillRect .buffer, .color {
102         lda  #.color
103         +fillRect .buffer
104     }
105     
106 fillRect_code:
107         sta     .color
108         ldx     #8
109 --
110         ldy     #8
111 -
112         sta     (.buf),y
113         dey
114         bne     -
115
116         lda     .buf
117         clc
118         adc     #32
119         sta     .buf
120     lda .buf+1
121     adc #0
122     sta .buf+1
123         lda     .color
124         dex
125         bne     --
126
127         rts
128
129 }
130
131
132 : ===========================================================================
133 :    !source "gira.asm"
134 : ===========================================================================
135
136     !zone {
137
138 ;
139 ; define "gira_pos" and "gira_scr" before including
140 ;
141     .pos = gira_pos
142     .scr00 = gira_scr
143     .scr01 = gira_scr + $8
144     .scr10 = gira_scr + $100
145     .scr11 = gira_scr + $108
146
147 ; gira:
148     !macro gira {
149         jsr  gira_code
150     }
151
152 ; init code:
153     !macro giraInit {
154         +fillRect gira_scr-1, 1
155         +fillRect gira_scr+7, 2
156         +fillRect gira_scr+$107, 3
157     }
158
159 gira_code:
160
161     inc  .pos
162     lda  .pos
163     and  #63
164     sta  .pos
165     tax
166
167     lda  .tabla,x
168     tax
169     lda  .scr11,x
170     adc  #1
171     and  #3
172  
173     sta  .scr11,x    
174     txa
175     eor  #$E7
176     tax
177     lda  .scr00,x
178     adc  #1
179     and  #3
180     sta  .scr00,x
181
182     lda  .pos
183     eor #63
184     tax
185     lda  .tabla,x
186     eor  #$E0
187     tax
188     lda  .scr01,x
189     adc  #1
190     and  #3
191     sta  .scr01,x
192
193     txa
194     eor  #$E7
195     tax
196     lda  .scr10,x
197     adc  #1
198     and  #3
199     sta  .scr10,x
200
201     rts
202
203 .tabla
204     !byte 224, 192, 160, 128,  96, 225,  64, 193, 161, 226, 129,  32, 194,  97, 162, 227
205     !byte 195, 130, 228,  65, 163, 196,  98, 229, 131, 164, 197, 230, 231, 132, 165, 198
206     !byte  99,  66,  33,   0, 199, 166, 133, 100, 167,  67, 134, 101, 135,  34,  68, 102
207     !byte 103,  69,  35,  70,  71,  36,   1,  37,  38,  39,   2,   3,   4,   5,   6,   7
208
209 }
210
211 : ===========================================================================
212 :    !source "circ.asm"
213 : ===========================================================================
214
215     !zone {
216 ;
217 ; define "circ_pos" before including
218 ;
219
220     .pos = circ_pos
221     .scr00 = circ_scr
222     .scr01 = circ_scr + $8
223     .scr10 = circ_scr + $100
224     .scr11 = circ_scr + $108
225
226 ; circ:
227     !macro circ {
228         jsr  circ_code
229     }
230
231 circ_code:
232
233         ldx     .pos
234     lda .tabla,x
235     tax
236         inc     .scr11,x
237     eor #07
238     tax
239         inc     .scr10,x
240     eor #$e7
241     tax
242         inc     .scr01,x
243     eor #07
244     tax
245         inc     .scr00,x
246
247     rts
248
249 .tabla
250         !byte   0,  32,   1,  33,  64,   2,  65,  34,  96,  66,   3,  97,  35,  98,  67, 128
251         !byte   4, 129,  36,  99, 130,  68, 160,   5, 161, 131, 100,  37, 162,  69, 132, 192
252         !byte 163, 101,   6, 193,  38, 194,  70, 164, 133, 195, 102, 224,   7, 225,  39, 165
253         !byte 226, 196, 134,  71, 227, 103, 197, 166, 228, 135, 198, 229, 167, 230, 199, 231
254
255 }
256
257
258 : ===========================================================================
259     !source "mapcopy.asm"
260 : ===========================================================================
261
262     !zone {
263
264 ;
265 ; define "show_pos" and "show_scr" before including
266 ;
267     .pos = show_pos
268     .source = show_source
269     .dest = show_dest
270
271 ; show:
272     !macro show {
273         jsr  show_code
274     }
275
276 show_code
277     ldx .pos
278     lda .tabla,x
279     tax
280 .source_label
281     lda .source,x
282 .dest_label
283     sta .dest,x
284     inc .pos
285     rts
286
287     show_source_addr = .source_label + 1
288     show_dest_addr = .dest_label + 1
289
290 .tabla
291     !byte 195, 227, 194, 162, 228,  97, 226, 128, 129, 130, 225,  64, 161, 163, 224,  96
292     !byte 160, 193, 196,  32,  98, 192,  65,   0, 131, 229, 164,  33,  99, 197,  66, 132
293     !byte   1, 230,  34, 165,  67, 100, 198,   2,   3,  68,   6,  35, 133, 166,   5, 101
294     !byte   4, 199,   7,  36, 231,  69, 134, 167,  37,  38, 102, 135,  70, 232,  71,  39
295     !byte 103, 136, 200, 168,   8,  11,  10, 169, 233, 104,   9,  12, 137, 201,  13,  40
296     !byte  44,  72,  43, 170,  42,  45,  41, 105,  73, 202,  14, 138, 234,  74,  75, 106
297     !byte 203,  76, 171,  46,  77, 235, 107, 139,  15, 108, 172, 174, 140, 173, 141, 142
298     !byte 204, 109, 206, 207, 205,  78, 175,  47, 236, 239, 143, 237, 238, 110, 240, 241
299     !byte 111, 208,  16,  79, 176, 209, 242,  48, 144, 243,  80, 177, 244, 112, 210,  17
300     !byte  49, 178, 179,  52,  53, 147,  18,  81, 211, 145,  19, 146,  51,  85, 180,  20
301     !byte  54, 113, 148,  22,  50,  84, 212,  21,  55, 115, 245,  83, 114, 116,  23,  82
302     !byte  86,  88,  89,  87, 118, 117, 119, 149, 213,  56,  57, 181, 214,  90, 121, 122
303     !byte 150, 151, 182, 183, 215, 246,  58, 120, 247,  24, 153, 184, 152, 216,  25,  26
304     !byte  59,  91, 248, 123, 185,  27,  60, 217, 154,  28,  92, 249,  29, 155, 186,  61
305     !byte 218,  93, 124, 250, 187,  30, 251,  62, 156,  94, 125, 219, 188,  31, 252, 254
306     !byte 220, 253,  63, 126, 255, 157, 221,  95, 159, 191, 222, 127, 189, 223, 158, 190
307
308 }
309
310
311 : ===========================================================================
312     !source "text.asm"
313 : ===========================================================================
314
315     !zone {
316
317 ;
318 ; define:
319 ;  "text_state": 8 bytes of internal state
320 ;  "text_scr"  : output screen address
321 ;  "text_data" : text character data
322 ;
323     text_char  = text_state + 0
324     .fps = text_state + 1
325     text_pixel = text_state + 2
326     text_skip  = text_state + 3
327     .cb  = text_state + 4
328     .fsz = text_state + 5
329     text_endFlag = text_state + 6
330     .scr = text_scr
331     .data = text_data    
332     
333     ; abbreviations
334     .pos = text_char
335     .cxy = text_pixel
336     .skp = text_skip
337
338 ; "pos" pointer to the next character
339 ; "cxy" screen position
340 ; "fps" position in font data of current character
341 ; "fsz" remaining bytes of current character
342 ; "skp" skip data, used in spaces, begining and ending
343 ; "cb"  current output byte (8 bits, one column)
344
345 ; text:
346     !macro text {
347         jsr  text_code
348     }
349
350     !macro textInit1 {
351         lda #$1f
352         sta text_pixel
353         lda #0
354         sta text_endFlag
355         sta text_skip
356         sta text_char
357         lda #1
358         sta text_base_color
359         lda #<text_scr
360         sta text_dest_addr
361         lda #>text_scr
362         sta text_dest_addr+1
363     }
364
365     !macro textInit2 .scrOut {
366         lda #$1f
367         sta text_pixel
368         lda #0
369         sta text_endFlag
370         sta text_skip
371         sta text_base_color
372         lda #<.scrOut
373         sta text_dest_addr
374         lda #>.scrOut
375         sta text_dest_addr+1
376     }
377
378
379 text_code
380
381     lda .cxy
382     and #$e0
383     bne .putpixel     ; If we have more pixels, put them
384
385     ; To next x coord
386     lda .cxy
387     clc
388     adc #1
389     and #$1f
390     sta .cxy
391
392     ; If we are in "skip" mode, skip :-)
393     lda .skp
394     beq .noskip
395
396 .doSkip
397     ; skip this step (filling with blanks)
398     dec .skp
399     
400 .storeColumn0
401     lda #0
402     jmp .storeColumn
403     
404 .noskip
405     ; Continue font data
406     dec .fsz
407
408     ; Skip just one column if just at the end of character
409     beq .storeColumn0
410     
411     bpl .nextcolumn ; More columns
412     
413     ; Get next character
414     ldx .pos
415     inc .pos
416     lda .data, x
417     bpl .fontdata
418     
419     ; A > 128, skip "A-128" characters
420     and #$7F
421     sta .skp
422     
423     ; If skip == 127 (value=255), signal the end of text data
424     eor #$7F
425     bne .storeColumn0
426     
427     lda #1
428     sta text_endFlag
429     jmp .storeColumn0
430     
431 .fontdata
432     tax
433         lda .font_size,x
434         sta .fsz
435         dec .fsz
436
437         lda .font_pos,x
438         sta .fps
439     
440 .nextcolumn
441     ldx .fps
442     inc .fps
443     lda .font_data, x
444
445 .storeColumn
446     sta .cb
447     ; skip over to putpixel
448
449 .putpixel
450     lda .cxy
451     clc
452     adc #$e0
453         sta .cxy
454     ldx .cxy
455 .base_color
456         lda #1
457         ror .cb
458         rol
459 .dest_label
460         sta .scr,x
461         rts
462
463     text_base_color = .base_color + 1
464     text_dest_addr  = .dest_label + 1
465     
466 ; font
467 .font_size:
468         !byte 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 3, 2, 3, 3, 3, 3, 3, 1, 2, 3, 1
469         !byte 5, 3, 3, 3, 3, 3, 2, 2, 3, 3, 5, 4, 3, 3, 5, 4, 4, 4, 3, 3, 4, 4
470         !byte 1, 3, 4, 3, 5, 5, 5, 4, 5, 4, 4, 3, 4, 4, 7, 4, 4, 4, 2, 1
471 .font_pos:
472         !byte   0,   3,   6,   9,  12,  15,  18,  21,  24,  27,  30,  32,  35
473         !byte  35,  38,  40,  43,  46,  49,  50,  52,   4,  55,  55,  60,  63
474         !byte  65,  68,  71,  73,  75,  78,  78,  33,  83,  86,  89,  94,  98
475         !byte 102, 106, 109, 112, 116,  94, 120, 123, 127, 130, 134, 139, 144
476         !byte 148, 153, 157, 161, 164, 167, 171, 178, 171, 182, 186,   5
477 .font_data:
478         !byte  56, 68, 56, 36,124,  4, 76, 84, 36, 68, 84, 40, 24, 40,124,100
479         !byte  84, 88, 56, 84, 72, 76, 80, 96, 40, 84, 40, 36, 84, 56, 44, 28
480         !byte 124, 36, 24, 24, 36,124, 24, 52, 16, 60, 80, 24, 37, 30,124, 32
481         !byte  28,188,  1,190,124, 24, 36, 60, 32, 28, 32, 28, 24, 36, 24, 63
482         !byte  36, 24, 36, 63, 60, 16, 32, 52, 44,120, 36, 56,  4, 60, 56,  4
483         !byte  56,  4, 56, 57,  6, 56, 44, 52, 36, 12, 48,208, 48, 12,252,164
484         !byte 164, 88,120,132,132, 72,252,132,132,120,252,164,132,252,160,128
485         !byte 120,132,164, 56,252, 32, 32,252,  8,  4,248,252, 48, 80,140,252
486         !byte   4, 4, 252, 64, 60, 64,252, 64, 48,  8,252,120,132,132,132,120
487         !byte 252,144,144, 96,120,132,134,133,120,252,144,144,108, 68,164,164
488         !byte 152,128,252,128,248,  4,  4,248,  4,  8,240,224, 28, 16,224, 28
489         !byte  16,224,204, 48, 48,204,140,148,164,196,  0,  0
490
491
492 text_data
493     !byte (128+5), 41, 10, 28, 29, 63, 63, 63
494     !byte (128+8), 15, 10, 28, 29, 14, 27, 63, 63, 63
495     !byte (128+4), 55, 17, 18, 28, 62, 18, 28, 62, 10
496     !byte (128+8), 27, 14, 10, 21, 21, 34
497     !byte (128+15), 15, 10, 28, 29
498     !byte (128+14), 39, 40, 48, 50
499     !byte (128+21)
500     !byte (128+17), 10, 23, 13, 62, 23, 24, 32
501     !byte (128+8), 28, 24, 22, 14
502     !byte (128+8), 11, 14, 10, 30, 29, 18, 15, 30, 21
503     !byte (128+0), 14, 15, 15, 14, 12, 29, 28, 63, 63, 63
504     !byte (128+16)
505     !byte (128+31)
506     !byte 255
507     !byte       55, 17, 18, 28, 62, 18, 28, 62, 10, 21, 21
508     !byte (128+1), 15, 24, 27, 62, 23, 24, 32
509     !byte (128+8), 11, 34, 14, 63, 63, 63
510     !byte (128+13), 63, 63, 63, 11, 34, 14
511     !byte (128+22)
512     !byte (128+31)
513     !byte (128+15), 11, 34, 62, 39, 48, 54, 38
514     !byte       2, 0, 0, 7, 63, 5, 63, 2, 9
515     !byte       11, 34, 62, 39, 48, 54, 38
516     !byte       2, 0, 0, 7, 63, 5, 63, 2, 9
517     !byte       11, 34, 62, 39, 48, 54, 38
518     !byte       2, 0, 0, 7, 63, 5, 63, 2, 9
519     !byte (128+15)
520     !byte (128+31)
521     !byte 255
522
523 }
524
525
526 : ===========================================================================
527     !source "moireSmooth.asm"
528 : ===========================================================================
529
530     !zone {
531
532 ; Parameters (consts)
533     .scr = moire_scr
534     .buf = moire_buf
535 ; Variables
536     .p1 = moire_temp
537     .o1 = moire_temp + 2
538     .o2 = moire_temp + 4
539     .o3 = moire_temp + 6
540     .o4 = moire_temp + 8
541     .iter = moire_temp + 10
542     .valStartX = moire_temp + 11
543     .deltaX = moire_temp + 12
544     .deltaY = moire_temp + 13
545     .yPos = moire_temp + 14
546     
547 ; moire!:
548     !macro moire {
549         jsr  moire_code
550     }
551
552     !macro add16 .var, .value {
553         lda .var
554         clc
555         adc #<.value
556         sta .var
557         lda .var+1
558         adc #>.value
559         sta .var+1
560     }
561     
562     !macro sto16 .var, .value {
563         lda #<.value
564         sta .var
565         lda #>.value
566         sta .var+1
567     }
568
569 moire_code:
570     
571     lda #0
572     sta .iter
573
574 .loop:
575
576     lda #1
577     sta .deltaX
578     sta .deltaY
579
580     +sto16 .p1, .buf
581
582     lda .iter
583     sta .valStartX
584
585     ldx #15
586 --
587     ldy #15
588 -
589     sta (.p1),y
590     clc
591     adc .deltaX
592     inc .deltaX
593     dey
594     bpl -
595
596     +add16 .p1, $0020
597
598     lda #1
599     sta .deltaX
600     
601     lda .valStartX
602     clc
603     adc .deltaY
604     sta .valStartX
605     inc .deltaY
606
607     dex
608     bpl --
609     
610
611 ; update screen
612     +sto16 .p1, .buf
613     +sto16 .o1, .scr + $01E0
614     +sto16 .o2, .scr + $0200
615     +sto16 .o3, .scr + $01F0
616     +sto16 .o4, .scr + $0210
617
618     lda #15
619     sta .yPos
620 --
621     ldy #15
622 -
623     lda (.p1),y
624     lsr
625     lsr
626     lsr
627     lsr
628     tax
629     lda .tabCol,x
630
631     cmp (.o1),y
632     beq .noCopy
633     sta (.o1),y
634     sta (.o2),y
635     tax
636     tya
637     eor #15
638     tay
639     txa
640     sta (.o3),y
641     sta (.o4),y
642     tya
643     eor #15
644     tay
645 .noCopy:
646     dey
647     bpl -
648
649     +add16 .p1, $0020
650     +add16 .o1, $FFE0
651     +add16 .o2, $0020
652     +add16 .o3, $FFE0
653     +add16 .o4, $0020
654
655     dec .yPos
656     bpl --
657
658     inc .iter
659     lda .iter
660     and #$3f
661     beq +
662     jmp .loop
663 +
664     rts
665
666 .tabCol
667     !byte 0,11,12,15,1,15,12,11
668     !byte 0,11,12,15,1,15,12,11
669     !byte 0,11,12,15,1,15,12,11
670     !byte 0,11,12,15,1,15,12,11
671
672 }
673
674 : ===========================================================================
675
676
677 ; Main loop
678
679 main
680     
681     +giraInit
682     +textInit1
683     lda #63
684     sta gira_pos
685     
686     ; Start show-pos from middle of screen
687     lda #128
688     sta show_pos
689     
690     ; First color is 3
691     lda #3
692     sta text_base_color
693
694     ; Clear old text image
695     lda #0
696     tax
697 -
698     sta text_scr,x
699     inx
700     bne -
701     
702 loop
703     +gira
704     +circ
705     +show
706     +text
707     
708     lda text_pixel
709     eor #$1f
710     bne notChangeTextColor
711     lda text_base_color
712     clc
713     adc #2
714     and #3
715     adc #3
716     sta text_base_color
717     
718 notChangeTextColor
719
720     lda show_pos
721     bne loop
722     
723     lda show_dest_addr+1
724     eor #1
725     sta show_dest_addr+1
726
727     lda text_endFlag
728     beq loop
729
730 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
731 ; End of first text, do moire!
732     jsr clearScr
733     +moire
734
735 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
736 ; End of moire, do ending text
737     +textInit2 $300
738     jsr clearScr
739     
740 ; loop text output
741 -
742     +text
743
744     lda text_pixel
745     eor #$1f
746     bne -
747     lda text_dest_addr+1
748     eor #7
749     sta text_dest_addr+1
750
751     lda text_endFlag
752     beq -
753
754     jmp main
755
756 clearScr    
757     ; Clear screen
758     lda #0
759     tax
760 -
761     sta $200,x
762     sta $300,x
763     sta $400,x
764     sta $500,x
765     inx
766     bne -
767     rts
768