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