8 #define MSG_(__level__, __vm__, ...) do { ((__vm__) ? ((struct dcpu16 *)(__vm__))->msg_cb_ : dcpu16_msg_)(__level__, __VA_ARGS__); } while (0)
9 #define MSG_INFO(__vm__, ...) MSG_(DCPU16_MSG_INFO, __vm__, __VA_ARGS__)
10 #define MSG_ERROR(__vm__, ...) MSG_(DCPU16_MSG_ERROR, __vm__, __VA_ARGS__)
12 #define MSG_DEBUG(__vm__, ...) MSG_(DCPU16_MSG_DEBUG, __vm__, __VA_ARGS__)
14 #define MSG_DEBUG(__vm__, ...) do { } while (0)
21 DCPU16_WORD interrupt_message
;
25 void clock_reset_(struct dcpu16
*vm
, struct dcpu16_hw
*hw
) {
26 struct clock_
*clock
= (struct clock_
*)hw
->data
;
30 memset(clock
, 0, sizeof *clock
);
34 void clock_cycle_(struct dcpu16
*vm
, struct dcpu16_hw
*hw
) {
35 struct clock_
*clock
= (struct clock_
*)hw
->data
;
37 /* cycle is only called 100000 times per second */
38 /* maximum rate is 60hz / word_max = 3932160 */
44 if (clock
->cycle_
>= clock
->rate
) {
45 /* THIS CHECK IS WRONG, JUST A PLACEHOLDER */
49 if (clock
->interrupt_message
) {
50 if (dcpu16_interrupt(vm
, clock
->interrupt_message
))
51 MSG_ERROR(vm
, "%s: could not send interrupt", hw
->mod
->name_
);
57 void clock_hwi_(struct dcpu16
*vm
, struct dcpu16_hw
*hw
) {
58 struct clock_
*clock
= (struct clock_
*)hw
->data
;
59 DCPU16_WORD reg_a
= vm
->reg
[DCPU16_REG_A
];
60 DCPU16_WORD reg_b
= vm
->reg
[DCPU16_REG_B
];
63 case 0: /* set tick gather rate, 60hz/B */
67 case 1: /* fetch elapsed count since rate was set */
68 vm
->reg
[DCPU16_REG_C
] = clock
->tick
;
72 clock
->interrupt_message
= reg_b
;
78 int clock_data_init_(struct dcpu16_hw
*hw
, void *data
) {
81 hw
->data
= calloc(1, sizeof(struct clock_
));
82 if (hw
->data
== NULL
) {
83 MSG_ERROR(hw
->vm
, "%s():%s", "calloc", strerror(errno
));
90 void clock_data_free_(struct dcpu16_hw
*hw
) {
100 struct dcpu16_hw_module dcpu16_hw_module_clock
= {
101 .name_
= "Generic Clock (compatible)",
109 .cycle
= clock_cycle_
,
110 .reset
= clock_reset_
,
112 .data_init
= clock_data_init_
,
113 .data_free
= clock_data_free_
,