fixed bug with instruction decoding in dcpu, fixed bugs in display
[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 extern const char * const dcpu16_reg_names[];
14 enum dcpu16_register_indexes {
15 DCPU16_REG_A = 0,
16 DCPU16_REG_B,
17 DCPU16_REG_C,
18 DCPU16_REG_X,
19 DCPU16_REG_Y,
20 DCPU16_REG_Z,
21 DCPU16_REG_I,
22 DCPU16_REG_J,
23 DCPU16_REG_PC,
24 DCPU16_REG_SP,
25 DCPU16_REG_EX,
26 DCPU16_REG_IA,
27 DCPU16_REG__NUM
28 };
29
30 /* a self-contained dcpu16 core */
31 struct dcpu16 {
32 void (*warn_cb_)(char *fmt, ...);
33 void (*trace_cb_)(char *fmt, ...);
34
35 struct dcpu16_acct_cb *cb_table_; /* list of callbacks to invoke for certain events */
36 size_t cb_table_entries_; /* callback list maintenance */
37 size_t cb_table_allocated_; /* callback list maintenance */
38
39 struct dcpu16_hw *hw_table_; /* list of hardware attached to system */
40 size_t hw_table_entries_; /* hardware list maintenance */
41 size_t hw_table_allocated_; /* hardware list maintenance */
42
43 unsigned int skip_ : 1; /* skip execution of next instruction */
44 unsigned int interrupts_deferred_ : 1; /* queue software interrupts */
45 unsigned int on_fire_ : 1; /* cpu is on fire */
46 DCPU16_WORD reg_work_[2]; /* work registers for holding literal values while decoding instructions */
47 DCPU16_WORD interrupts_[DCPU16_INTERRUPT_QUEUE_SIZE]; /* fifo of pending interrupts */
48 size_t interrupts_head_; /* interrupt queue maintenance */
49 size_t interrupts_tail_; /* interrupt queue maintenance */
50
51 unsigned long long cycle; /* number of cycles this core has executed */
52 DCPU16_WORD reg[DCPU16_REG__NUM]; /* system registers, a b c x y z i j */
53 DCPU16_WORD ram[DCPU16_RAM]; /* memory */
54 };
55
56 /* these are used for accounting/watchpointing/modules/&c */
57 typedef unsigned int dcpu16_acct_event;
58 typedef void (dcpu16_ev_cb_t)(struct dcpu16 *, dcpu16_acct_event, DCPU16_WORD, void *);
59 #define DCPU16_ACCT_EV_CYCLE (1<<1)
60 #define DCPU16_ACCT_EV_READ (1<<2)
61 #define DCPU16_ACCT_EV_WRITE (1<<3)
62 #define DCPU16_ACCT_EV_NOP (1<<4)
63 #define DCPU16_ACCT_EV_RESET (1<<5)
64 struct dcpu16_acct_cb {
65 dcpu16_ev_cb_t *fn;
66 void *data;
67 dcpu16_acct_event mask;
68 };
69
70 typedef void (dcpu16_hw_signal_t)(struct dcpu16 *, void *);
71 /* these are used to define hardware attached to the system */
72 struct dcpu16_hw {
73 struct dcpu16 *vm; /* which system do I belong to */
74 char *name_;
75
76 DCPU16_WORD id_l;
77 DCPU16_WORD id_h;
78 DCPU16_WORD ver;
79 DCPU16_WORD mfg_l;
80 DCPU16_WORD mfg_h;
81
82 dcpu16_hw_signal_t *hwi;
83 dcpu16_hw_signal_t *cycle;
84 dcpu16_hw_signal_t *reset;
85 void *data;
86 };
87
88 /* instantiate a new core */
89 struct dcpu16 *dcpu16_new(void);
90
91 /* reset a core to initial state */
92 void dcpu16_reset(struct dcpu16 *);
93
94 /* print the current state of a core */
95 void dcpu16_state_print(struct dcpu16 *);
96
97 /* print the contents of ram from second to third argument */
98 void dcpu16_dump_ram(struct dcpu16 *, DCPU16_WORD, DCPU16_WORD);
99
100 /* print words in buf as asm */
101 DCPU16_WORD dcpu16_mnemonify_buf(DCPU16_WORD *);
102
103 /* print the instruction at the specified address, returns number of words consumed in decoding */
104 DCPU16_WORD dcpu16_disassemble_print(struct dcpu16 *, DCPU16_WORD);
105
106 /* register new 'hardware' device with system */
107 int dcpu16_hw_add(struct dcpu16 *, struct dcpu16_hw *);
108
109 /* register a callback for an accounting event */
110 int dcpu16_acct_add(struct dcpu16 *, dcpu16_acct_event, dcpu16_ev_cb_t *, void *);
111
112 /* execute the next instruction */
113 void dcpu16_step(struct dcpu16 *);
114
115 /* release a core */
116 void dcpu16_delete(struct dcpu16 **);
117
118 int dcpu16_interrupt(struct dcpu16 *, DCPU16_WORD);
119
120 /* register callbacks to handle warning and debug messages, default is writing to stderr, may be set to null */
121 void dcpu16_warn_cb_set(void (*)(char *, ...));
122 void dcpu16_trace_cb_set(void (*)(char *, ...));
123
124 #endif /* DCPU16_H_3XXIQQG2 */