new version
[Chiptunes-pms150c.git] / bsv.c
1 #include <stdio.h>
2 #include "fakeasm.h"
3 typedef unsigned char u8;
4
5 u8 i0, i1, i2; //MEM
6 u8 n; //MEM
7 u8 sample; //MEM
8 u8 tmp_1; //MEM
9 u8 acc; //ACC
10
11 u8 notes[] = {
12 0x84, 0x9d, 0xb0, 0x69, 0x9d, 0x84, 0x69, 0x58,
13 0x75, 0x8c, 0xb0, 0x69, 0x8c, 0x75, 0x69, 0x58
14 };
15
16 u8 mod3hi, mod3lo; //MEM
17 //todo: can mod3hi be aliased to tmp_1?
18 void mod3(void) {
19 MOV (acc,mod3hi)
20 ADD (mod3lo,acc) // mod3lo = hi+lo
21 CLEAR (acc)
22 ADDC0 (acc) // mod3hi, 1bit
23 SWAP (acc)
24 MOV (mod3hi, acc)
25
26 MOV (acc, mod3lo)
27 SWAP (acc)
28 AND (acc, 0xf) // (mod3lo>>4)
29 XCH (mod3lo) // acc=mod3lo, mod3lo=mod3lo>>4
30 AND (acc, 0xF) // acc=mod3lo&0xf, mod3lo=mod3lo>>4
31 ADD (acc, mod3lo) // (mod3lo & 0xF)
32 ADD (acc, mod3hi)
33 MOV (mod3lo, acc)
34
35 AND (acc, 0x3) // acc = (mod3lo & 0x3)
36 SR (mod3lo)
37 SR (mod3lo) // (mod3lo >> 2)
38 ADD (acc, mod3lo)
39 MOV (mod3lo, acc)
40
41 AND (acc, 0x3) // acc = (mod3lo & 0x3)
42 SR (mod3lo)
43 SR (mod3lo) // (mod3lo >> 2)
44 ADD (acc, mod3lo)
45
46 if (acc > 2) { // TODO: acc in [0,1,2,3,4]
47 SUB (acc,3)
48 }
49 }
50
51 void g(void){
52 u8 notes_ix = acc & 0x7;
53 if(3&i2)
54 notes_ix += 8;
55 u8 result = ((i1<<8|i0)*notes[notes_ix])>>8; // keep hi byte
56 acc = result;
57 }
58 void main(void){
59 CLEAR (i0)
60 CLEAR (i1)
61 CLEAR (i2)
62 for(;;) {
63 MOV (acc, i2)// "mov mem,mem"
64 MOV (n, acc)// does not exist
65 SL (n)
66 SL (n)
67 MOV (acc, i1)
68 SWAP (acc)
69 AND (acc, 0xf)
70 SR (acc)
71 SR (acc)
72 OR (n, acc)
73
74 MOV (acc, n)
75 CALL (g)
76 SWAP (acc)
77 AND (acc, 0x1)
78 MOV (sample, acc)
79
80 MOV (acc, i2)
81 SL (acc)
82 SL (acc)
83 SL (acc)
84 MOV (tmp_1, acc) // fresh tmp_1:
85 MOV (acc, i1)
86 SWAP (acc)
87 AND (acc, 0xf)
88 SR (acc)
89 OR (acc, tmp_1) // tmp_1 done.
90 XOR (acc, n)
91 CALL (g)
92 SR (acc)
93 AND (acc, i2)
94 SR (acc)
95 AND (acc, 3)
96 ADD (sample, acc)
97
98 MOV (acc, i2)
99 MOV (mod3hi, acc)
100 SR (mod3hi)
101 SR (mod3hi)
102 SR (mod3hi)
103 SWAP (acc)
104 AND (acc, 0xf0)
105 SL (acc)
106 MOV (mod3lo, acc)
107 MOV (acc, i1)
108 SR (acc)
109 SR (acc)
110 SR (acc)
111 OR (mod3lo, acc)
112 CALL (mod3)
113 ADD (acc, n)
114 CALL (g)
115 SR (acc)
116 SR (acc)
117 MOV (tmp_1, acc) // acc saved in tmp_1; fresh acc
118 MOV (acc, i2)
119 // shift-divide by six
120 // note: i2 is max 0x78; so acc will <= 20. (breaks vor values >=128)
121 SR (acc)
122 ADD (acc, i2)
123 SR (acc)
124 SR (acc)
125 ADD (acc, i2)
126 SR (acc)
127 SR (acc)
128 ADD (acc, i2)
129 SR (acc)
130 SR (acc)
131 SR (acc)
132 // end divide by six
133 AND (acc, tmp_1) // acc restored from tmp_1
134 AND (acc, 3)
135 ADD (sample, acc)
136
137 MOV (acc, i2)
138 MOV (mod3hi, acc)
139 SR (mod3hi)
140 SR (mod3hi)
141 SWAP (acc)
142 AND (acc, 0xf0)
143 SL (acc)
144 SL (acc)
145 MOV (mod3lo, acc)
146 MOV (acc, i1)
147 SR (acc)
148 SR (acc)
149 OR (mod3lo, acc)
150 CALL (mod3)
151 SUB (acc, n)
152 SUB (acc, 8)
153 NEG (acc)
154 CALL (g)
155 SR (acc)
156 MOV (tmp_1, acc) // acc saved in tmp_1; fresh acc
157 MOV (acc, i2)
158 // shift-divide by ten
159 // note: i2 is max 0x78; so acc will <= 12.
160 INC (i2)
161 SR (acc)
162 ADD (acc, i2)
163 SR (acc)
164 SR (acc)
165 SR (acc)
166 ADD (acc, i2)
167 SR (acc)
168 ADD (acc, i2)
169 SWAP (acc)
170 DEC (i2)
171 // end divide by ten
172 AND (acc, tmp_1) // acc restored from tmp_1
173 AND (acc, 3)
174 ADD (sample, acc)
175
176 MOV (acc, sample)
177 SWAP (acc)
178 putchar(acc);
179
180 INC (i0)
181 i1 += !i0; // ADDC i1
182 i2 += !i1 && !i0; // ADDC i2
183
184 if(i2 == 0x78) break;
185 }
186 }
Imprint / Impressum