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
88d59159 16u8 mod3hi, mod3lo; //MEM
837c7d6c 17void mod3(void) {
5b28e9da
TG
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)
837c7d6c 24
5b28e9da
TG
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;
88d59159
TG
30}
31
b2ed0598
TG
32void g(void){
33 u8 notes_ix = acc & 0x7;
ac66812d
TG
34 if(3&i2)
35 notes_ix += 8;
fb819dd9 36 u8 result = ((i1<<8|i0)*notes[notes_ix])>>8; // keep hi byte
268e276e 37 acc = result;
c395d5c8 38}
f3768c26 39void main(void){
5ebb16dc
TG
40 CLEAR (i0)
41 CLEAR (i1)
42 CLEAR (i2)
f3768c26 43 for(;;) {
fc496164
TG
44 MOV (acc, i2)// "mov mem,mem"
45 MOV (n, acc)// does not exist
5ebb16dc
TG
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)
bd4f3b3e 54
b2ed0598
TG
55 MOV (acc, n)
56 CALL (g)
5ebb16dc
TG
57 SWAP (acc)
58 AND (acc, 0x1)
59 MOV (sample, acc)
bd4f3b3e 60
d17a26fd
TG
61 MOV (acc, i2)
62 SL (acc)
63 SL (acc)
64 SL (acc)
f5823e3f 65 MOV (tmp_1, acc) // fresh tmp_1:
d17a26fd
TG
66 MOV (acc, i1)
67 SWAP (acc)
68 AND (acc, 0xf)
69 SR (acc)
70 OR (acc, tmp_1) // tmp_1 done.
b2ed0598
TG
71 XOR (acc, n)
72 CALL (g)
5ebb16dc
TG
73 SR (acc)
74 AND (acc, i2)
75 SR (acc)
76 AND (acc, 3)
77 ADD (sample, acc)
bd4f3b3e 78
837c7d6c
TG
79 mod3hi = i2>>3;
80 mod3lo = i2<<5|i1>>3;
abd85947 81 CALL (mod3)
b2ed0598
TG
82 ADD (acc, n)
83 CALL (g)
5ebb16dc
TG
84 SR (acc)
85 SR (acc)
c004ed39
TG
86 MOV (tmp_1, acc) // acc saved in tmp_1; fresh acc
87 MOV (acc, i2)
88 // shift-divide by six
2ae98f0b 89 // note: i2 is max 0x78; so acc will <= 20. (breaks vor values >=128)
7e0dee10
TG
90 SR (acc)
91 ADD (acc, i2)
7e0dee10 92 SR (acc)
7e0dee10
TG
93 SR (acc)
94 ADD (acc, i2)
7e0dee10 95 SR (acc)
7e0dee10
TG
96 SR (acc)
97 ADD (acc, i2)
7e0dee10 98 SR (acc)
7e0dee10 99 SR (acc)
7e0dee10 100 SR (acc)
c004ed39
TG
101 // end divide by six
102 AND (acc, tmp_1) // acc restored from tmp_1
5ebb16dc
TG
103 AND (acc, 3)
104 ADD (sample, acc)
bd4f3b3e 105
89250744
TG
106 mod3hi = i2>>2;
107 mod3lo = i2<<6|i1>>2;
abd85947
TG
108 CALL (mod3)
109 SUB (acc, n)
110 SUB (acc, 8)
111 NEG (acc)
b2ed0598 112 CALL (g)
5ebb16dc 113 SR (acc)
c004ed39 114 MOV (tmp_1, acc) // acc saved in tmp_1; fresh acc
7cbbb5ba 115 MOV (acc, i2)
c004ed39 116 // shift-divide by ten
b29570af 117 // note: i2 is max 0x78; so acc will <= 12.
1d9b2eb7
TG
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)
28556352 127 SWAP (acc)
1d9b2eb7 128 DEC (i2)
c004ed39
TG
129 // end divide by ten
130 AND (acc, tmp_1) // acc restored from tmp_1
5ebb16dc
TG
131 AND (acc, 3)
132 ADD (sample, acc)
bd4f3b3e 133
be69f3a9
TG
134 MOV (acc, sample)
135 SWAP (acc)
136 putchar(acc);
ac66812d 137
42c6050e
TG
138 INC (i0)
139 i1 += !i0; // ADDC i1
140 i2 += !i1 && !i0; // ADDC i2
fef0e35b 141 }
c395d5c8 142}
Imprint / Impressum