forked from daitangio/x16-emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vera_spi.c
70 lines (64 loc) · 976 Bytes
/
vera_spi.c
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
// Commander X16 Emulator
// Copyright (c) 2019 Michael Steil
// All rights reserved. License: 2-clause BSD
#include <stdio.h>
#include <stdbool.h>
#include "sdcard.h"
bool ss;
bool busy;
uint8_t sending_byte, received_byte;
int outcounter;
void
vera_spi_init()
{
ss = false;
busy = false;
received_byte = 0xff;
}
void
vera_spi_step()
{
if (busy) {
outcounter++;
if (outcounter == 8) {
busy = false;
if (sdcard_file) {
received_byte = sdcard_handle(sending_byte);
} else {
received_byte = 0xff;
}
}
}
}
uint8_t
vera_spi_read(uint8_t reg)
{
switch (reg) {
case 0:
return received_byte;
case 1:
return busy << 7 | ss;
}
return 0;
}
void
vera_spi_write(uint8_t reg, uint8_t value)
{
switch (reg) {
case 0:
if (ss && !busy) {
sending_byte = value;
busy = true;
outcounter = 0;
}
break;
case 1:
if (ss != (value & 1)) {
ss = value & 1;
if (ss) {
sdcard_select();
}
}
break;
}
}