aboutsummaryrefslogtreecommitdiffstats
path: root/src/uxn.h
blob: 6f0140572bd7550438be071ef4374cf00abe81cd (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
#ifndef UXNGBA_UXN_H
#define UXNGBA_UXN_H

#include <stdio.h>

/*
Copyright (c) 2021 Devine Lu Linvega

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.
*/

#define PAGE_PROGRAM 0x0100

typedef struct {
	u8 ptr, kptr, error;
	u8 dat[256];
} Stack;

typedef struct {
	u16 ptr;
	u8 *dat;
} Memory;

typedef struct Device {
	struct Uxn *u;
	u8 addr, dat[16], *mem;
	u16 vector;
	void (*talk)(struct Device *d, u8, u8);
} Device;

typedef struct Uxn {
	Stack wst, rst, *src, *dst;
	Memory ram;
	Device dev[16];
} Uxn;

struct Uxn;

static inline void mempoke8(u8 *m, u16 a, u8 b) { m[a] = b; }
static inline u8 mempeek8(u8 *m, u16 a) { return m[a]; }
static inline void mempoke16(u8 *m, u16 a, u16 b) { mempoke8(m, a, b >> 8); mempoke8(m, a + 1, b); }
static inline u16 mempeek16(u8 *m, u16 a) { return (mempeek8(m, a) << 8) + mempeek8(m, a + 1); }
static inline void devpoke8(Device *d, u8 a, u8 b) { d->dat[a & 0xf] = b; d->talk(d, a & 0x0f, 1); }
static inline u8 devpeek8(Device *d, u8 a) { d->talk(d, a & 0x0f, 0); return d->dat[a & 0xf];  }
static inline void devpoke16(Device *d, u8 a, u16 b) { devpoke8(d, a, b >> 8); devpoke8(d, a + 1, b); }
static inline u16 devpeek16(Device *d, u16 a) { return (devpeek8(d, a) << 8) + devpeek8(d, a + 1); }

int uxn_eval(Uxn *u, u16 vec);
Device *uxn_port(Uxn *u, u8 id, char *name, void (*talkfn)(Device *, u8, u8));
#endif // UXNGBA_UXN_H