Skip to content

Commit be3b7de

Browse files
committed
rp6502 math-coprocessor
1 parent deb4208 commit be3b7de

File tree

3 files changed

+188
-0
lines changed

3 files changed

+188
-0
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"makefile.configureOnOpen": false
3+
}

include/rp6502.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,29 @@ long __fastcall__ ria_call_long (unsigned char op);
130130
#define RIA_OP_GETLABEL 0x2D
131131
#define RIA_OP_GETFREE 0x2E
132132

133+
/* Math coprocessor opcodes */
134+
#define RIA_OP_MTH_MUL8 0x30
135+
#define RIA_OP_MTH_MUL16 0x31
136+
#define RIA_OP_MTH_MULS16 0x32
137+
#define RIA_OP_MTH_DIV16 0x33
138+
#define RIA_OP_MTH_SQRT32 0x34
139+
#define RIA_OP_MTH_FADD 0x38
140+
#define RIA_OP_MTH_FSUB 0x39
141+
#define RIA_OP_MTH_FMUL 0x3A
142+
#define RIA_OP_MTH_FDIV 0x3B
143+
#define RIA_OP_MTH_FSQRT 0x3C
144+
#define RIA_OP_MTH_FSIN 0x3D
145+
#define RIA_OP_MTH_FCOS 0x3E
146+
#define RIA_OP_MTH_FATAN2 0x3F
147+
#define RIA_OP_MTH_FPOW 0x40
148+
#define RIA_OP_MTH_FLOG 0x41
149+
#define RIA_OP_MTH_FEXP 0x42
150+
#define RIA_OP_MTH_FTOI 0x43
151+
#define RIA_OP_MTH_ITOF 0x44
152+
#define RIA_OP_MTH_DADD 0x48
153+
#define RIA_OP_MTH_DMUL 0x49
154+
#define RIA_OP_MTH_DDIV 0x4A
155+
133156
/* RIA attribute IDs */
134157

135158
#define RIA_ATTR_ERRNO_OPT 0x00
@@ -185,6 +208,39 @@ int __fastcall__ f_setlabel (const char* name);
185208
int __fastcall__ f_getlabel (const char* path, char* label);
186209
int __fastcall__ f_getfree (const char* name, unsigned long* free, unsigned long* total);
187210

211+
/* Math coprocessor API */
212+
213+
/* IEEE 754 single-precision float represented as raw 32-bit pattern */
214+
typedef unsigned long fp32_t;
215+
216+
/* Integer math */
217+
unsigned int __fastcall__ mth_mul8 (unsigned char a, unsigned char b);
218+
unsigned long __fastcall__ mth_mul16 (unsigned int a, unsigned int b);
219+
long __fastcall__ mth_muls16 (int a, int b);
220+
unsigned long __fastcall__ mth_div16 (unsigned long dividend, unsigned int divisor);
221+
unsigned int __fastcall__ mth_sqrt32 (unsigned long n);
222+
223+
/* Float32 conversions */
224+
fp32_t __fastcall__ mth_itof (long i);
225+
long __fastcall__ mth_ftoi (fp32_t x);
226+
227+
/* Float32 arithmetic */
228+
fp32_t __fastcall__ mth_addf (fp32_t a, fp32_t b);
229+
fp32_t __fastcall__ mth_subf (fp32_t a, fp32_t b);
230+
fp32_t __fastcall__ mth_mulf (fp32_t a, fp32_t b);
231+
fp32_t __fastcall__ mth_divf (fp32_t a, fp32_t b);
232+
233+
/* Float32 unary */
234+
fp32_t __fastcall__ mth_sqrtf (fp32_t x);
235+
fp32_t __fastcall__ mth_sinf (fp32_t x);
236+
fp32_t __fastcall__ mth_cosf (fp32_t x);
237+
fp32_t __fastcall__ mth_logf (fp32_t x);
238+
fp32_t __fastcall__ mth_expf (fp32_t x);
239+
240+
/* Float32 binary transcendental */
241+
fp32_t __fastcall__ mth_atan2f (fp32_t y, fp32_t x);
242+
fp32_t __fastcall__ mth_powf (fp32_t base, fp32_t exp);
243+
188244
/* XREG location helpers */
189245

190246
#define xreg_ria_keyboard(...) xreg(0, 0, 0, __VA_ARGS__)

