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 void g(void){
17 u8 notes_ix = acc & 0x7;
18 if(3&i2)
19 notes_ix += 8;
20 u8 result = ((i1<<8|i0)*notes[notes_ix])>>8; // keep hi byte
21 acc = result;
22 }
23 void main(void){
24 CLEAR (i0)
25 CLEAR (i1)
26 CLEAR (i2)
27 for(;;) {
28 MOV (acc, i2)// "mov mem,mem"
29 MOV (n, acc)// does not exist
30 SL (n)
31 SL (n)
32 MOV (acc, i1)
33 SWAP (acc)
34 AND (acc, 0xf)
35 SR (acc)
36 SR (acc)
37 OR (n, acc)
38
39 MOV (acc, n)
40 CALL (g)
41 SWAP (acc)
42 AND (acc, 0x1)
43 MOV (sample, acc)
44
45 MOV (acc, i2)
46 SL (acc)
47 SL (acc)
48 SL (acc)
49 MOV (tmp_1, acc) // fresh tmp_1:
50 MOV (acc, i1)
51 SWAP (acc)
52 AND (acc, 0xf)
53 SR (acc)
54 OR (acc, tmp_1) // tmp_1 done.
55 XOR (acc, n)
56 CALL (g)
57 SR (acc)
58 AND (acc, i2)
59 SR (acc)
60 AND (acc, 3)
61 ADD (sample, acc)
62
63 { u8 tmp2 = (i2<<5|i1>>3)%3;//TODO
64 MOV (acc, tmp2)
65 ADD (acc, n)
66 CALL (g)
67 } //tmp2
68 SR (acc)
69 SR (acc)
70 MOV (tmp_1, acc) // acc saved in tmp_1; fresh acc
71 MOV (acc, i2)
72 // shift-divide by six
73 // note: i2 is max 0x78; so acc will <= 20. (breaks vor values >=128)
74 SR (acc)
75 ADD (acc, i2)
76 SR (acc)
77 SR (acc)
78 ADD (acc, i2)
79 SR (acc)
80 SR (acc)
81 ADD (acc, i2)
82 SR (acc)
83 SR (acc)
84 SR (acc)
85 // end divide by six
86 AND (acc, tmp_1) // acc restored from tmp_1
87 AND (acc, 3)
88 ADD (sample, acc)
89
90 MOV (acc, n)
91 ADD (acc, 8)
92 { u8 tmp3 = (i2<<6|i1>>2)%3;
93 SUB (acc, tmp3)
94 } // tmp3
95 CALL (g)
96 SR (acc)
97 MOV (tmp_1, acc) // acc saved in tmp_1; fresh acc
98 MOV (acc, i2)
99 // shift-divide by ten
100 // note: i2 is max 0x78; so acc will <= 12.
101 i2++;
102 acc >>= 1;
103 acc += i2;
104
105 acc >>= 1;
106 acc += 0;
107
108 acc >>= 1;
109 acc += 0;
110
111 acc >>= 1;
112 acc += i2;
113
114 acc >>= 1;
115 acc += i2;
116
117 acc >>= 1;
118 acc += 0;
119
120 acc >>= 1;
121 acc += 0;
122
123 acc >>= 1;
124 acc += 0;
125
126 acc >>= 1;
127 i2--;
128
129 // end divide by ten
130 AND (acc, tmp_1) // acc restored from tmp_1
131 AND (acc, 3)
132 ADD (sample, acc)
133
134 MOV (acc, sample)
135 SWAP (acc)
136 putchar(acc);
137
138 INC (i0)
139 i1 += !i0; // ADDC i1
140 i2 += !i1 && !i0; // ADDC i2
141 }
142 }
Imprint / Impressum