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 mod3hi = i2>>2;
138 MOV (acc, i2)
139 SWAP (acc)
140 AND (acc, 0xf0)
141 SL (acc)
142 SL (acc)
143 MOV (mod3lo, acc)
144 MOV (acc, i1)
145 SR (acc)
146 SR (acc)
147 OR (mod3lo, acc)
148
149 CALL (mod3)
150 SUB (acc, n)
151 SUB (acc, 8)
152 NEG (acc)
153 CALL (g)
154 SR (acc)
155 MOV (tmp_1, acc) // acc saved in tmp_1; fresh acc
156 MOV (acc, i2)
157 // shift-divide by ten
158 // note: i2 is max 0x78; so acc will <= 12.
159 INC (i2)
160 SR (acc)
161 ADD (acc, i2)
162 SR (acc)
163 SR (acc)
164 SR (acc)
165 ADD (acc, i2)
166 SR (acc)
167 ADD (acc, i2)
168 SWAP (acc)
169 DEC (i2)
170 // end divide by ten
171 AND (acc, tmp_1) // acc restored from tmp_1
172 AND (acc, 3)
173 ADD (sample, acc)
174
175 MOV (acc, sample)
176 SWAP (acc)
177 putchar(acc);
178
179 INC (i0)
180 i1 += !i0; // ADDC i1
181 i2 += !i1 && !i0; // ADDC i2
182
183 if(i2 == 0x78) break;
184 }
185 }
Imprint / Impressum