1 #ifndef DCPU16_H_3XXIQQG2
2 #define DCPU16_H_3XXIQQG2
6 /* the target system's concept of a word */
7 typedef unsigned short DCPU16_WORD
;
9 /* how much ram the system has */
10 #define DCPU16_RAM 0x10000
11 #define DCPU16_INTERRUPT_QUEUE_SIZE 256
13 extern const char * const dcpu16_reg_names
[];
14 enum dcpu16_register_indexes
{
30 /* a self-contained dcpu16 core */
32 void (*warn_cb_
)(char *fmt
, ...);
33 void (*trace_cb_
)(char *fmt
, ...);
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 */
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 */
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 */
52 DCPU16_WORD reg
[DCPU16_REG__NUM
]; /* system registers, a b c x y z i j */
53 DCPU16_WORD ram
[DCPU16_RAM
]; /* memory */
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
{
67 dcpu16_acct_event mask
;
70 typedef void (dcpu16_hw_signal_t
)(struct dcpu16
*, struct dcpu16_hw
*);
71 /* these are used to define hardware attached to the system */
73 struct dcpu16
*vm
; /* which system do I belong to */
74 struct dcpu16_hw_module
*mod
; /* whence I came */
83 dcpu16_hw_signal_t
*hwi
;
84 dcpu16_hw_signal_t
*cycle
;
85 dcpu16_hw_signal_t
*reset
;
90 /* human-readable text describing hw module control operations, for convenience's sake */
91 struct dcpu16_hw_ctl_cmd
{
97 typedef int (dcpu16_hw_data_init_t
)(struct dcpu16_hw
*, void *);
98 typedef void (dcpu16_hw_data_free_t
)(struct dcpu16_hw
*);
99 typedef int (dcpu16_hw_ctl_t
)(struct dcpu16_hw
*, const char *, void *, void *);
100 struct dcpu16_hw_module
{
101 struct dcpu16_hw
*template;
102 dcpu16_hw_data_init_t
*data_init
;
103 dcpu16_hw_data_free_t
*data_free
;
104 dcpu16_hw_ctl_t
*ctl
;
105 struct dcpu16_hw_ctl_cmd
*ctl_cmd
;
108 /* instantiate a new core */
109 struct dcpu16
*dcpu16_new(void);
111 /* reset a core to initial state */
112 void dcpu16_reset(struct dcpu16
*);
114 /* print the current state of a core */
115 void dcpu16_state_print(struct dcpu16
*);
117 /* print the contents of ram from second to third argument */
118 void dcpu16_dump_ram(struct dcpu16
*, DCPU16_WORD
, DCPU16_WORD
);
120 /* print words in buf as asm */
121 DCPU16_WORD
dcpu16_mnemonify_buf(DCPU16_WORD
*);
123 /* print the instruction at the specified address, returns number of words consumed in decoding */
124 DCPU16_WORD
dcpu16_disassemble_print(struct dcpu16
*, DCPU16_WORD
);
126 /* create and delete 'hardware' objects */
127 struct dcpu16_hw
*dcpu16_hw_new(struct dcpu16
*, struct dcpu16_hw_module
*, void *);
128 void dcpu16_hw_del(struct dcpu16_hw
**);
130 /* set options on hardware objects */
131 int dcpu16_hw_ctl(struct dcpu16_hw
*, const char *, void *, void *);
133 /* register new 'hardware' device with system */
134 int dcpu16_hw_attach(struct dcpu16
*, struct dcpu16_hw
*);
136 /* register a callback for an accounting event */
137 int dcpu16_acct_add(struct dcpu16
*, dcpu16_acct_event
, dcpu16_ev_cb_t
*, void *);
139 /* execute the next instruction */
140 void dcpu16_step(struct dcpu16
*);
143 void dcpu16_delete(struct dcpu16
**);
145 int dcpu16_interrupt(struct dcpu16
*, DCPU16_WORD
);
146 void dcpu16_cycle_inc(struct dcpu16
*, unsigned int);
148 /* register callbacks to handle warning and debug messages, default is writing to stderr, may be set to null */
149 void dcpu16_warn_cb_set(void (*)(char *, ...));
150 void dcpu16_trace_cb_set(void (*)(char *, ...));
152 #endif /* DCPU16_H_3XXIQQG2 */