new version
[Chiptunes-pms150c.git] / bsv.c
CommitLineData
5ebb16dc
TG
1#include <stdio.h>
2#include "fakeasm.h"
b3a565e1
TG
3typedef unsigned char u8;
4
268e276e 5u8 i0, i1, i2; //MEM
7e02078f 6u8 n; //MEM
5ebb16dc 7u8 sample; //MEM
d17a26fd 8u8 tmp_1; //MEM
268e276e 9u8 acc; //ACC
f3768c26 10
c1873bed
TG
11u8 notes[] = {
12 0x84, 0x9d, 0xb0, 0x69, 0x9d, 0x84, 0x69, 0x58,
13 0x75, 0x8c, 0xb0, 0x69, 0x8c, 0x75, 0x69, 0x58
14};
c320426f 15
837c7d6c
TG
16u8 mod3hi, mod3lo;
17void mod3(void) {
18 acc = (mod3hi<<8|mod3lo) % 3;
19}
20
b2ed0598
TG
21void g(void){
22 u8 notes_ix = acc & 0x7;
ac66812d
TG
23 if(3&i2)
24 notes_ix += 8;
fb819dd9 25 u8 result = ((i1<<8|i0)*notes[notes_ix])>>8; // keep hi byte
268e276e 26 acc = result;
c395d5c8 27}
f3768c26 28void main(void){
5ebb16dc
TG
29 CLEAR (i0)
30 CLEAR (i1)
31 CLEAR (i2)
f3768c26 32 for(;;) {
fc496164
TG
33 MOV (acc, i2)// "mov mem,mem"
34 MOV (n, acc)// does not exist
5ebb16dc
TG
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)
bd4f3b3e 43
b2ed0598
TG
44 MOV (acc, n)
45 CALL (g)
5ebb16dc
TG
46 SWAP (acc)
47 AND (acc, 0x1)
48 MOV (sample, acc)
bd4f3b3e 49
d17a26fd
TG
50 MOV (acc, i2)
51 SL (acc)
52 SL (acc)
53 SL (acc)
f5823e3f 54 MOV (tmp_1, acc) // fresh tmp_1:
d17a26fd
TG
55 MOV (acc, i1)
56 SWAP (acc)
57 AND (acc, 0xf)
58 SR (acc)
59 OR (acc, tmp_1) // tmp_1 done.
b2ed0598
TG
60 XOR (acc, n)
61 CALL (g)
5ebb16dc
TG
62 SR (acc)
63 AND (acc, i2)
64 SR (acc)
65 AND (acc, 3)
66 ADD (sample, acc)
bd4f3b3e 67
837c7d6c
TG
68 mod3hi = i2>>3;
69 mod3lo = i2<<5|i1>>3;
70 mod3();
b2ed0598
TG
71 ADD (acc, n)
72 CALL (g)
5ebb16dc
TG
73 SR (acc)
74 SR (acc)
c004ed39
TG
75 MOV (tmp_1, acc) // acc saved in tmp_1; fresh acc
76 MOV (acc, i2)
77 // shift-divide by six
2ae98f0b 78 // note: i2 is max 0x78; so acc will <= 20. (breaks vor values >=128)
7e0dee10
TG
79 SR (acc)
80 ADD (acc, i2)
7e0dee10 81 SR (acc)
7e0dee10
TG
82 SR (acc)
83 ADD (acc, i2)
7e0dee10 84 SR (acc)
7e0dee10
TG
85 SR (acc)
86 ADD (acc, i2)
7e0dee10 87 SR (acc)
7e0dee10 88 SR (acc)
7e0dee10 89 SR (acc)
c004ed39
TG
90 // end divide by six
91 AND (acc, tmp_1) // acc restored from tmp_1
5ebb16dc
TG
92 AND (acc, 3)
93 ADD (sample, acc)
bd4f3b3e 94
b2ed0598 95 MOV (acc, n)
2d826e28 96 ADD (acc, 8)
837c7d6c 97 { u8 tmp3 = ((i2<<6|i1>>2)&0x1fff)%3;
b2ed0598
TG
98 SUB (acc, tmp3)
99 } // tmp3
b2ed0598 100 CALL (g)
5ebb16dc 101 SR (acc)
c004ed39 102 MOV (tmp_1, acc) // acc saved in tmp_1; fresh acc
7cbbb5ba 103 MOV (acc, i2)
c004ed39 104 // shift-divide by ten
b29570af 105 // note: i2 is max 0x78; so acc will <= 12.
1d9b2eb7
TG
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)
28556352 115 SWAP (acc)
1d9b2eb7 116 DEC (i2)
c004ed39
TG
117 // end divide by ten
118 AND (acc, tmp_1) // acc restored from tmp_1
5ebb16dc
TG
119 AND (acc, 3)
120 ADD (sample, acc)
bd4f3b3e 121
be69f3a9
TG
122 MOV (acc, sample)
123 SWAP (acc)
124 putchar(acc);
ac66812d 125
42c6050e
TG
126 INC (i0)
127 i1 += !i0; // ADDC i1
128 i2 += !i1 && !i0; // ADDC i2
fef0e35b 129 }
c395d5c8 130}
Imprint / Impressum