new version
[Chiptunes-pms150c.git] / bsv.c
CommitLineData
5ebb16dc
TG
1#include <stdio.h>
2#include "fakeasm.h"
b3a565e1
TG
3typedef unsigned char u8;
4
19c9f57b 5u8 notes[16]; //MEM
268e276e 6u8 i0, i1, i2; //MEM
7e02078f 7u8 n; //MEM
5ebb16dc 8u8 sample; //MEM
d17a26fd 9u8 tmp_1; //MEM
268e276e 10u8 acc; //ACC
f3768c26 11
88d59159 12u8 mod3hi, mod3lo; //MEM
6494a942 13//todo: can mod3hi be aliased to tmp_1?
837c7d6c 14void mod3(void) {
6494a942
TG
15 MOV (acc,mod3hi)
16 ADD (mod3lo,acc) // mod3lo = hi+lo
17 CLEAR (acc)
18 ADDC0 (acc) // mod3hi, 1bit
19 SWAP (acc)
20 MOV (mod3hi, acc)
837c7d6c 21
6494a942
TG
22 MOV (acc, mod3lo)
23 SWAP (acc)
24 AND (acc, 0xf) // (mod3lo>>4)
25 XCH (mod3lo) // acc=mod3lo, mod3lo=mod3lo>>4
26 AND (acc, 0xF) // acc=mod3lo&0xf, mod3lo=mod3lo>>4
27 ADD (acc, mod3lo) // (mod3lo & 0xF)
28 ADD (acc, mod3hi)
29 MOV (mod3lo, acc)
bf20ae1c 30
6494a942
TG
31 AND (acc, 0x3) // acc = (mod3lo & 0x3)
32 SR (mod3lo)
33 SR (mod3lo) // (mod3lo >> 2)
34 ADD (acc, mod3lo)
35 MOV (mod3lo, acc)
5f0928cc 36
6494a942
TG
37 AND (acc, 0x3) // acc = (mod3lo & 0x3)
38 SR (mod3lo)
39 SR (mod3lo) // (mod3lo >> 2)
40 ADD (acc, mod3lo)
5f0928cc 41
6494a942 42 SUB (acc,3)
1ee69112
TG
43 if (flag_c){ // T0SN FLAG.C (0x00.1) -- skip if carry clear
44 ADD (acc,3)
6494a942 45 }
88d59159
TG
46}
47
b2ed0598
TG
48void g(void){
49 u8 notes_ix = acc & 0x7;
ac66812d
TG
50 if(3&i2)
51 notes_ix += 8;
fb819dd9 52 u8 result = ((i1<<8|i0)*notes[notes_ix])>>8; // keep hi byte
268e276e 53 acc = result;
c395d5c8 54}
f3768c26 55void main(void){
5ebb16dc
TG
56 CLEAR (i0)
57 CLEAR (i1)
58 CLEAR (i2)
19c9f57b
TG
59
60 //rom is not mmapped; must load into ram first
61 MOV (acc, 0x84)
62 MOV (notes[0x0], acc)
63 MOV (notes[0x5], acc)
64 MOV (acc, 0x9d)
65 MOV (notes[0x1], acc)
66 MOV (notes[0x4], acc)
67 MOV (acc, 0xb0)
68 MOV (notes[0x2], acc)
69 MOV (notes[0xA], acc)
70 MOV (acc, 0x69)
71 MOV (notes[0x3], acc)
72 MOV (notes[0x6], acc)
73 MOV (notes[0xB], acc)
74 MOV (notes[0xE], acc)
75 MOV (acc, 0x58)
76 MOV (notes[0x7], acc)
77 MOV (notes[0xF], acc)
78 MOV (acc, 0x75)
79 MOV (notes[0x8], acc)
80 MOV (notes[0xD], acc)
81 MOV (acc, 0x8c)
82 MOV (notes[0x9], acc)
83 MOV (notes[0xC], acc)
84
f3768c26 85 for(;;) {
fc496164
TG
86 MOV (acc, i2)// "mov mem,mem"
87 MOV (n, acc)// does not exist
5ebb16dc
TG
88 SL (n)
89 SL (n)
90 MOV (acc, i1)
91 SWAP (acc)
92 AND (acc, 0xf)
93 SR (acc)
94 SR (acc)
95 OR (n, acc)
bd4f3b3e 96
b2ed0598
TG
97 MOV (acc, n)
98 CALL (g)
5ebb16dc
TG
99 SWAP (acc)
100 AND (acc, 0x1)
101 MOV (sample, acc)
bd4f3b3e 102
d17a26fd
TG
103 MOV (acc, i2)
104 SL (acc)
105 SL (acc)
106 SL (acc)
f5823e3f 107 MOV (tmp_1, acc) // fresh tmp_1:
d17a26fd
TG
108 MOV (acc, i1)
109 SWAP (acc)
110 AND (acc, 0xf)
111 SR (acc)
112 OR (acc, tmp_1) // tmp_1 done.
b2ed0598
TG
113 XOR (acc, n)
114 CALL (g)
5ebb16dc
TG
115 SR (acc)
116 AND (acc, i2)
117 SR (acc)
118 AND (acc, 3)
119 ADD (sample, acc)
bd4f3b3e 120
774dc938
TG
121 MOV (acc, i2)
122 MOV (mod3hi, acc)
123 SR (mod3hi)
124 SR (mod3hi)
125 SR (mod3hi)
126 SWAP (acc)
127 AND (acc, 0xf0)
128 SL (acc)
129 MOV (mod3lo, acc)
130 MOV (acc, i1)
131 SR (acc)
132 SR (acc)
133 SR (acc)
134 OR (mod3lo, acc)
abd85947 135 CALL (mod3)
b2ed0598
TG
136 ADD (acc, n)
137 CALL (g)
5ebb16dc
TG
138 SR (acc)
139 SR (acc)
c004ed39
TG
140 MOV (tmp_1, acc) // acc saved in tmp_1; fresh acc
141 MOV (acc, i2)
142 // shift-divide by six
2ae98f0b 143 // note: i2 is max 0x78; so acc will <= 20. (breaks vor values >=128)
7e0dee10
TG
144 SR (acc)
145 ADD (acc, i2)
7e0dee10 146 SR (acc)
7e0dee10
TG
147 SR (acc)
148 ADD (acc, i2)
7e0dee10 149 SR (acc)
7e0dee10
TG
150 SR (acc)
151 ADD (acc, i2)
7e0dee10 152 SR (acc)
7e0dee10 153 SR (acc)
7e0dee10 154 SR (acc)
c004ed39
TG
155 // end divide by six
156 AND (acc, tmp_1) // acc restored from tmp_1
5ebb16dc
TG
157 AND (acc, 3)
158 ADD (sample, acc)
bd4f3b3e 159
8ed39eb1 160 MOV (acc, i2)
5ef6ccab
TG
161 MOV (mod3hi, acc)
162 SR (mod3hi)
163 SR (mod3hi)
8ed39eb1
TG
164 SWAP (acc)
165 AND (acc, 0xf0)
166 SL (acc)
167 SL (acc)
168 MOV (mod3lo, acc)
169 MOV (acc, i1)
170 SR (acc)
171 SR (acc)
172 OR (mod3lo, acc)
abd85947
TG
173 CALL (mod3)
174 SUB (acc, n)
175 SUB (acc, 8)
176 NEG (acc)
b2ed0598 177 CALL (g)
5ebb16dc 178 SR (acc)
c004ed39 179 MOV (tmp_1, acc) // acc saved in tmp_1; fresh acc
7cbbb5ba 180 MOV (acc, i2)
c004ed39 181 // shift-divide by ten
b29570af 182 // note: i2 is max 0x78; so acc will <= 12.
1d9b2eb7
TG
183 INC (i2)
184 SR (acc)
185 ADD (acc, i2)
186 SR (acc)
187 SR (acc)
188 SR (acc)
189 ADD (acc, i2)
190 SR (acc)
191 ADD (acc, i2)
28556352 192 SWAP (acc)
1d9b2eb7 193 DEC (i2)
c004ed39
TG
194 // end divide by ten
195 AND (acc, tmp_1) // acc restored from tmp_1
5ebb16dc
TG
196 AND (acc, 3)
197 ADD (sample, acc)
bd4f3b3e 198
be69f3a9
TG
199 MOV (acc, sample)
200 SWAP (acc)
201 putchar(acc);
ac66812d 202
42c6050e
TG
203 INC (i0)
204 i1 += !i0; // ADDC i1
205 i2 += !i1 && !i0; // ADDC i2
e465ced0
TG
206
207 if(i2 == 0x78) break;
fef0e35b 208 }
c395d5c8 209}
Imprint / Impressum