Commit MetaInfo

Revision83cd56eb5378ad8d204ec56493194a3978b58caf (tree)
Time2019-05-20 14:08:41
Authoringlorion <homemicro@ingl...>
Commiteringlorion

Log Message

added micrornd app

Change Summary

Incremental Difference

diff -r ba9d1cd9352d -r 83cd56eb5378 apps/micrornd/Makefile
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/apps/micrornd/Makefile Sun May 19 22:08:41 2019 -0700
@@ -0,0 +1,13 @@
1+TARGETS = micrornd.bin
2+
3+all : $(TARGETS)
4+
5+clean :
6+
7+distclean : clean
8+ -rm $(TARGETS)
9+
10+micrornd.bin : micrornd.s
11+ xa -M -bt 57344 -o micrornd.bin micrornd.s
12+
13+.PHONY : all clean distclean
diff -r ba9d1cd9352d -r 83cd56eb5378 apps/micrornd/micrornd.s
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/apps/micrornd/micrornd.s Sun May 19 22:08:41 2019 -0700
@@ -0,0 +1,174 @@
1+ curpos = $80
2+ vidbase = $2000
3+ kbdcol = $d002
4+ kbdrow = $d003
5+ cls = $e003
6+ fontbase = $e800
7+
8+ ;; Locations at which to store temporary values.
9+ ;; These must be consecutive, with no page boundary in between.
10+ t0 = $8
11+ t1 = $9
12+
13+ ;; A location on the zero page. May be the same or different
14+ ;; from the t locations.
15+ zp0 = t0
16+
17+ * = $0000
18+ .byt "HM",0,1
19+ .word _start
20+ .word 32
21+ .word _endhdr
22+ .word _end-_start
23+ .word _start
24+ .word 0
25+
26+_endhdr:
27+ * = $0400
28+_start:
29+ jsr cls
30+ lda #0
31+ tax
32+ jsr setcur
33+ ldx #0
34+main_loop:
35+ jsr rnd
36+ jsr showhex
37+ lda #$20
38+ jsr showchr
39+ lda #$20
40+ jsr showchr
41+ dex
42+ bne main_loop
43+
44+halt:
45+ jmp halt
46+
47+rnd:
48+;;; Returns a pseudorandom number in a.
49+;;; If I've counted correctly, this takes 44 cycles and 29 bytes if
50+;;; micrornd_state is located on the zero page, 56 cycles and 41 bytes
51+;;; if micrornd_state is not on the zero page. In both cases excluding
52+;;; the rts at the end and the sequence to call the procedure. The
53+;;; PRNG uses 4 bytes of state.
54+;;; The four instructions at the beginning can be removed to reduce
55+;;; the size of the state by one byte, the size of the routine by
56+;;; 8 bytes (12 bytes if state is not on the zero page), and the number
57+;;; of clock cycles by 14 (18 if not using zero page).
58+ lda micrornd_state + 1
59+ eor micrornd_state + 3
60+ sta micrornd_state + 1
61+ inc micrornd_state + 3
62+
63+ lda micrornd_state + 1
64+ asl
65+ eor #$d5
66+ adc micrornd_state + 2
67+ sta micrornd_state + 1
68+ lda micrornd_state + 2
69+ adc #1
70+ sta micrornd_state + 2
71+ lda micrornd_state
72+ adc micrornd_state + 1
73+ sta micrornd_state
74+ rts
75+
76+setcur:
77+;;; Sets the cursor position.
78+;;; In:
79+;;; a Row (0..24).
80+;;; x Column (0..39).
81+ ;; We need 320 * row + 8 * col.
82+ ;; = 256 * row + 64 * row + 8 * col.
83+ ;; Save row in curpos + 1.
84+ sta curpos + 1
85+ ;; Set upper 2 bits of curpos to lower 2 bits of row
86+ ;; (other bits of curpos are irrelevant at this point).
87+ lsr
88+ ror
89+ ror
90+ sta curpos
91+ ;; Add curpos + 1 to (row >> 2) to get row + (row >> 2).
92+ rol
93+ and #$3f
94+ clc
95+ adc curpos + 1
96+ sta curpos + 1
97+ ;; Mask out lower 6 bits of curpos, then shift right.
98+ ;; This makes curpos at most #$60.
99+ lda curpos
100+ and #$c0
101+ lsr
102+ sta curpos
103+ ;; Add col << 2.
104+ txa
105+ asl
106+ asl
107+ ;; At this point, a is at most #$9c.
108+ ;; Add curpos. This makes it at most #$fc, so no carry.
109+ adc curpos
110+ ;; Shift left one more time. This can carry.
111+ asl
112+ sta curpos
113+ ;; Add video memory base.
114+ lda #>vidbase
115+ adc curpos + 1
116+ sta curpos + 1
117+ rts
118+
119+showhex:
120+ pha
121+ lsr
122+ lsr
123+ lsr
124+ lsr
125+ tay
126+ lda hexits, y
127+ jsr showchr
128+ pla
129+ and #$f
130+ tay
131+ lda hexits, y
132+ jmp showchr
133+
134+showchr:
135+;;; Displays a character at the current cursor position
136+;;; and updates the cursor position.
137+;;; In:
138+;;; a Character to display.
139+;;; Clobbers y.
140+ asl
141+ rol
142+ rol
143+ tay
144+ rol
145+ and #7
146+ clc
147+ adc #>fontbase
148+ sta $a1
149+ tya
150+ and #$f8
151+ sta $a0
152+ ldy #7
153+showchr_loop:
154+ lda ($a0), y
155+ sta (curpos), y
156+ dey
157+ bpl showchr_loop
158+ lda curpos
159+ ;; Assume carry is clear. Last instr affecting it was
160+ ;; adc #>fontbase, which should not have resulted in carry.
161+ adc #8
162+ sta curpos
163+ lda curpos + 1
164+ adc #0
165+ sta curpos + 1
166+ rts
167+
168+hexits:
169+ .byte "0123456789abcdef"
170+
171+micrornd_state:
172+ .byte 0, 0, 0, 0
173+
174+_end:
Show on old repository browser