v1.7 spec mostly implemented, mostly untested
[dcpu16] / dcpu16.h
1 #ifndef DCPU16_H_3XXIQQG2
2 #define DCPU16_H_3XXIQQG2
3
4 #include <stdlib.h>
5
6 /* the target system's concept of a word */
7 typedef unsigned short DCPU16_WORD;
8
9 /* how much ram the system has */
10 #define DCPU16_RAM 0x10000
11 #define DCPU16_INTERRUPT_QUEUE_SIZE 256
12
13 /* a self-contained dcpu16 core */
14 struct dcpu16 {
15 struct dcpu16_acct_cb *cb_table_; /* list of callbacks to invoke for certain events */
16 size_t cb_table_entries_; /* callback list maintenance */
17 size_t cb_table_allocated_; /* callback list maintenance */
18
19 struct dcpu16_hw *hw_table_; /* list of hardware attached to system */
20 size_t hw_table_entries_; /* hardware list maintenance */
21 size_t hw_table_allocated_; /* hardware list maintenance */
22
23 unsigned int skip_ : 1; /* skip execution of next instruction */
24 unsigned int interrupts_deferred_ : 1; /* queue software interrupts */
25 unsigned int on_fire_ : 1; /* cpu is on fire */
26 DCPU16_WORD reg_work_[2]; /* work registers for holding literal values while decoding instructions */
27 DCPU16_WORD interrupts_[DCPU16_INTERRUPT_QUEUE_SIZE]; /* fifo of pending interrupts */
28 size_t interrupts_head_; /* interrupt queue maintenance */
29 size_t interrupts_tail_; /* interrupt queue maintenance */
30
31 unsigned long long cycle; /* number of cycles this core has executed */
32 DCPU16_WORD reg[8]; /* system registers, a b c x y z i j */
33 DCPU16_WORD pc; /* program counter */
34 DCPU16_WORD sp; /* stack pointer */
35 DCPU16_WORD ex; /* overflow */
36 DCPU16_WORD ia; /* interrupt address */
37 DCPU16_WORD ram[DCPU16_RAM]; /* memory */
38 };
39
40 /* these are used for accounting/watchpointing/modules/&c */
41 typedef unsigned int dcpu16_acct_event;
42 typedef void (dcpu16_ev_cb_t)(struct dcpu16 *, dcpu16_acct_event, DCPU16_WORD, void *);
43 #define DCPU16_ACCT_EV_CYCLE (1<<1)
44 #define DCPU16_ACCT_EV_READ (1<<2)
45 #define DCPU16_ACCT_EV_WRITE (1<<3)
46 #define DCPU16_ACCT_EV_NOP (1<<4)
47 #define DCPU16_ACCT_EV_RESET (1<<5)
48 struct dcpu16_acct_cb {
49 dcpu16_ev_cb_t *fn;
50 void *data;
51 dcpu16_acct_event mask;
52 };
53
54 /* these are used to define hardware attached to the system */
55 struct dcpu16_hw {
56 char *name_;
57 DCPU16_WORD id_l;
58 DCPU16_WORD id_h;
59 DCPU16_WORD ver;
60 DCPU16_WORD mfg_l;
61 DCPU16_WORD mfg_h;
62 void (*int_fn)(struct dcpu16 *, void *);
63 void *data;
64 };
65
66 /* instantiate a new core */
67 struct dcpu16 *dcpu16_new(void);
68
69 /* reset a core to initial state */
70 void dcpu16_reset(struct dcpu16 *);
71
72 /* print the current state of a core */
73 void dcpu16_state_print(struct dcpu16 *);
74
75 /* print the contents of ram from second to third argument */
76 void dcpu16_dump_ram(struct dcpu16 *, DCPU16_WORD, DCPU16_WORD);
77
78 /* print words in buf as asm */
79 DCPU16_WORD dcpu16_mnemonify_buf(DCPU16_WORD *);
80
81 /* print the instruction at the specified address, returns number of words consumed in decoding */
82 DCPU16_WORD dcpu16_disassemble_print(struct dcpu16 *, DCPU16_WORD);
83
84 /* register new 'hardware' device with system */
85 int dcpu16_hw_add(struct dcpu16 *, struct dcpu16_hw *);
86
87 /* register a callback for an accounting event */
88 int dcpu16_acct_add(struct dcpu16 *, dcpu16_acct_event, dcpu16_ev_cb_t *, void *);
89
90 /* execute the next instruction */
91 void dcpu16_step(struct dcpu16 *);
92
93 /* release a core */
94 void dcpu16_delete(struct dcpu16 **);
95
96 /* register callbacks to handle warning and debug messages, default is writing to stderr, may be set to null */
97 void dcpu16_warn_cb_set(void (*)(char *, ...));
98 void dcpu16_trace_cb_set(void (*)(char *, ...));
99
100 #endif /* DCPU16_H_3XXIQQG2 */