merge
[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 /* we call this sort of function for emitting a readable message */
31 typedef void (dcpu16_msg_cb_t)(unsigned int, char *, ...);
32
33 /* we may emit any of these types of messages */
34 enum dcpu16_msg_type {
35 DCPU16_MSG_ERROR = 0,
36 DCPU16_MSG_INFO,
37 DCPU16_MSG_DEBUG
38 };
39
40 /* update the default message handler */
41 dcpu16_msg_cb_t *dcpu16_msg_set_default(dcpu16_msg_cb_t *);
42
43 /* hardware devices may want to call this directly, without a core context */
44 extern dcpu16_msg_cb_t *dcpu16_msg_;
45
46 /* a self-contained dcpu16 core */
47 struct dcpu16 {
48 dcpu16_msg_cb_t *msg_cb_; /* callback to invoke for messages */
49
50 struct dcpu16_acct_cb *cb_table_; /* list of callbacks to invoke for certain events */
51 size_t cb_table_entries_; /* callback list maintenance */
52 size_t cb_table_allocated_; /* callback list maintenance */
53
54 struct dcpu16_hw *hw_table_; /* list of hardware attached to system */
55 size_t hw_table_entries_; /* hardware list maintenance */
56 size_t hw_table_allocated_; /* hardware list maintenance */
57
58 unsigned long long cycle_; /* number of cycles this core has executed */
59 unsigned int skip_ : 1; /* skip execution of next instruction */
60 unsigned int interrupts_deferred_ : 1; /* queue software interrupts */
61 unsigned int on_fire_ : 1; /* cpu is on fire */
62 DCPU16_WORD reg_work_[2]; /* work registers for holding literal values while decoding instructions */
63 DCPU16_WORD interrupts_[DCPU16_INTERRUPT_QUEUE_SIZE]; /* fifo of pending interrupts */
64 size_t interrupts_head_; /* interrupt queue maintenance */
65 size_t interrupts_tail_; /* interrupt queue maintenance */
66
67 DCPU16_WORD reg[DCPU16_REG__NUM]; /* system registers, a b c x y z i j */
68 DCPU16_WORD ram[DCPU16_RAM]; /* memory */
69 };
70
71 /* these are used for accounting/watchpointing/modules/&c */
72 typedef unsigned int dcpu16_acct_event;
73 typedef void (dcpu16_ev_cb_t)(struct dcpu16 *, dcpu16_acct_event, DCPU16_WORD, void *);
74 #define DCPU16_ACCT_EV_CYCLE (1<<1)
75 #define DCPU16_ACCT_EV_READ (1<<2)
76 #define DCPU16_ACCT_EV_WRITE (1<<3)
77 #define DCPU16_ACCT_EV_REG_READ (1<<4)
78 #define DCPU16_ACCT_EV_REG_WRITE (1<<5)
79 #define DCPU16_ACCT_EV_NOP (1<<6)
80 #define DCPU16_ACCT_EV_RESET (1<<7)
81 struct dcpu16_acct_cb {
82 dcpu16_ev_cb_t *fn; /* call this */
83 void *data; /* also mention this */
84 dcpu16_acct_event mask; /* when (mask & event) is true and */
85 DCPU16_WORD addr_l, /* addr is this or higher and */
86 addr_h; /* addr is this or lower */
87 };
88
89 typedef void (dcpu16_hw_signal_t)(struct dcpu16 *, struct dcpu16_hw *);
90 /* this structure defines a specific instance of this type of 'hardware' */
91 struct dcpu16_hw {
92 struct dcpu16 *vm; /* which system do I belong to */
93 struct dcpu16_hw_module *mod; /* whence I came */
94 void *data; /* per-instance data */
95 };
96
97 /* human-readable text describing hw module control operations, for convenience's sake */
98 struct dcpu16_hw_ctl_cmd {
99 char *command;
100 char *data_in_type;
101 char *data_out_type;
102 char *description;
103 };
104
105 typedef int (dcpu16_hw_data_init_t)(struct dcpu16_hw *, void *);
106 typedef void (dcpu16_hw_data_free_t)(struct dcpu16_hw *);
107 typedef int (dcpu16_hw_ctl_t)(struct dcpu16_hw *, const char *, void *, void *);
108 struct dcpu16_hw_module {
109 char *name_; /* dymo label on front panel */
110
111 DCPU16_WORD id_l;
112 DCPU16_WORD id_h;
113 DCPU16_WORD ver;
114 DCPU16_WORD mfg_l;
115 DCPU16_WORD mfg_h;
116
117 dcpu16_hw_signal_t *hwi; /* hardware interrupt handler */
118 dcpu16_hw_signal_t *cycle; /* cycle tick handler */
119 dcpu16_hw_signal_t *reset; /* reset handler */
120
121 dcpu16_hw_data_init_t *data_init; /* how to allocate a dcpu16_hw instance's data */
122 dcpu16_hw_data_free_t *data_free;
123 dcpu16_hw_ctl_t *ctl;
124 struct dcpu16_hw_ctl_cmd *ctl_cmd;
125 };
126
127 /* update a message handler */
128 dcpu16_msg_cb_t *dcpu16_msg_set(struct dcpu16 *, dcpu16_msg_cb_t *);
129
130 /* instantiate a new core */
131 struct dcpu16 *dcpu16_new(void);
132
133 /* reset a core to initial state */
134 void dcpu16_reset(struct dcpu16 *);
135
136 /* print words in buf as asm */
137 DCPU16_WORD dcpu16_mnemonify_buf(DCPU16_WORD *);
138
139 /* print the instruction at the specified address, returns number of words consumed in decoding */
140 DCPU16_WORD dcpu16_disassemble_print(struct dcpu16 *, DCPU16_WORD);
141
142 /* create and delete 'hardware' objects */
143 struct dcpu16_hw *dcpu16_hw_new(struct dcpu16 *, struct dcpu16_hw_module *, void *);
144 void dcpu16_hw_del(struct dcpu16_hw **);
145
146 /* set options on hardware objects */
147 int dcpu16_hw_ctl(struct dcpu16_hw *, const char *, void *, void *);
148
149 /* register new 'hardware' device with system */
150 int dcpu16_hw_attach(struct dcpu16 *, struct dcpu16_hw *);
151
152 /* register a callback for an accounting event */
153 int dcpu16_acct_add(struct dcpu16 *, dcpu16_acct_event, dcpu16_ev_cb_t *, DCPU16_WORD, DCPU16_WORD, void *);
154
155 /* execute the next instruction */
156 void dcpu16_step(struct dcpu16 *);
157
158 /* release a core */
159 void dcpu16_delete(struct dcpu16 **);
160
161 /* signal hardware interrupt */
162 int dcpu16_interrupt(struct dcpu16 *, DCPU16_WORD);
163
164 /* consume a cycle */
165 void dcpu16_cycle_inc(struct dcpu16 *, unsigned int);
166
167 #endif /* DCPU16_H_3XXIQQG2 */