comparison sidlib.c @ 386:94ff750679a6

Implement sidlib_write_sid_header() function.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 01 Jan 2022 16:25:42 +0200
parents 1049033c6bbd
children c96448875797
comparison
equal deleted inserted replaced
385:1049033c6bbd 386:94ff750679a6
1205 item = bsearch(&key, dbh->pindex, dbh->nnodes, 1205 item = bsearch(&key, dbh->pindex, dbh->nnodes,
1206 sizeof(SIDLibSTILNode *), sidlib_stildb_compare_nodes); 1206 sizeof(SIDLibSTILNode *), sidlib_stildb_compare_nodes);
1207 1207
1208 return item != NULL ? *item : NULL; 1208 return item != NULL ? *item : NULL;
1209 } 1209 }
1210
1211
1212 int sidlib_write_sid_header(th_ioctx *ctx, const SIDLibPSIDHeader *psid)
1213 {
1214 int ret = THERR_OK;
1215 SIDLibPSIDHeader hsid;
1216
1217 if (psid->version < 1 || psid->version > 4)
1218 {
1219 ret = th_io_error(ctx, THERR_INVALID_DATA,
1220 "Invalid or unsupported SID version %d",
1221 psid->version);
1222 goto exit;
1223 }
1224
1225 if (psid->isRSID && psid->version < 2)
1226 {
1227 ret = th_io_error(ctx, THERR_INVALID_DATA,
1228 "Invalid SID, RSID set, but version %d < 2?",
1229 psid->version);
1230 goto exit;
1231 }
1232
1233 // Write header
1234 hsid.magic[0] = psid->isRSID ? 'R' : 'P';
1235 hsid.magic[1] = 'S';
1236 hsid.magic[2] = 'I';
1237 hsid.magic[3] = 'D';
1238 hsid.dataOffset =
1239 4 + 7*2 + 4 + 3*SIDLIB_PSID_STR_LEN;
1240
1241 if (psid->version >= 2)
1242 hsid.dataOffset += 2 + 4*1;
1243
1244 if (!thfwrite_str(ctx, (uint8_t *) hsid.magic, SIDLIB_PSID_MAGIC_LEN) ||
1245 !thfwrite_be16(ctx, psid->version) ||
1246 !thfwrite_be16(ctx, hsid.dataOffset) ||
1247 !thfwrite_be16(ctx, psid->loadAddress) ||
1248 !thfwrite_be16(ctx, psid->initAddress) ||
1249 !thfwrite_be16(ctx, psid->playAddress) ||
1250 !thfwrite_be16(ctx, psid->nSongs) ||
1251 !thfwrite_be16(ctx, psid->startSong) ||
1252 !thfwrite_be32(ctx, psid->speed) ||
1253 !thfwrite_str(ctx, psid->sidName, SIDLIB_PSID_STR_LEN) ||
1254 !thfwrite_str(ctx, psid->sidAuthor, SIDLIB_PSID_STR_LEN) ||
1255 !thfwrite_str(ctx, psid->sidReleased, SIDLIB_PSID_STR_LEN))
1256 {
1257 ret = th_io_error(ctx, ctx->status,
1258 "Could not write PSID/RSID header.");
1259 goto exit;
1260 }
1261
1262 // Check if we need to load PSIDv2NG header ...
1263 if (psid->version >= 2)
1264 {
1265 // Yes, we need to
1266 if (!thfwrite_be16(ctx, psid->flags) ||
1267 !thfwrite_u8(ctx, psid->startPage) ||
1268 !thfwrite_u8(ctx, psid->pageLength) ||
1269 !thfwrite_u8(ctx, psid->sid2Addr) ||
1270 !thfwrite_u8(ctx, psid->sid3Addr))
1271 {
1272 ret = th_io_error(ctx, ctx->status,
1273 "Error writing PSID/RSID v2+ extra header data.");
1274 goto exit;
1275 }
1276 }
1277
1278 exit:
1279 return ret;
1280 }