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;
abd85947 70 CALL (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
89250744
TG
95 mod3hi = i2>>2;
96 mod3lo = i2<<6|i1>>2;
abd85947
TG
97 CALL (mod3)
98 SUB (acc, n)
99 SUB (acc, 8)
100 NEG (acc)
b2ed0598 101 CALL (g)
5ebb16dc 102 SR (acc)
c004ed39 103 MOV (tmp_1, acc) // acc saved in tmp_1; fresh acc
7cbbb5ba 104 MOV (acc, i2)
c004ed39 105 // shift-divide by ten
b29570af 106 // note: i2 is max 0x78; so acc will <= 12.
1d9b2eb7
TG
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)
28556352 116 SWAP (acc)
1d9b2eb7 117 DEC (i2)
c004ed39
TG
118 // end divide by ten
119 AND (acc, tmp_1) // acc restored from tmp_1
5ebb16dc
TG
120 AND (acc, 3)
121 ADD (sample, acc)
bd4f3b3e 122
be69f3a9
TG
123 MOV (acc, sample)
124 SWAP (acc)
125 putchar(acc);
ac66812d 126
42c6050e
TG
127 INC (i0)
128 i1 += !i0; // ADDC i1
129 i2 += !i1 && !i0; // ADDC i2
fef0e35b 130 }
c395d5c8 131}
Imprint / Impressum