1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
#include "genesis.h"
#include "util.h"
enum {
I2C_IDLE,
I2C_START,
I2C_DEVICE_ACK,
I2C_ADDRESS_HI,
I2C_ADDRESS_HI_ACK,
I2C_ADDRESS,
I2C_ADDRESS_ACK,
I2C_READ,
I2C_READ_ACK,
I2C_WRITE,
I2C_WRITE_ACK
};
char * i2c_states[] = {
"idle",
"start",
"device ack",
"address hi",
"address hi ack",
"address",
"address ack",
"read",
"read_ack",
"write",
"write_ack"
};
void eeprom_init(eeprom_state *state, uint8_t *buffer, uint32_t size)
{
state->slave_sda = 1;
state->host_sda = state->scl = 0;
state->buffer = buffer;
state->size = size;
state->state = I2C_IDLE;
}
void set_host_sda(eeprom_state *state, uint8_t val)
{
if (state->scl) {
if (val & ~state->host_sda) {
//low to high, stop condition
state->state = I2C_IDLE;
state->slave_sda = 1;
} else if (~val & state->host_sda) {
//high to low, start condition
state->state = I2C_START;
state->slave_sda = 1;
state->counter = 8;
}
}
state->host_sda = val;
}
void set_scl(eeprom_state *state, uint8_t val)
{
if (val & ~state->scl) {
//low to high transition
switch (state->state)
{
case I2C_START:
case I2C_ADDRESS_HI:
case I2C_ADDRESS:
case I2C_WRITE:
state->latch = state->host_sda | state->latch << 1;
state->counter--;
if (!state->counter) {
switch (state->state & 0x7F)
{
case I2C_START:
state->state = I2C_DEVICE_ACK;
break;
case I2C_ADDRESS_HI:
state->address = state->latch << 8;
state->state = I2C_ADDRESS_HI_ACK;
break;
case I2C_ADDRESS:
state->address |= state->latch;
state->state = I2C_ADDRESS_ACK;
break;
case I2C_WRITE:
state->buffer[state->address] = state->latch;
state->state = I2C_WRITE_ACK;
break;
}
}
break;
case I2C_DEVICE_ACK:
if (state->latch & 1) {
state->state = I2C_READ;
state->counter = 8;
if (state->size < 256) {
state->address = state->latch >> 1;
}
state->latch = state->buffer[state->address];
} else {
if (state->size < 256) {
state->address = state->latch >> 1;
state->state = I2C_WRITE;
} else if (state->size < 4096) {
state->address = (state->latch & 0xE) << 7;
state->state = I2C_ADDRESS;
} else {
state->state = I2C_ADDRESS_HI;
}
state->counter = 8;
}
break;
case I2C_ADDRESS_HI_ACK:
state->state = I2C_ADDRESS;
state->counter = 8;
break;
case I2C_ADDRESS_ACK:
state->state = I2C_WRITE;
state->address &= state->size-1;
state->counter = 8;
break;
case I2C_READ:
state->counter--;
if (!state->counter) {
state->state = I2C_READ_ACK;
}
break;
case I2C_READ_ACK:
state->state = I2C_READ;
state->counter = 8;
state->address++;
//TODO: page mask
state->address &= state->size-1;
state->latch = state->buffer[state->address];
break;
case I2C_WRITE_ACK:
state->state = I2C_WRITE;
state->counter = 8;
state->address++;
//TODO: page mask
state->address &= state->size-1;
break;
}
} else if (~val & state->scl) {
//high to low transition
switch (state->state & 0x7F)
{
case I2C_DEVICE_ACK:
case I2C_ADDRESS_HI_ACK:
case I2C_ADDRESS_ACK:
case I2C_READ_ACK:
case I2C_WRITE_ACK:
state->slave_sda = 0;
break;
case I2C_READ:
state->slave_sda = state->latch >> 7;
state->latch = state->latch << 1;
break;
default:
state->slave_sda = 1;
break;
}
}
state->scl = val;
}
uint8_t get_sda(eeprom_state *state)
{
return state->host_sda & state->slave_sda;
}
eeprom_map *find_eeprom_map(uint32_t address, genesis_context *gen)
{
for (int i = 0; i < gen->num_eeprom; i++)
{
if (address >= gen->eeprom_map[i].start && address <= gen->eeprom_map[i].end) {
return gen->eeprom_map + i;
}
}
return NULL;
}
void * write_eeprom_i2c_w(uint32_t address, void * context, uint16_t value)
{
genesis_context *gen = ((m68k_context *)context)->system;
eeprom_map *map = find_eeprom_map(address, gen);
if (!map) {
fatal_error("Could not find EEPROM map for address %X\n", address);
}
if (map->scl_mask) {
set_scl(&gen->eeprom, (value & map->scl_mask) != 0);
}
if (map->sda_write_mask) {
set_host_sda(&gen->eeprom, (value & map->sda_write_mask) != 0);
}
return context;
}
void * write_eeprom_i2c_b(uint32_t address, void * context, uint8_t value)
{
genesis_context *gen = ((m68k_context *)context)->system;
eeprom_map *map = find_eeprom_map(address, gen);
if (!map) {
fatal_error("Could not find EEPROM map for address %X\n", address);
}
uint16_t expanded, mask;
if (address & 1) {
expanded = value;
mask = 0xFF;
} else {
expanded = value << 8;
mask = 0xFF00;
}
if (map->scl_mask & mask) {
set_scl(&gen->eeprom, (expanded & map->scl_mask) != 0);
}
if (map->sda_write_mask & mask) {
set_host_sda(&gen->eeprom, (expanded & map->sda_write_mask) != 0);
}
return context;
}
uint16_t read_eeprom_i2c_w(uint32_t address, void * context)
{
genesis_context *gen = ((m68k_context *)context)->system;
eeprom_map *map = find_eeprom_map(address, gen);
if (!map) {
fatal_error("Could not find EEPROM map for address %X\n", address);
}
uint16_t ret = 0;
if (map->sda_read_bit < 16) {
ret = get_sda(&gen->eeprom) << map->sda_read_bit;
}
return ret;
}
uint8_t read_eeprom_i2c_b(uint32_t address, void * context)
{
genesis_context *gen = ((m68k_context *)context)->system;
eeprom_map *map = find_eeprom_map(address, gen);
if (!map) {
fatal_error("Could not find EEPROM map for address %X\n", address);
}
uint8_t bit = address & 1 ? map->sda_read_bit : map->sda_read_bit - 8;
uint8_t ret = 0;
if (bit < 8) {
ret = get_sda(&gen->eeprom) << bit;
}
return ret;
}
|