aboutsummaryrefslogtreecommitdiff
path: root/c64/sid.c
blob: 845f7998cf5b08b3fb4e854e5f999e9cdb18ab74 (plain)
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
// sid.c
//
// 20060803 Markku Alén

#include "sid.h"
#include "c64.h"

static int record_init;
static unsigned int record_freq;
static unsigned char *record_buffer;
static unsigned int record_max_len;
static unsigned int record_index;

void start_record(unsigned int frequency, unsigned char *wave_buf, unsigned int wave_max_len)
{
	record_init = 1;
	record_freq = frequency;
	record_buffer = wave_buf;
	record_max_len = wave_max_len;
	record_index = 0;
}

unsigned int stop_record(void)
{
	unsigned int len;
	len = record_index;
	record_freq = 0;
	record_buffer = 0;
	record_max_len = 0;
	record_index = 0;
	return len;
}

static void record(unsigned int counter, unsigned int sample)
{
	static unsigned int last_counter;
	static unsigned int last_sample;
	unsigned int elapsed;
	if(record_init)
	{
		record_init = 0;
		last_counter = counter;
	}
	elapsed = ((record_freq * (counter - last_counter)) + (F_CPU / 2)) / F_CPU;
	while(elapsed-- > 0)
	{
		if(record_index < record_max_len)
			record_buffer[record_index] = sample;
		record_index += 1;
	}
	last_counter = counter;
}

static int volume;

static unsigned char regs[32];

void sid_init(void)
{
	int i;
	for(i = sizeof(regs);i-- > 0;)
		regs[i] = 0x00;
	(void)stop_record();
	volume = 0;
}

int sid_read(int address)
{
	switch(address & 0x001f)
	{
	case 0x0018:
		return volume;
	default:
		return regs[address % sizeof(regs)];
	}
}

void sid_write(int address, int data)
{
	switch(address & 0x001f)
	{
	case 0x0018:
		volume = data & 0x0f;
		if(record_buffer != 0)
			record(total_cycles, (volume << 4) | volume);
		break;
	default:
		regs[address % sizeof(regs)] = data;
	}
}