annotate vgm.c @ 1999:7a6835c02db0

Fix libretro target on windows
author Mike Pavone <pavone@retrodev.com>
date Sat, 11 Jul 2020 21:04:16 -0700
parents e35b00626b3e
children fc8fd89aeba9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1909
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1 #include <stdlib.h>
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
2 #include <string.h>
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
3 #include <stddef.h>
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
4 #include "vgm.h"
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
5
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
6 vgm_writer *vgm_write_open(char *filename, uint32_t rate, uint32_t clock, uint32_t cycle)
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
7 {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
8 FILE *f = fopen(filename, "wb");
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
9 if (!f) {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
10 return NULL;
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
11 }
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
12 vgm_writer *writer = calloc(sizeof(vgm_writer), 1);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
13 memcpy(writer->header.ident, "Vgm ", 4);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
14 writer->header.version = 0x150;
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
15 writer->header.data_offset = sizeof(writer->header) - offsetof(vgm_header, data_offset);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
16 writer->header.rate = rate;
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
17 writer->f = f;
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
18 if (1 != fwrite(&writer->header, sizeof(writer->header), 1, f)) {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
19 free(writer);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
20 fclose(f);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
21 return NULL;
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
22 }
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
23 writer->master_clock = clock;
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
24 writer->last_cycle = cycle;
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
25
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
26 return writer;
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
27 }
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
28
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
29 void vgm_sn76489_init(vgm_writer *writer, uint32_t clock, uint16_t feedback, uint8_t shift_reg_size, uint8_t flags)
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
30 {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
31 if (flags && writer->header.version < 0x151) {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
32 writer->header.version = 0x151;
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
33 }
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
34 writer->header.sn76489_clk = clock,
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
35 writer->header.sn76489_fb = feedback;
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
36 writer->header.sn76489_shift = shift_reg_size;
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
37 writer->header.sn76489_flags = flags;
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
38 }
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
39
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
40 static void wait_commands(vgm_writer *writer, uint32_t delta)
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
41 {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
42 if (!delta) {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
43 return;
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
44 }
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
45 if (delta <= 0x10) {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
46 fputc(CMD_WAIT_SHORT + (delta - 1), writer->f);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
47 } else if (delta >= 735 && delta <= (735 + 0x10)) {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
48 fputc(CMD_WAIT_60, writer->f);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
49 wait_commands(writer, delta - 735);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
50 } else if (delta >= 882 && delta <= (882 + 0x10)) {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
51 fputc(CMD_WAIT_50, writer->f);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
52 wait_commands(writer, delta - 882);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
53 } else if (delta > 0xFFFF) {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
54 uint8_t cmd[3] = {CMD_WAIT, 0xFF, 0xFF};
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
55 fwrite(cmd, 1, sizeof(cmd), writer->f);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
56 wait_commands(writer, delta - 0xFFFF);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
57 } else {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
58 uint8_t cmd[3] = {CMD_WAIT, delta, delta >> 8};
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
59 fwrite(cmd, 1, sizeof(cmd), writer->f);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
60 }
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
61 }
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
62
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
63 static void add_wait(vgm_writer *writer, uint32_t cycle)
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
64 {
1996
e35b00626b3e Update cycle to VGM sample conversion based on ValleyBell's suggestion
Michael Pavone <pavone@retrodev.com>
parents: 1993
diff changeset
65 uint64_t last_sample = (uint64_t)writer->last_cycle * (uint64_t)44100;
e35b00626b3e Update cycle to VGM sample conversion based on ValleyBell's suggestion
Michael Pavone <pavone@retrodev.com>
parents: 1993
diff changeset
66 last_sample /= (uint64_t)writer->master_clock;
e35b00626b3e Update cycle to VGM sample conversion based on ValleyBell's suggestion
Michael Pavone <pavone@retrodev.com>
parents: 1993
diff changeset
67 uint64_t sample = (uint64_t)cycle * (uint64_t)44100;
e35b00626b3e Update cycle to VGM sample conversion based on ValleyBell's suggestion
Michael Pavone <pavone@retrodev.com>
parents: 1993
diff changeset
68 sample /= (uint64_t)writer->master_clock;
e35b00626b3e Update cycle to VGM sample conversion based on ValleyBell's suggestion
Michael Pavone <pavone@retrodev.com>
parents: 1993
diff changeset
69 uint32_t delta = sample - last_sample;
1909
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
70
1996
e35b00626b3e Update cycle to VGM sample conversion based on ValleyBell's suggestion
Michael Pavone <pavone@retrodev.com>
parents: 1993
diff changeset
71 writer->last_cycle = cycle;
1909
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
72 writer->header.num_samples += delta;
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
73 wait_commands(writer, delta);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
74 }
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
75
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
76 void vgm_sn76489_write(vgm_writer *writer, uint32_t cycle, uint8_t value)
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
77 {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
78 add_wait(writer, cycle);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
79 uint8_t cmd[2] = {CMD_PSG, value};
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
80 fwrite(cmd, 1, sizeof(cmd), writer->f);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
81 }
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
82
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
83 void vgm_ym2612_init(vgm_writer *writer, uint32_t clock)
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
84 {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
85 writer->header.ym2612_clk = clock;
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
86 }
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
87
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
88 void vgm_ym2612_part1_write(vgm_writer *writer, uint32_t cycle, uint8_t reg, uint8_t value)
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
89 {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
90 add_wait(writer, cycle);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
91 uint8_t cmd[3] = {CMD_YM2612_0, reg, value};
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
92 fwrite(cmd, 1, sizeof(cmd), writer->f);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
93 }
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
94
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
95 void vgm_ym2612_part2_write(vgm_writer *writer, uint32_t cycle, uint8_t reg, uint8_t value)
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
96 {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
97 add_wait(writer, cycle);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
98 uint8_t cmd[3] = {CMD_YM2612_1, reg, value};
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
99 fwrite(cmd, 1, sizeof(cmd), writer->f);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
100 }
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
101
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
102 void vgm_adjust_cycles(vgm_writer *writer, uint32_t deduction)
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
103 {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
104 if (deduction > writer->last_cycle) {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
105 writer->last_cycle = 0;
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
106 } else {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
107 writer->last_cycle -= deduction;
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
108 }
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
109 }
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
110
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
111 void vgm_close(vgm_writer *writer)
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
112 {
1993
a9449608d0b0 Add stop command to end of recorded VGM stream
Mike Pavone <pavone@retrodev.com>
parents: 1909
diff changeset
113 uint8_t cmd = 0x66;
a9449608d0b0 Add stop command to end of recorded VGM stream
Mike Pavone <pavone@retrodev.com>
parents: 1909
diff changeset
114 fwrite(&cmd, 1, sizeof(cmd), writer->f);
1909
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
115 writer->header.eof_offset = ftell(writer->f) - offsetof(vgm_header, eof_offset);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
116 fseek(writer->f, SEEK_SET, 0);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
117 fwrite(&writer->header, sizeof(writer->header), 1, writer->f);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
118 fclose(writer->f);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
119 free(writer);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
120 }