fork of PCE focusing on macplus, supporting DaynaPort SCSI network emulation
1/*****************************************************************************
2 * pce *
3 *****************************************************************************/
4
5/*****************************************************************************
6 * File name: src/utils/pfi/encode.c *
7 * Created: 2014-01-03 by Hampa Hug <hampa@hampa.ch> *
8 * Copyright: (C) 2014-2022 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
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
28
29#include <drivers/pfi/pfi.h>
30#include <drivers/pfi/decode-bits.h>
31
32#include <drivers/pri/pri.h>
33#include <drivers/pri/pri-img.h>
34
35
36static
37int pfi_encode_track (pfi_trk_t *dtrk, pri_trk_t *strk, unsigned c, unsigned h)
38{
39 unsigned long i, j, n;
40 unsigned long dclk, sclk;
41 unsigned long dval, sval, total;
42 unsigned long s1, s2;
43 const unsigned char *s;
44
45 pfi_trk_reset (dtrk);
46 pfi_trk_set_clock (dtrk, par_pfi_clock);
47
48 sclk = strk->clock;
49 dclk = dtrk->clock;
50
51 sval = 0;
52 dval = 0;
53
54 total = 0;
55
56 s1 = (unsigned long) (par_slack1 * strk->clock + 0.5);
57 s2 = (unsigned long) (par_slack2 * strk->clock + 0.5);
58
59 j = (strk->size - s1 % strk->size) % strk->size;
60 n = par_revolution * strk->size;
61 n += s1 + s2;
62 s = strk->data;
63
64 for (i = 0; i < n; i++) {
65 sval += dclk;
66 dval += sval / sclk;
67 sval %= sclk;
68
69 if (s[j >> 3] & (0x80 >> (j & 7))) {
70 if (pfi_trk_add_pulse (dtrk, dval)) {
71 return (1);
72 }
73
74 total += dval;
75 dval = 0;
76 }
77
78 j += 1;
79
80 if (j >= strk->size) {
81 pfi_trk_add_index (dtrk, total + dval);
82 j = 0;
83 }
84 }
85
86 return (0);
87}
88
89static
90int pfi_encode_pri (pfi_img_t *dimg, const char *fname)
91{
92 unsigned c, h;
93 pri_img_t *simg;
94 pri_cyl_t *cyl;
95 pri_trk_t *strk;
96 pfi_trk_t *dtrk;
97
98 if ((simg = pri_img_load (fname, PRI_FORMAT_NONE)) == NULL) {
99 return (1);
100 }
101
102 for (c = 0; c < simg->cyl_cnt; c++) {
103 if ((cyl = simg->cyl[c]) == NULL) {
104 continue;
105 }
106
107 for (h = 0; h < cyl->trk_cnt; h++) {
108 if ((strk = cyl->trk[h]) == NULL) {
109 continue;
110 }
111
112 dtrk = pfi_img_get_track (dimg, c, h, 1);
113
114 if (dtrk == NULL) {
115 return (1);
116 }
117
118 if (pfi_encode_track (dtrk, strk, c, h)) {
119 return (1);
120 }
121 }
122 }
123
124 return (0);
125}
126
127int pfi_encode (pfi_img_t *img, const char *type, const char *fname)
128{
129 if (strcmp (type, "pri") == 0) {
130 return (pfi_encode_pri (img, fname));
131 }
132 else if (strcmp (type, "text") == 0) {
133 return (pfi_encode_text (img, fname));
134 }
135
136 return (1);
137}