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