-
Notifications
You must be signed in to change notification settings - Fork 58
/
adc.c
81 lines (73 loc) · 1.91 KB
/
adc.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
71
72
73
74
75
76
77
78
79
80
81
/*B-em v2.2 by Tom Walker
ADC emulation*/
#include "b-em.h"
#include "adc.h"
#include "via.h"
#include "sysvia.h"
static uint8_t adc_status,adc_high,adc_low,adc_latch;
int adc_time;
uint8_t adc_read(uint16_t addr)
{
switch (addr & 3)
{
case 0:
return adc_status;
break;
case 1:
return adc_high;
break;
case 2:
return adc_low;
break;
}
return 0x40;
}
void adc_write(uint16_t addr, uint8_t val)
{
if (!(addr & 3))
{
adc_latch = val;
adc_time = (val & 8) ? 156 : 62; /* Choose time according to requested accuracy */
adc_status = (val & 0xF) | 0x80; /*Busy, converting*/
sysvia_set_cb1(1);
// printf("ADC conversion - %02X\n",val);
}
}
void adc_poll()
{
uint32_t val = (uint32_t)(joyaxes[adc_status & 3] * -32760.0 + 32760); // putting the ) after the .0 makes the numbers 32k+ ONLY in VS
if (val > 0xFFFF)
val = 0xFFFF;
adc_status =(adc_status & 0xF) | 0x40; /*Not busy, conversion complete*/
adc_status|=(val & 0xC000) >> 10;
adc_high = val >> 8;
adc_low = val & 0xFF;
sysvia_set_cb1(0);
}
void adc_init()
{
adc_status = 0x40; /*Not busy, conversion complete*/
adc_high = adc_low = adc_latch = 0;
adc_time = 0;
al_install_joystick();
}
void adc_savestate(FILE *f)
{
unsigned char bytes[5];
bytes[0] = adc_status;
bytes[1] = adc_low;
bytes[2] = adc_high;
bytes[3] = adc_latch;
bytes[4] = adc_time;
fwrite(bytes, sizeof(bytes), 1, f);
}
void adc_loadstate(FILE *f)
{
unsigned char bytes[5];
fread(bytes, sizeof(bytes), 1, f);
adc_status = bytes[0];
adc_low = bytes[1];
adc_high = bytes[2];
adc_latch = bytes[3];
adc_time = bytes[4];
}