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 mod3hi = i2>>3;
99 MOV(acc, i2)
100 SWAP(acc)
101 AND(acc, 0xf0)
102 SL(acc)
103 MOV(mod3lo, acc)
104 MOV(acc, i1)
105 SR(acc)
106 SR(acc)
107 SR(acc)
108 OR(mod3lo, acc)
109 CALL (mod3)
110 ADD (acc, n)
111 CALL (g)
112 SR (acc)
113 SR (acc)
114 MOV (tmp_1, acc) // acc saved in tmp_1; fresh acc
115 MOV (acc, i2)
116 // shift-divide by six
117 // note: i2 is max 0x78; so acc will <= 20. (breaks vor values >=128)
118 SR (acc)
119 ADD (acc, i2)
120 SR (acc)
121 SR (acc)
122 ADD (acc, i2)
123 SR (acc)
124 SR (acc)
125 ADD (acc, i2)
126 SR (acc)
127 SR (acc)
128 SR (acc)
129 // end divide by six
130 AND (acc, tmp_1) // acc restored from tmp_1
131 AND (acc, 3)
132 ADD (sample, acc)
133
134 mod3hi = i2>>2;
135 mod3lo = i2<<6|i1>>2;
136 CALL (mod3)
137 SUB (acc, n)
138 SUB (acc, 8)
139 NEG (acc)
140 CALL (g)
141 SR (acc)
142 MOV (tmp_1, acc) // acc saved in tmp_1; fresh acc
143 MOV (acc, i2)
144 // shift-divide by ten
145 // note: i2 is max 0x78; so acc will <= 12.
146 INC (i2)
147 SR (acc)
148 ADD (acc, i2)
149 SR (acc)
150 SR (acc)
151 SR (acc)
152 ADD (acc, i2)
153 SR (acc)
154 ADD (acc, i2)
155 SWAP (acc)
156 DEC (i2)
157 // end divide by ten
158 AND (acc, tmp_1) // acc restored from tmp_1
159 AND (acc, 3)
160 ADD (sample, acc)
161
162 MOV (acc, sample)
163 SWAP (acc)
164 putchar(acc);
165
166 INC (i0)
167 i1 += !i0; // ADDC i1
168 i2 += !i1 && !i0; // ADDC i2
169
170 if(i2 == 0x78) break;
171 }
172 }
Imprint / Impressum