fork of PCE focusing on macplus, supporting DaynaPort SCSI network emulation
1/*****************************************************************************
2 * pce *
3 *****************************************************************************/
4
5/*****************************************************************************
6 * File name: src/utils/psi/load.c *
7 * Created: 2013-06-09 by Hampa Hug <hampa@hampa.ch> *
8 * Copyright: (C) 2013-2017 Hampa Hug <hampa@hampa.ch> *
9 *****************************************************************************/
10
11/*****************************************************************************
12 * This program is free software. You can redistribute it and / or modify it *
13 * under the terms of the GNU General Public License version 2 as published *
14 * by the Free Software Foundation. *
15 * *
16 * This program is distributed in the hope that it will be useful, but *
17 * WITHOUT ANY WARRANTY, without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
19 * Public License for more details. *
20 *****************************************************************************/
21
22
23#include "main.h"
24#include "load.h"
25
26#include <stdlib.h>
27#include <stdio.h>
28#include <string.h>
29
30#include <drivers/psi/psi.h>
31
32
33static
34int psi_load_sectors_cb (psi_img_t *img, psi_sct_t *sct,
35 unsigned c, unsigned h, unsigned s, unsigned a, void *opaque)
36{
37 FILE *fp;
38
39 fp = opaque;
40
41 psi_sct_fill (sct, 0);
42
43 if (fread (sct->data, 1, sct->n, fp) != sct->n) {
44 ;
45 }
46
47 par_cnt += 1;
48
49 return (0);
50}
51
52int psi_load_sectors (psi_img_t *img, const char *fname)
53{
54 int r;
55 FILE *fp;
56
57 fp = fopen (fname, "rb");
58
59 if (fp == NULL) {
60 fprintf (stderr, "%s: can't open file (%s)\n", arg0, fname);
61 return (1);
62 }
63
64 par_cnt = 0;
65
66 r = psi_for_all_sectors (img, psi_load_sectors_cb, fp);
67
68 fclose (fp);
69
70 if (par_verbose) {
71 fprintf (stderr, "%s: load %lu sectors\n", arg0, par_cnt);
72 }
73
74 if (r) {
75 fprintf (stderr, "%s: loading sectors failed\n", arg0);
76 }
77
78 return (r);
79}
80
81
82static
83int psi_load_weak_cb (psi_img_t *img, psi_sct_t *sct,
84 unsigned c, unsigned h, unsigned s, unsigned a, void *opaque)
85{
86 FILE *fp;
87
88 fp = opaque;
89
90 if (psi_weak_alloc (sct)) {
91 return (1);
92 }
93
94 if (fread (sct->weak, 1, sct->n, fp) != sct->n) {
95 ;
96 }
97
98 par_cnt += 1;
99
100 return (0);
101}
102
103int psi_load_weak (psi_img_t *img, const char *fname)
104{
105 int r;
106 FILE *fp;
107
108 if ((fp = fopen (fname, "rb")) == NULL) {
109 fprintf (stderr, "%s: can't open file (%s)\n", arg0, fname);
110 return (1);
111 }
112
113 par_cnt = 0;
114
115 r = psi_for_all_sectors (img, psi_load_weak_cb, fp);
116
117 fclose (fp);
118
119 if (par_verbose) {
120 fprintf (stderr, "%s: load %lu weak masks\n", arg0, par_cnt);
121 }
122
123 if (r) {
124 fprintf (stderr, "%s: loading weak masks failed\n", arg0);
125 }
126
127 return (r);
128}
129
130
131static
132int psi_load_tags_cb (psi_img_t *img, psi_sct_t *sct,
133 unsigned c, unsigned h, unsigned s, unsigned a, void *opaque)
134{
135 FILE *fp;
136 unsigned char buf[12];
137
138 fp = opaque;
139
140 if (fread (buf, 1, 12, fp) != 12) {
141 return (1);
142 }
143
144 psi_sct_set_tags (sct, buf, 12);
145
146 par_cnt += 1;
147
148 return (0);
149}
150
151int psi_load_tags (psi_img_t *img, const char *fname)
152{
153 int r;
154 FILE *fp;
155
156 fp = fopen (fname, "rb");
157
158 if (fp == NULL) {
159 fprintf (stderr, "%s: can't open file (%s)\n", arg0, fname);
160 return (1);
161 }
162
163 par_cnt = 0;
164
165 r = psi_for_all_sectors (img, psi_load_tags_cb, fp);
166
167 fclose (fp);
168
169 if (par_verbose) {
170 fprintf (stderr, "%s: load %lu sector tags\n", arg0, par_cnt);
171 }
172
173 if (r) {
174 fprintf (stderr, "%s: loading sector tags failed\n", arg0);
175 }
176
177 return (r);
178}