libsrc/rp6502/mth.c

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#include <rp6502.h>
2+
3+
/* ---- Integer ---- */
4+
5+
unsigned int __fastcall__ mth_mul8(unsigned char a, unsigned char b)
6+
{
7+
ria_set_a(b);
8+
ria_push_char(a);
9+
return ria_call_int(RIA_OP_MTH_MUL8);
10+
}
11+
12+
unsigned long __fastcall__ mth_mul16(unsigned int a, unsigned int b)
13+
{
14+
ria_set_ax(b);
15+
ria_push_int(a);
16+
return ria_call_long(RIA_OP_MTH_MUL16);
17+
}
18+
19+
long __fastcall__ mth_muls16(int a, int b)
20+
{
21+
ria_set_ax((unsigned int)b);
22+
ria_push_int((unsigned int)a);
23+
return ria_call_long(RIA_OP_MTH_MULS16);
24+
}
25+
26+
unsigned long __fastcall__ mth_div16(unsigned long dividend, unsigned int divisor)
27+
{
28+
ria_set_ax(divisor);
29+
ria_push_long(dividend);
30+
return ria_call_long(RIA_OP_MTH_DIV16);
31+
}
32+
33+
unsigned int __fastcall__ mth_sqrt32(unsigned long n)
34+
{
35+
ria_set_axsreg(n);
36+
return ria_call_int(RIA_OP_MTH_SQRT32);
37+
}
38+
39+
/* ---- Float32 conversions ---- */
40+
41+
fp32_t __fastcall__ mth_itof(long i)
42+
{
43+
ria_set_axsreg((unsigned long)i);
44+
return (fp32_t)ria_call_long(RIA_OP_MTH_ITOF);
45+
}
46+
47+
long __fastcall__ mth_ftoi(fp32_t x)
48+
{
49+
ria_set_axsreg(x);
50+
return ria_call_long(RIA_OP_MTH_FTOI);
51+
}
52+
53+
/* ---- Float32 arithmetic (binary: a on xstack, b fastcall) ---- */
54+
55+
fp32_t __fastcall__ mth_addf(fp32_t a, fp32_t b)
56+
{
57+
ria_set_axsreg(b);
58+
ria_push_long(a);
59+
return (fp32_t)ria_call_long(RIA_OP_MTH_FADD);
60+
}
61+
62+
fp32_t __fastcall__ mth_subf(fp32_t a, fp32_t b)
63+
{
64+
ria_set_axsreg(b);
65+
ria_push_long(a);
66+
return (fp32_t)ria_call_long(RIA_OP_MTH_FSUB);
67+
}
68+
69+
fp32_t __fastcall__ mth_mulf(fp32_t a, fp32_t b)
70+
{
71+
ria_set_axsreg(b);
72+
ria_push_long(a);
73+
return (fp32_t)ria_call_long(RIA_OP_MTH_FMUL);
74+
}
75+
76+
fp32_t __fastcall__ mth_divf(fp32_t a, fp32_t b)
77+
{
78+
ria_set_axsreg(b);
79+
ria_push_long(a);
80+
return (fp32_t)ria_call_long(RIA_OP_MTH_FDIV);
81+
}
82+
83+
/* ---- Float32 unary ---- */
84+
85+
fp32_t __fastcall__ mth_sqrtf(fp32_t x)
86+
{
87+
ria_set_axsreg(x);
88+
return (fp32_t)ria_call_long(RIA_OP_MTH_FSQRT);
89+
}
90+
91+
fp32_t __fastcall__ mth_sinf(fp32_t x)
92+
{
93+
ria_set_axsreg(x);
94+
return (fp32_t)ria_call_long(RIA_OP_MTH_FSIN);
95+
}
96+
97+
fp32_t __fastcall__ mth_cosf(fp32_t x)
98+
{
99+
ria_set_axsreg(x);
100+
return (fp32_t)ria_call_long(RIA_OP_MTH_FCOS);
101+
}
102+
103+
fp32_t __fastcall__ mth_logf(fp32_t x)
104+
{
105+
ria_set_axsreg(x);
106+
return (fp32_t)ria_call_long(RIA_OP_MTH_FLOG);
107+
}
108+
109+
fp32_t __fastcall__ mth_expf(fp32_t x)
110+
{
111+
ria_set_axsreg(x);
112+
return (fp32_t)ria_call_long(RIA_OP_MTH_FEXP);
113+
}
114+
115+
/* ---- Float32 binary transcendental (y on xstack, x fastcall) ---- */
116+
117+
fp32_t __fastcall__ mth_atan2f(fp32_t y, fp32_t x)
118+
{
119+
ria_set_axsreg(x);
120+
ria_push_long(y);
121+
return (fp32_t)ria_call_long(RIA_OP_MTH_FATAN2);
122+
}
123+
124+
fp32_t __fastcall__ mth_powf(fp32_t base, fp32_t exp)
125+
{
126+
ria_set_axsreg(exp);
127+
ria_push_long(base);
128+
return (fp32_t)ria_call_long(RIA_OP_MTH_FPOW);
129+
}

0 commit comments

Comments
 (0)