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 CALL (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 mod3hi = i2>>2;
96 mod3lo = i2<<6|i1>>2;
97 CALL (mod3)
98 SUB (acc, n)
99 SUB (acc, 8)
100 NEG (acc)
101 CALL (g)
102 SR (acc)
103 MOV (tmp_1, acc) // acc saved in tmp_1; fresh acc
104 MOV (acc, i2)
105 // shift-divide by ten
106 // note: i2 is max 0x78; so acc will <= 12.
107 INC (i2)
108 SR (acc)
109 ADD (acc, i2)
110 SR (acc)
111 SR (acc)
112 SR (acc)
113 ADD (acc, i2)
114 SR (acc)
115 ADD (acc, i2)
116 SWAP (acc)
117 DEC (i2)
118 // end divide by ten
119 AND (acc, tmp_1) // acc restored from tmp_1
120 AND (acc, 3)
121 ADD (sample, acc)
122
123 MOV (acc, sample)
124 SWAP (acc)
125 putchar(acc);
126
127 INC (i0)
128 i1 += !i0; // ADDC i1
129 i2 += !i1 && !i0; // ADDC i2
130 }
131 }
Imprint / Impressum