typo, missing semicolon
[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 long long cycle_; /* number of cycles this core has executed */
44 unsigned int skip_ : 1; /* skip execution of next instruction */
45 unsigned int interrupts_deferred_ : 1; /* queue software interrupts */
46 unsigned int on_fire_ : 1; /* cpu is on fire */
47 DCPU16_WORD reg_work_[2]; /* work registers for holding literal values while decoding instructions */
48 DCPU16_WORD interrupts_[DCPU16_INTERRUPT_QUEUE_SIZE]; /* fifo of pending interrupts */
49 size_t interrupts_head_; /* interrupt queue maintenance */
50 size_t interrupts_tail_; /* interrupt queue maintenance */
51
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_REG_READ (1<<4)
63 #define DCPU16_ACCT_EV_REG_WRITE (1<<5)
64 #define DCPU16_ACCT_EV_NOP (1<<6)
65 #define DCPU16_ACCT_EV_RESET (1<<7)
66 struct dcpu16_acct_cb {
67 dcpu16_ev_cb_t *fn; /* call this */
68 void *data; /* also mention this */
69 dcpu16_acct_event mask; /* when (mask & event) is true and */
70 DCPU16_WORD addr_l, /* addr is this or higher and */
71 addr_h; /* addr is this or lower */
72 };
73
74 typedef void (dcpu16_hw_signal_t)(struct dcpu16 *, struct dcpu16_hw *);
75 /* this structure defines a specific instance of this type of 'hardware' */
76 struct dcpu16_hw {
77 struct dcpu16 *vm; /* which system do I belong to */
78 struct dcpu16_hw_module *mod; /* whence I came */
79 void *data; /* per-instance data */
80 };
81
82 /* human-readable text describing hw module control operations, for convenience's sake */
83 struct dcpu16_hw_ctl_cmd {
84 char *command;
85 char *data_in_type;
86 char *data_out_type;
87 char *description;
88 };
89
90 typedef int (dcpu16_hw_data_init_t)(struct dcpu16_hw *, void *);
91 typedef void (dcpu16_hw_data_free_t)(struct dcpu16_hw *);
92 typedef int (dcpu16_hw_ctl_t)(struct dcpu16_hw *, const char *, void *, void *);
93 struct dcpu16_hw_module {
94 char *name_; /* dymo label on front panel */
95
96 DCPU16_WORD id_l;
97 DCPU16_WORD id_h;
98 DCPU16_WORD ver;
99 DCPU16_WORD mfg_l;
100 DCPU16_WORD mfg_h;
101
102 dcpu16_hw_signal_t *hwi; /* hardware interrupt handler */
103 dcpu16_hw_signal_t *cycle; /* cycle tick handler */
104 dcpu16_hw_signal_t *reset; /* reset handler */
105
106 dcpu16_hw_data_init_t *data_init; /* how to allocate a dcpu16_hw instance's data */
107 dcpu16_hw_data_free_t *data_free;
108 dcpu16_hw_ctl_t *ctl;
109 struct dcpu16_hw_ctl_cmd *ctl_cmd;
110 };
111
112 /* instantiate a new core */
113 struct dcpu16 *dcpu16_new(void);
114
115 /* reset a core to initial state */
116 void dcpu16_reset(struct dcpu16 *);
117
118 /* print words in buf as asm */
119 DCPU16_WORD dcpu16_mnemonify_buf(DCPU16_WORD *);
120
121 /* print the instruction at the specified address, returns number of words consumed in decoding */
122 DCPU16_WORD dcpu16_disassemble_print(struct dcpu16 *, DCPU16_WORD);
123
124 /* create and delete 'hardware' objects */
125 struct dcpu16_hw *dcpu16_hw_new(struct dcpu16 *, struct dcpu16_hw_module *, void *);
126 void dcpu16_hw_del(struct dcpu16_hw **);
127
128 /* set options on hardware objects */
129 int dcpu16_hw_ctl(struct dcpu16_hw *, const char *, void *, void *);
130
131 /* register new 'hardware' device with system */
132 int dcpu16_hw_attach(struct dcpu16 *, struct dcpu16_hw *);
133
134 /* register a callback for an accounting event */
135 int dcpu16_acct_add(struct dcpu16 *, dcpu16_acct_event, dcpu16_ev_cb_t *, DCPU16_WORD, DCPU16_WORD, void *);
136
137 /* execute the next instruction */
138 void dcpu16_step(struct dcpu16 *);
139
140 /* release a core */
141 void dcpu16_delete(struct dcpu16 **);
142
143 /* signal hardware interrupt */
144 int dcpu16_interrupt(struct dcpu16 *, DCPU16_WORD);
145
146 /* consume a cycle */
147 void dcpu16_cycle_inc(struct dcpu16 *, unsigned int);
148
149 /* register callbacks to handle warning and debug messages, default is writing to stderr, may be set to null */
150 void dcpu16_warn_cb_set(void (*)(char *, ...));
151 void dcpu16_trace_cb_set(void (*)(char *, ...));
152
153 #endif /* DCPU16_H_3XXIQQG2 */