support png output, buffered file writing
[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
12 /* a self-contained dcpu16 core */
13 struct dcpu16 {
14 struct dcpu16_acct_cb *cb_table_; /* list of callbacks to invoke for certain events */
15 size_t cb_table_entries_; /* callback list maintenance */
16 size_t cb_table_allocated_; /* callback list maintenance */
17 unsigned int skip_ : 1; /* skip execution of next instruction */
18 DCPU16_WORD reg_work_[2]; /* work registers for holding literal values while decoding instructions */
19
20 unsigned long long cycle; /* number of cycles this core has executed */
21 DCPU16_WORD reg[8]; /* system registers, a b c x y z i j */
22 DCPU16_WORD pc; /* program counter */
23 DCPU16_WORD sp; /* stack pointer */
24 DCPU16_WORD o; /* overflow */
25 DCPU16_WORD ram[DCPU16_RAM]; /* memory */
26 };
27
28 /* these are used for accounting/watchpointing/modules/&c */
29 typedef unsigned int dcpu16_acct_event;
30 typedef void (dcpu16_ev_cb_t)(struct dcpu16 *, dcpu16_acct_event, DCPU16_WORD, void *);
31 #define DCPU16_ACCT_EV_CYCLE (1<<1)
32 #define DCPU16_ACCT_EV_READ (1<<2)
33 #define DCPU16_ACCT_EV_WRITE (1<<3)
34 #define DCPU16_ACCT_EV_NOP (1<<4)
35 #define DCPU16_ACCT_EV_RESET (1<<5)
36 struct dcpu16_acct_cb {
37 dcpu16_ev_cb_t *fn;
38 void *data;
39 dcpu16_acct_event mask;
40 };
41
42 /* instantiate a new core */
43 struct dcpu16 *dcpu16_new(void);
44
45 /* reset a core to initial state */
46 void dcpu16_reset(struct dcpu16 *);
47
48 /* print the current state of a core */
49 void dcpu16_state_print(struct dcpu16 *);
50
51 /* print the contents of ram from second to third argument */
52 void dcpu16_dump_ram(struct dcpu16 *, DCPU16_WORD, DCPU16_WORD);
53
54 /* print the instruction at the specified address, returns number of words consumed in decoding */
55 DCPU16_WORD dcpu16_disassemble_print(struct dcpu16 *, DCPU16_WORD);
56
57 /* register a callback for an accounting event */
58 int dcpu16_acct_add(struct dcpu16 *, dcpu16_acct_event mask, dcpu16_ev_cb_t *fn, void *data);
59
60 /* execute the next instruction */
61 void dcpu16_step(struct dcpu16 *);
62
63 /* release a core */
64 void dcpu16_delete(struct dcpu16 **);
65
66 /* register callbacks to handle warning and debug messages, default is writing to stderr, may be set to null */
67 void dcpu16_warn_cb_set(void (*)(char *, ...));
68 void dcpu16_trace_cb_set(void (*)(char *, ...));
69
70 #endif /* DCPU16_H_3XXIQQG2 */