annotate vgm.c @ 2688:b42f00a3a937 default tip

Fix default target. Ensure m68k.h and z80.h are built before anything else when no dep info is available
author Michael Pavone <pavone@retrodev.com>
date Mon, 31 Mar 2025 21:06:18 -0700
parents 3f58fec775df
children
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;
2558
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
18 writer->header_size = sizeof(vgm_header);
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
19 fseek(f, writer->header_size, SEEK_CUR);
1909
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
20 writer->master_clock = clock;
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
21 writer->last_cycle = cycle;
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 return writer;
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
24 }
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 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
27 {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
28 if (flags && writer->header.version < 0x151) {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
29 writer->header.version = 0x151;
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 writer->header.sn76489_clk = clock,
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
32 writer->header.sn76489_fb = feedback;
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
33 writer->header.sn76489_shift = shift_reg_size;
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
34 writer->header.sn76489_flags = flags;
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
35 }
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
36
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
37 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
38 {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
39 if (!delta) {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
40 return;
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 <= 0x10) {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
43 fputc(CMD_WAIT_SHORT + (delta - 1), writer->f);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
44 } else if (delta >= 735 && delta <= (735 + 0x10)) {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
45 fputc(CMD_WAIT_60, writer->f);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
46 wait_commands(writer, delta - 735);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
47 } else if (delta >= 882 && delta <= (882 + 0x10)) {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
48 fputc(CMD_WAIT_50, writer->f);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
49 wait_commands(writer, delta - 882);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
50 } else if (delta > 0xFFFF) {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
51 uint8_t cmd[3] = {CMD_WAIT, 0xFF, 0xFF};
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
52 fwrite(cmd, 1, sizeof(cmd), writer->f);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
53 wait_commands(writer, delta - 0xFFFF);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
54 } else {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
55 uint8_t cmd[3] = {CMD_WAIT, delta, delta >> 8};
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
56 fwrite(cmd, 1, sizeof(cmd), writer->f);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
57 }
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
58 }
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
59
2002
fc8fd89aeba9 Fix VGM delay calculation overflow when a YM-2612 write follows a PSG write in close succession
Mike Pavone <pavone@retrodev.com>
parents: 1996
diff changeset
60 #include "util.h"
1909
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
61 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
62 {
2002
fc8fd89aeba9 Fix VGM delay calculation overflow when a YM-2612 write follows a PSG write in close succession
Mike Pavone <pavone@retrodev.com>
parents: 1996
diff changeset
63 if (cycle < writer->last_cycle) {
fc8fd89aeba9 Fix VGM delay calculation overflow when a YM-2612 write follows a PSG write in close succession
Mike Pavone <pavone@retrodev.com>
parents: 1996
diff changeset
64 //This can happen when a YM-2612 write happens immediately after a PSG write
fc8fd89aeba9 Fix VGM delay calculation overflow when a YM-2612 write follows a PSG write in close succession
Mike Pavone <pavone@retrodev.com>
parents: 1996
diff changeset
65 //due to the relatively low granularity of the PSG's internal clock
fc8fd89aeba9 Fix VGM delay calculation overflow when a YM-2612 write follows a PSG write in close succession
Mike Pavone <pavone@retrodev.com>
parents: 1996
diff changeset
66 //given that VGM only has a granularity of 44.1 kHz ignoring this is harmless
fc8fd89aeba9 Fix VGM delay calculation overflow when a YM-2612 write follows a PSG write in close succession
Mike Pavone <pavone@retrodev.com>
parents: 1996
diff changeset
67 return;
fc8fd89aeba9 Fix VGM delay calculation overflow when a YM-2612 write follows a PSG write in close succession
Mike Pavone <pavone@retrodev.com>
parents: 1996
diff changeset
68 }
1996
e35b00626b3e Update cycle to VGM sample conversion based on ValleyBell's suggestion
Michael Pavone <pavone@retrodev.com>
parents: 1993
diff changeset
69 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
70 last_sample /= (uint64_t)writer->master_clock;
2006
327332138c5c Prevent wait truncation in VGM logging
Michael Pavone <pavone@retrodev.com>
parents: 2002
diff changeset
71 uint64_t sample = ((uint64_t)cycle + (uint64_t)writer->extra_delta) * (uint64_t)44100;
1996
e35b00626b3e Update cycle to VGM sample conversion based on ValleyBell's suggestion
Michael Pavone <pavone@retrodev.com>
parents: 1993
diff changeset
72 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
73 uint32_t delta = sample - last_sample;
1909
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
74
1996
e35b00626b3e Update cycle to VGM sample conversion based on ValleyBell's suggestion
Michael Pavone <pavone@retrodev.com>
parents: 1993
diff changeset
75 writer->last_cycle = cycle;
2006
327332138c5c Prevent wait truncation in VGM logging
Michael Pavone <pavone@retrodev.com>
parents: 2002
diff changeset
76 writer->extra_delta = 0;
1909
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
77 writer->header.num_samples += delta;
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
78 wait_commands(writer, delta);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
79 }
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
80
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
81 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
82 {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
83 add_wait(writer, cycle);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
84 uint8_t cmd[2] = {CMD_PSG, value};
2527
7e1215d17571 Log Game Gear PSG Pan to VGM
Michael Pavone <pavone@retrodev.com>
parents: 2006
diff changeset
85 fwrite(cmd, 1, sizeof(cmd), writer->f);
7e1215d17571 Log Game Gear PSG Pan to VGM
Michael Pavone <pavone@retrodev.com>
parents: 2006
diff changeset
86 }
7e1215d17571 Log Game Gear PSG Pan to VGM
Michael Pavone <pavone@retrodev.com>
parents: 2006
diff changeset
87
7e1215d17571 Log Game Gear PSG Pan to VGM
Michael Pavone <pavone@retrodev.com>
parents: 2006
diff changeset
88 void vgm_gg_pan_write(vgm_writer *writer, uint32_t cycle, uint8_t value)
7e1215d17571 Log Game Gear PSG Pan to VGM
Michael Pavone <pavone@retrodev.com>
parents: 2006
diff changeset
89 {
7e1215d17571 Log Game Gear PSG Pan to VGM
Michael Pavone <pavone@retrodev.com>
parents: 2006
diff changeset
90 add_wait(writer, cycle);
7e1215d17571 Log Game Gear PSG Pan to VGM
Michael Pavone <pavone@retrodev.com>
parents: 2006
diff changeset
91 uint8_t cmd[2] = {CMD_PSG_STEREO, value};
1909
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_init(vgm_writer *writer, uint32_t clock)
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 writer->header.ym2612_clk = clock;
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
98 }
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
99
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
100 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
101 {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
102 add_wait(writer, cycle);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
103 uint8_t cmd[3] = {CMD_YM2612_0, reg, value};
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
104 fwrite(cmd, 1, sizeof(cmd), writer->f);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
105 }
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
106
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
107 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
108 {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
109 add_wait(writer, cycle);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
110 uint8_t cmd[3] = {CMD_YM2612_1, reg, value};
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
111 fwrite(cmd, 1, sizeof(cmd), writer->f);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
112 }
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
113
2558
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
114 void vgm_ymf262_init(vgm_writer *writer, uint32_t clock)
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
115 {
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
116 if (writer->header.version < 0x151) {
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
117 writer->header.version = 0x151;
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
118 }
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
119 uint32_t min_size = sizeof(vgm_header) + offsetof(vgm_extended_header, ymf278b_clk);
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
120 if (writer->header_size < min_size) {
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
121 fseek(writer->f, min_size - writer->header_size, SEEK_CUR);
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
122 writer->header_size = min_size;
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
123 }
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
124 writer->ext.ymf262_clk = clock;
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
125 }
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
126
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
127 void vgm_ymf262_part1_write(vgm_writer *writer, uint32_t cycle, uint8_t reg, uint8_t value)
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
128 {
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
129 add_wait(writer, cycle);
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
130 uint8_t cmd[3] = {CMD_YMF262_0, reg, value};
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
131 fwrite(cmd, 1, sizeof(cmd), writer->f);
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
132 }
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
133
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
134 void vgm_ymf262_part2_write(vgm_writer *writer, uint32_t cycle, uint8_t reg, uint8_t value)
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
135 {
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
136 add_wait(writer, cycle);
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
137 uint8_t cmd[3] = {CMD_YMF262_1, reg, value};
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
138 fwrite(cmd, 1, sizeof(cmd), writer->f);
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
139 }
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
140
1909
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
141 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
142 {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
143 if (deduction > writer->last_cycle) {
2006
327332138c5c Prevent wait truncation in VGM logging
Michael Pavone <pavone@retrodev.com>
parents: 2002
diff changeset
144 writer->extra_delta += deduction - writer->last_cycle;
1909
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
145 writer->last_cycle = 0;
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
146 } else {
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
147 writer->last_cycle -= deduction;
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
148 }
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
149 }
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
150
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
151 void vgm_close(vgm_writer *writer)
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
152 {
1993
a9449608d0b0 Add stop command to end of recorded VGM stream
Mike Pavone <pavone@retrodev.com>
parents: 1909
diff changeset
153 uint8_t cmd = 0x66;
a9449608d0b0 Add stop command to end of recorded VGM stream
Mike Pavone <pavone@retrodev.com>
parents: 1909
diff changeset
154 fwrite(&cmd, 1, sizeof(cmd), writer->f);
1909
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
155 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
156 fseek(writer->f, SEEK_SET, 0);
2558
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
157 uint32_t extra_size = writer->header_size - sizeof(writer->header);
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
158 if (extra_size) {
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
159 writer->header.data_offset += extra_size;
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
160 }
1909
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
161 fwrite(&writer->header, sizeof(writer->header), 1, writer->f);
2558
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
162 if (extra_size) {
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
163 fwrite(&writer->ext, extra_size, 1, writer->f);
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
164 }
1909
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
165 fclose(writer->f);
508522f08e4d Initial stab at VGM logging support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
166 free(writer);
2558
3f58fec775df Initial work on YMF262 (aka OPL3) emulation
Michael Pavone <pavone@retrodev.com>
parents: 2527
diff changeset
167 }