summaryrefslogtreecommitdiff
path: root/vgm.c
blob: 0f828b3d904e735cf2d12b40dd387a3efe31ffd8 (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
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
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include "vgm.h"

vgm_writer *vgm_write_open(char *filename, uint32_t rate, uint32_t clock, uint32_t cycle)
{
	FILE *f = fopen(filename, "wb");
	if (!f) {
		return NULL;
	}
	vgm_writer *writer = calloc(sizeof(vgm_writer), 1);
	memcpy(writer->header.ident, "Vgm ", 4);
	writer->header.version = 0x150;
	writer->header.data_offset = sizeof(writer->header) - offsetof(vgm_header, data_offset);
	writer->header.rate = rate;
	writer->f = f;
	if (1 != fwrite(&writer->header, sizeof(writer->header), 1, f)) {
		free(writer);
		fclose(f);
		return NULL;
	}
	writer->master_clock = clock;
	writer->last_cycle = cycle;
	
	return writer;
}

void vgm_sn76489_init(vgm_writer *writer, uint32_t clock, uint16_t feedback, uint8_t shift_reg_size, uint8_t flags)
{
	if (flags && writer->header.version < 0x151) {
		writer->header.version = 0x151;
	}
	writer->header.sn76489_clk = clock,
	writer->header.sn76489_fb = feedback;
	writer->header.sn76489_shift = shift_reg_size;
	writer->header.sn76489_flags = flags;
}

static void wait_commands(vgm_writer *writer, uint32_t delta)
{
	if (!delta) {
		return;
	}
	if (delta <= 0x10) {
		fputc(CMD_WAIT_SHORT + (delta - 1), writer->f);
	} else if (delta >= 735 && delta <= (735 + 0x10)) {
		fputc(CMD_WAIT_60, writer->f);
		wait_commands(writer, delta - 735);
	} else if (delta >= 882 && delta <= (882 + 0x10)) {
		fputc(CMD_WAIT_50, writer->f);
		wait_commands(writer, delta - 882);
	} else if (delta > 0xFFFF) {
		uint8_t cmd[3] = {CMD_WAIT, 0xFF, 0xFF};
		fwrite(cmd, 1, sizeof(cmd), writer->f);
		wait_commands(writer, delta - 0xFFFF);
	} else {
		uint8_t cmd[3] = {CMD_WAIT, delta, delta >> 8};
		fwrite(cmd, 1, sizeof(cmd), writer->f);
	}
}

#include "util.h"
static void add_wait(vgm_writer *writer, uint32_t cycle)
{
	if (cycle < writer->last_cycle) {
		//This can happen when a YM-2612 write happens immediately after a PSG write
		//due to the relatively low granularity of the PSG's internal clock
		//given that VGM only has a granularity of 44.1 kHz ignoring this is harmless
		return;
	}
	uint64_t last_sample = (uint64_t)writer->last_cycle * (uint64_t)44100;
	last_sample /= (uint64_t)writer->master_clock;
	uint64_t sample = ((uint64_t)cycle + (uint64_t)writer->extra_delta) * (uint64_t)44100;
	sample /= (uint64_t)writer->master_clock;
	uint32_t delta = sample - last_sample;
	
	writer->last_cycle = cycle;
	writer->extra_delta = 0;
	writer->header.num_samples += delta;
	wait_commands(writer, delta);
}

static uint8_t last_cmd;
void vgm_sn76489_write(vgm_writer *writer, uint32_t cycle, uint8_t value)
{
	add_wait(writer, cycle);
	uint8_t cmd[2] = {CMD_PSG, value};
	last_cmd = CMD_PSG;
	fwrite(cmd, 1, sizeof(cmd), writer->f);
}

void vgm_ym2612_init(vgm_writer *writer, uint32_t clock)
{
	writer->header.ym2612_clk = clock;
}

void vgm_ym2612_part1_write(vgm_writer *writer, uint32_t cycle, uint8_t reg, uint8_t value)
{
	add_wait(writer, cycle);
	uint8_t cmd[3] = {CMD_YM2612_0, reg, value};
	last_cmd = CMD_YM2612_0;
	fwrite(cmd, 1, sizeof(cmd), writer->f);
}

void vgm_ym2612_part2_write(vgm_writer *writer, uint32_t cycle, uint8_t reg, uint8_t value)
{
	add_wait(writer, cycle);
	uint8_t cmd[3] = {CMD_YM2612_1, reg, value};
	last_cmd = CMD_YM2612_1;
	fwrite(cmd, 1, sizeof(cmd), writer->f);
}

void vgm_adjust_cycles(vgm_writer *writer, uint32_t deduction)
{
	if (deduction > writer->last_cycle) {
		writer->extra_delta += deduction - writer->last_cycle;
		writer->last_cycle = 0;
	} else {
		writer->last_cycle -= deduction;
	}
}

void vgm_close(vgm_writer *writer)
{
	uint8_t cmd = 0x66;
	fwrite(&cmd, 1, sizeof(cmd), writer->f);
	writer->header.eof_offset = ftell(writer->f) - offsetof(vgm_header, eof_offset);
	fseek(writer->f, SEEK_SET, 0);
	fwrite(&writer->header, sizeof(writer->header), 1, writer->f);
	fclose(writer->f);
	free(writer);
}