OXYGENHUB
◆ main enhanced-x16-emulator / src / i2c.h
2.6 KiB · Objective-C · last changed chore(): initial commit 25 days ago source blame history raw
1
// Commander X16 Emulator
2
// Copyright (c) 2021 Michael Steil
3
// All rights reserved. License: 2-clause BSD
4
5
#ifndef _I2C_H_
6
#define _I2C_H_
7
8
#include <stdint.h>
9
#include <stdbool.h>
10
11
#ifdef __cplusplus
12
extern "C" {
13
#endif
14
15
#define I2C_DATA_MASK 1
16
#define I2C_CLK_MASK 2
17
18
typedef struct {
19
	int clk_in;
20
	int data_in;
21
	int data_out;
22
} i2c_port_t;
23
24
extern i2c_port_t i2c_port;
25
26
void i2c_reset_state();
27
void i2c_step();
28
29
void i2c_kbd_buffer_add(uint8_t value);
30
uint8_t i2c_kbd_buffer_next();
31
void i2c_kbd_buffer_flush();
32
uint8_t i2c_kbd_buffer_count();
33
34
void i2c_mse_buffer_add(uint8_t value);
35
uint8_t i2c_mse_buffer_next();
36
void i2c_mse_buffer_flush();
37
uint8_t i2c_mse_buffer_count();
38
39
// fake mouse
40
void mouse_button_down(int num);
41
void mouse_button_up(int num);
42
void mouse_move(int x, int y);
43
uint8_t mouse_read(uint8_t reg);
44
void mouse_send_state(void);
45
void mouse_set_wheel(int8_t y);
46
uint8_t mouse_get_device_id();
47
void mouse_set_device_id(uint8_t d);
48
49
// ─── Debugger accessor ───────────────────────────────────────────────────────
50
// Side-effect-free snapshot of I2C bus state. Safe to call every frame from
51
// the debugger while the emulation is running.
52
//
53
// Known device addresses: 0x42 = SMC, 0x6F = RTC.
54
// device_name is a short static string label ("SMC", "RTC", or "" for unknown).
55
// Session counters (transactions_started etc.) are cumulative since boot and
56
// are never decremented.
57
58
typedef struct {
59
	int      state;                  // bus state machine: -1=STOP, 0=START, 1-8=bit index
60
	bool     read_mode;              // current byte is being read from the device
61
	uint8_t  value;                  // byte currently being assembled/shifted
62
	int      count;                  // byte count within the current transaction
63
	uint8_t  device;                 // I2C device address for the current transaction
64
	char     device_name[8];         // human-readable name for the device address
65
	int      clk_in;                 // i2c_port.clk_in
66
	int      data_in;                // i2c_port.data_in
67
	int      data_out;               // i2c_port.data_out
68
	uint32_t transactions_started;   // cumulative START conditions seen
69
	uint32_t transactions_completed; // cumulative STOP conditions seen
70
	uint32_t bytes_read;             // cumulative bytes read from devices
71
	uint32_t bytes_written;          // cumulative bytes written to devices
72
	uint8_t  kbd_fill;               // keyboard ring buffer occupancy
73
	uint8_t  mse_fill;               // mouse ring buffer occupancy
74
} i2c_debug_state_t;
75
76
void i2c_debug_get_state(i2c_debug_state_t *out);
77
78
#ifdef __cplusplus
79
}
80
#endif
81
82
#endif