UrForth: "STRLITERAL" now respects the optimiser
[urasm.git] / src / libfdc / dskldr_trd.c
blob8efc70a6651114b26dbd23c75413a68d33b9a6f3
1 /*
2 * disk images I/O
3 * coded by Ketmar // Invisible Vector
4 * Understanding is not required. Only obedience.
6 * This program is free software. It comes without any warranty, to
7 * the extent permitted by applicable law. You can redistribute it
8 * and/or modify it under the terms of the Do What The Fuck You Want
9 * To Public License, Version 2, as published by Sam Hocevar. See
10 * http://sam.zoy.org/wtfpl/COPYING for more details.
12 #include "dskldr.h"
13 #include "dskfs_all.h"
16 //==========================================================================
18 // dskLoadTRD
20 //==========================================================================
21 int dskLoadTRD (Floppy *flp, FILE *fl) {
22 uint8_t trkBuf[FLP_TRD_TRACK_SIZE], b;
23 if (fl == NULL) return FLPERR_SHIT;
24 if (fseek(fl, 0x8e7, SEEK_CUR) != 0) {
25 libfdcMsg(LIBFDC_MSG_ERROR, "cannot seek to TRD header");
26 return FLPERR_SHIT;
28 if (fread(&b, 1, 1, fl) != 1) {
29 libfdcMsg(LIBFDC_MSG_ERROR, "cannot read TRD header signature byte");
30 return FLPERR_SHIT;
32 if (b != 0x10) {
33 // TRD signature byte not found
34 libfdcMsg(LIBFDC_MSG_ERROR, "TRD signature byte not found");
35 return FLPERR_SHIT;
37 if (fseek(fl, -0x8e8, SEEK_CUR) != 0) {
38 libfdcMsg(LIBFDC_MSG_ERROR, "cannot seek to TRD start");
39 return FLPERR_SHIT;
41 if (flpFormatTRD(flp) != FLPERR_OK) {
42 libfdcMsg(LIBFDC_MSG_ERROR, "cannot format emulated floppy");
43 return FLPERR_SHIT;
45 //FIXME: TODO: single-sided, 43
46 for (int i = 0; i < FLP_MAX_TRACKS_80; ++i) {
47 size_t rd = fread(trkBuf, 1, FLP_TRD_TRACK_SIZE, fl);
48 if (rd < 1) {
49 if (i == 0) {
50 flpFormatTRD(flp);
51 flp->changed = 0;
52 flp->insert = 0;
53 return FLPERR_SHIT;
55 break;
57 if (rd < FLP_TRD_TRACK_SIZE) memset(trkBuf+rd, 0, FLP_TRD_TRACK_SIZE-rd);
58 if (flpFormatTRDTrack(flp, i, trkBuf, sizeof(trkBuf)) != FLPERR_OK) return FLPERR_SHIT;
59 if (rd < FLP_TRD_TRACK_SIZE) break; // no more bytes in file anyway
61 flp->insert = 1;
62 flp->changed = 0;
63 return FLPERR_OK;
67 //==========================================================================
69 // dskSaveTRD
71 //==========================================================================
72 int dskSaveTRD (Floppy *flp, FILE *fl) {
73 if (!flp || !flp->insert) return FLPERR_SHIT;
74 if (fl == NULL) return FLPERR_SHIT;
75 for (int trk = 0; trk < 80*2; ++trk) {
76 for (int sec = 1; sec < 17; ++sec) {
77 uint8_t sdata[256];
78 if (flpGetSectorData(flp, trk, sec, sdata, 256) != FLPERR_OK) return FLPERR_SHIT;
79 if (fwrite(sdata, 256, 1, fl) != 1) return FLPERR_SHIT;
82 flp->changed = 0;
83 return FLPERR_OK;