aboutsummaryrefslogtreecommitdiffstats
path: root/src/uxn/uxn.c
blob: 53f798396088e494b3fc522b580ddb820ab4c20a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <stdio.h>
#include "uxn.h"

/*
Copyright (u) 2021 Devine Lu Linvega
Copyright (u) 2021 Adrian Siekierka

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE.
*/

/* clang-format off */

static inline void   push8(Stack *s, Uint8 a) {
	s->dat[s->ptr++] = a;
}
static inline Uint8  pop8_keep(Stack *s) {
	return s->dat[--s->kptr];
}
static inline Uint8  pop8_nokeep(Stack *s) {
	return s->dat[--s->ptr];
}
static inline void   devpoke8(Device *d, Uint8 a, Uint8 b) { d->dat[a & 0xf] = b; d->talk(d, a & 0x0f, 1); }
static inline Uint8  devpeek8(Device *d, Uint8 a) { d->talk(d, a & 0x0f, 0); return d->dat[a & 0xf];  }
static inline void   push16(Stack *s, Uint16 a) { push8(s, a >> 8); push8(s, a); }
static inline void   devpoke16(Device *d, Uint8 a, Uint16 b) { devpoke8(d, a, b >> 8); devpoke8(d, a + 1, b); }
static inline Uint16 devpeek16(Device *d, Uint16 a) { return (devpeek8(d, a) << 8) + devpeek8(d, a + 1); }

/* clang-format on */

#pragma mark - Core

int
haltuxn(Uxn *u, char *name, int id)
{
	// txt_printf("Halted: %s#%04x, at 0x%04x\n", name, id, u->ram.ptr);
	u->ram.ptr = 0;
	return 0;
}

IWRAM_CODE
static inline void
opcuxn(Uxn *u, Uint8 instr)
{
#if 1
	// With CPU error checking enabled, the codebase becomes too large to fit in ITCM.
	// Therefore, we take some concessions.
	if (instr & 0x40) {
		u->src = &u->rst;
		u->dst = &u->wst;
	} else {
		u->src = &u->wst;
		u->dst = &u->rst;
	}

	switch (instr & 0xBF) {
#define UXN_SRC (u->src)
#define UXN_DST (u->dst)

#define UXN_KEEP_SYNC {}
#define pop8 pop8_nokeep
#define pop16(s) (pop8((s)) + (pop8((s)) << 8))

#define UXN_OPC(a) (a)
#include "uxn/opcodes.c"
#undef UXN_OPC

#undef pop16
#undef pop8
#undef UXN_KEEP_SYNC

#define UXN_KEEP_SYNC {(*(UXN_SRC)).kptr = (*(UXN_SRC)).ptr;}
#define pop8 pop8_keep
#define pop16(s) (pop8((s)) + (pop8((s)) << 8))

#define UXN_OPC(a) (a | 0x80)
#include "uxn/opcodes.c"
#undef UXN_OPC

#undef pop16
#undef pop8
#undef UXN_KEEP_SYNC

#undef UXN_DST
#undef UXN_SRC
	}
#else
	switch (instr) {
#define UXN_KEEP_SYNC {}
#define pop8 pop8_nokeep
#define pop16(s) (pop8((s)) + (pop8((s)) << 8))

#define UXN_OPC(a) (a)
#define UXN_SRC (&u->wst)
#define UXN_DST (&u->rst)
#include "uxn/opcodes.c"
#undef UXN_DST
#undef UXN_SRC
#undef UXN_OPC

#define UXN_OPC(a) (a | 0x40)
#define UXN_SRC (&u->rst)
#define UXN_DST (&u->wst)
#include "uxn/opcodes.c"
#undef UXN_DST
#undef UXN_SRC
#undef UXN_OPC

#undef pop16
#undef pop8
#undef UXN_KEEP_SYNC

#define UXN_KEEP_SYNC {(*(UXN_SRC)).kptr = (*(UXN_SRC)).ptr;}
#define pop8 pop8_keep
#define pop16(s) (pop8((s)) + (pop8((s)) << 8))

#define UXN_OPC(a) (a | 0x80)
#define UXN_SRC (&u->wst)
#define UXN_DST (&u->rst)
#include "uxn/opcodes.c"
#undef UXN_DST
#undef UXN_SRC
#undef UXN_OPC

#define UXN_OPC(a) (a | 0xC0)
#define UXN_SRC (&u->rst)
#define UXN_DST (&u->wst)
#include "uxn/opcodes.c"
#undef UXN_DST
#undef UXN_SRC
#undef UXN_OPC

#undef pop16
#undef pop8
#undef UXN_KEEP_SYNC
	}
#endif
}

IWRAM_CODE
int
evaluxn(Uxn *u, Uint16 vec)
{
	u->ram.ptr = vec;
	while(u->ram.ptr) {
	    opcuxn(u, u->ram.dat[u->ram.ptr++]);
    }
	return 1;
}

Device *
portuxn(Uxn *u, Uint8 id, char *name, void (*talkfn)(Device *d, Uint8 b0, Uint8 w))
{
	Device *d = &u->dev[id];
	d->addr = id * 0x10;
	d->u = u;
	d->mem = u->ram.dat;
	d->talk = talkfn;
	// txt_printf("Dev:#%02x:0x%04x:%s \n", id, d->addr, name);
	return d;
}