Refactor and get rid of common.h

This commit is contained in:
spicyjpeg
2025-02-28 02:15:21 +01:00
parent 7b5953322f
commit a39f159aaf
15 changed files with 1396 additions and 1526 deletions

View File

@@ -266,7 +266,7 @@ int psx_audio_xa_encode(psx_audio_xa_settings_t settings, psx_audio_encoder_stat
uint8_t init_sector = 1;
if (settings.stereo) { sample_count <<= 1; }
for (i = 0, j = 0; i < sample_count || ((j % 18) != 0); i += sample_jump, j++) {
psx_cdrom_sector_mode2_t *sector_data = (psx_cdrom_sector_mode2_t*) (output + ((j/18) * xa_sector_size) - xa_offset);
uint8_t *block_data = sector_data->data + ((j%18) * 0x80);
@@ -282,7 +282,7 @@ int psx_audio_xa_encode(psx_audio_xa_settings_t settings, psx_audio_encoder_stat
memcpy(block_data + 12, block_data + 8, 4);
if ((j+1)%18 == 0) {
psx_cdrom_calculate_checksums((uint8_t*) sector_data, PSX_CDROM_SECTOR_TYPE_MODE2_FORM2);
psx_cdrom_calculate_checksums((psx_cdrom_sector_t *)sector_data, PSX_CDROM_SECTOR_TYPE_MODE2_FORM2);
init_sector = 1;
}
}

View File

@@ -21,49 +21,88 @@ freely, subject to the following restrictions:
3. This notice may not be removed or altered from any source distribution.
*/
#include <stdint.h>
#include <string.h>
#include "libpsxav.h"
static uint32_t psx_cdrom_calculate_edc(uint8_t *sector, uint32_t offset, uint32_t size)
{
#define EDC_CRC32_POLYNOMIAL 0xD8018001
static uint32_t edc_crc32(uint8_t *data, int length) {
uint32_t edc = 0;
for (int i = offset; i < offset+size; i++) {
edc ^= 0xFF&(uint32_t)sector[i];
for (int ibit = 0; ibit < 8; ibit++) {
edc = (edc>>1)^(0xD8018001*(edc&0x1));
}
for (int i = 0; i < length; i++) {
edc ^= 0xFF & (uint32_t)data[i];
for (int j = 0; j < 8; j++)
edc = (edc >> 1) ^ (EDC_CRC32_POLYNOMIAL * (edc & 0x1));
}
return edc;
}
void psx_cdrom_calculate_checksums(uint8_t *sector, psx_cdrom_sector_type_t type)
{
switch (type) {
case PSX_CDROM_SECTOR_TYPE_MODE1: {
uint32_t edc = psx_cdrom_calculate_edc(sector, 0x0, 0x810);
sector[0x810] = (uint8_t)(edc);
sector[0x811] = (uint8_t)(edc >> 8);
sector[0x812] = (uint8_t)(edc >> 16);
sector[0x813] = (uint8_t)(edc >> 24);
#define TO_BCD(x) ((x) + ((x) / 10) * 6)
void psx_cdrom_init_sector(psx_cdrom_sector_t *sector, int lba, psx_cdrom_sector_type_t type) {
// Sync sequence
memset(sector->mode1.sync + 1, 0xff, 10);
sector->mode1.sync[0x0] = 0x00;
sector->mode1.sync[0xb] = 0x00;
// Timecode
lba += 150;
sector->mode1.header.minute = TO_BCD(lba / 4500);
sector->mode1.header.second = TO_BCD((lba / 75) % 60);
sector->mode1.header.sector = TO_BCD(lba % 75);
// Mode
if (type == PSX_CDROM_SECTOR_TYPE_MODE1) {
sector->mode1.header.mode = 0x01;
} else {
sector->mode2.header.mode = 0x02;
memset(sector->mode2.subheader, 0, sizeof(psx_cdrom_sector_xa_subheader_t));
sector->mode2.subheader[0].submode = PSX_CDROM_SECTOR_XA_SUBMODE_DATA;
if (type == PSX_CDROM_SECTOR_TYPE_MODE2_FORM2)
sector->mode2.subheader[0].submode |= PSX_CDROM_SECTOR_XA_SUBMODE_FORM2;
memcpy(sector->mode2.subheader + 1, sector->mode2.subheader, sizeof(psx_cdrom_sector_xa_subheader_t));
}
}
void psx_cdrom_calculate_checksums(psx_cdrom_sector_t *sector, psx_cdrom_sector_type_t type) {
uint8_t *data = (uint8_t *)sector;
uint32_t edc;
switch (type) {
case PSX_CDROM_SECTOR_TYPE_MODE1:
edc = edc_crc32(data, 0x810);
data[0x810] = (uint8_t)(edc);
data[0x811] = (uint8_t)(edc >> 8);
data[0x812] = (uint8_t)(edc >> 16);
data[0x813] = (uint8_t)(edc >> 24);
memset(sector + 0x814, 0, 8);
// TODO: ECC
} break;
case PSX_CDROM_SECTOR_TYPE_MODE2_FORM1: {
uint32_t edc = psx_cdrom_calculate_edc(sector, 0x10, 0x808);
sector[0x818] = (uint8_t)(edc);
sector[0x819] = (uint8_t)(edc >> 8);
sector[0x81A] = (uint8_t)(edc >> 16);
sector[0x81B] = (uint8_t)(edc >> 24);
break;
case PSX_CDROM_SECTOR_TYPE_MODE2_FORM1:
edc = edc_crc32(data + 0x10, 0x808);
data[0x818] = (uint8_t)(edc);
data[0x819] = (uint8_t)(edc >> 8);
data[0x81A] = (uint8_t)(edc >> 16);
data[0x81B] = (uint8_t)(edc >> 24);
// TODO: ECC
} break;
case PSX_CDROM_SECTOR_TYPE_MODE2_FORM2: {
uint32_t edc = psx_cdrom_calculate_edc(sector, 0x10, 0x91C);
sector[0x92C] = (uint8_t)(edc);
sector[0x92D] = (uint8_t)(edc >> 8);
sector[0x92E] = (uint8_t)(edc >> 16);
sector[0x92F] = (uint8_t)(edc >> 24);
} break;
break;
case PSX_CDROM_SECTOR_TYPE_MODE2_FORM2:
edc = edc_crc32(data + 0x10, 0x91C);
data[0x92C] = (uint8_t)(edc);
data[0x92D] = (uint8_t)(edc >> 8);
data[0x92E] = (uint8_t)(edc >> 16);
data[0x92F] = (uint8_t)(edc >> 24);
break;
}
}
}

View File

@@ -21,8 +21,7 @@ freely, subject to the following restrictions:
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef __LIBPSXAV_H__
#define __LIBPSXAV_H__
#pragma once
#include <stdbool.h>
#include <stdint.h>
@@ -106,6 +105,11 @@ typedef struct {
uint8_t data[0x918];
} psx_cdrom_sector_mode2_t;
typedef union {
psx_cdrom_sector_mode1_t mode1;
psx_cdrom_sector_mode2_t mode2;
} psx_cdrom_sector_t;
_Static_assert(sizeof(psx_cdrom_sector_mode1_t) == PSX_CDROM_SECTOR_SIZE, "Invalid Mode1 sector size");
_Static_assert(sizeof(psx_cdrom_sector_mode2_t) == PSX_CDROM_SECTOR_SIZE, "Invalid Mode2 sector size");
@@ -137,6 +141,5 @@ typedef enum {
PSX_CDROM_SECTOR_TYPE_MODE2_FORM2
} psx_cdrom_sector_type_t;
void psx_cdrom_calculate_checksums(uint8_t *sector, psx_cdrom_sector_type_t type);
#endif /* __LIBPSXAV_H__ */
void psx_cdrom_init_sector(psx_cdrom_sector_t *sector, int lba, psx_cdrom_sector_type_t type);
void psx_cdrom_calculate_checksums(psx_cdrom_sector_t *sector, psx_cdrom_sector_type_t type);