fork of PCE focusing on macplus, supporting DaynaPort SCSI network emulation
at master 215 lines 4.5 kB view raw
1/***************************************************************************** 2 * pce * 3 *****************************************************************************/ 4 5/***************************************************************************** 6 * File name: src/utils/pri/text-raw.c * 7 * Created: 2017-10-29 by Hampa Hug <hampa@hampa.ch> * 8 * Copyright: (C) 2017-2023 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 <config.h> 24 25#include <stdlib.h> 26#include <stdio.h> 27#include <string.h> 28 29#include <drivers/pri/pri.h> 30 31#include <lib/text.h> 32 33#include "main.h" 34#include "text.h" 35 36 37static 38void raw_dec_byte (pri_text_t *ctx) 39{ 40 unsigned char val; 41 42 val = (ctx->shift >> (ctx->shift_cnt - 8)) & 0xff; 43 44 ctx->last_val = val & 1; 45 ctx->shift_cnt -= 8; 46 47 if (ctx->column > 0) { 48 if (ctx->column >= 16) { 49 fputc ('\n', ctx->txt.fp); 50 ctx->column = 0; 51 } 52 else { 53 fputc (' ', ctx->txt.fp); 54 } 55 } 56 57 fprintf (ctx->txt.fp, "%02X", val); 58 59 ctx->column += 1; 60} 61 62void raw_dec_flush (pri_text_t *ctx) 63{ 64 while (ctx->shift_cnt >= 8) { 65 raw_dec_byte (ctx); 66 } 67 68 if (ctx->shift_cnt > 0) { 69 txt_dec_bits (ctx, ctx->shift_cnt); 70 } 71} 72 73int txt_raw_dec_track (pri_text_t *ctx) 74{ 75 unsigned long bit; 76 unsigned long type, val; 77 78 fprintf (ctx->txt.fp, "TRACK %lu %lu\n\n", ctx->c, ctx->h); 79 fprintf (ctx->txt.fp, "MODE RAW\n"); 80 fprintf (ctx->txt.fp, "RATE %lu\n\n", pri_trk_get_clock (ctx->trk)); 81 82 ctx->shift_cnt = 0; 83 ctx->last_val = 0; 84 85 ctx->column = 0; 86 ctx->need_nl = 0; 87 88 pri_trk_set_pos (ctx->trk, 0); 89 90 while (ctx->trk->wrap == 0) { 91 while (pri_trk_get_event (ctx->trk, &type, &val) == 0) { 92 raw_dec_flush (ctx); 93 txt_dec_event (ctx, type, val); 94 } 95 96 pri_trk_get_bits (ctx->trk, &bit, 1); 97 98 ctx->shift = (ctx->shift << 1) | (bit & 1); 99 ctx->shift_cnt += 1; 100 101 if (ctx->shift_cnt >= 8) { 102 raw_dec_byte (ctx); 103 } 104 } 105 106 raw_dec_flush (ctx); 107 108 if (ctx->column > 0) { 109 fputc ('\n', ctx->txt.fp); 110 } 111 112 return (0); 113} 114 115static 116int raw_enc_bit (pri_text_t *ctx) 117{ 118 unsigned long val; 119 120 while (txt_match_eol (&ctx->txt) == 0) { 121 if (txt_match_uint (&ctx->txt, 16, &val) == 0) { 122 return (1); 123 } 124 125 if ((val != 0) && (val != 1)) { 126 return (1); 127 } 128 129 if (txt_enc_bits_raw (ctx, val, 1)) { 130 return (1); 131 } 132 } 133 134 return (0); 135} 136 137static 138int raw_enc_fill (pri_text_t *ctx) 139{ 140 unsigned long max; 141 unsigned long val; 142 143 if (txt_match_uint (&ctx->txt, 10, &max) == 0) { 144 return (1); 145 } 146 147 if (txt_match_uint (&ctx->txt, 16, &val) == 0) { 148 return (1); 149 } 150 151 max *= 8; 152 153 while (ctx->bit_cnt < max) { 154 if (txt_enc_bits_raw (ctx, val, 8)) { 155 return (1); 156 } 157 } 158 159 return (0); 160} 161 162static 163int raw_enc_hex (pri_text_t *ctx, unsigned val) 164{ 165 if (txt_enc_bits_raw (ctx, val, 8)) { 166 return (1); 167 } 168 169 return (0); 170} 171 172static 173int raw_enc_rep (pri_text_t *ctx) 174{ 175 unsigned long cnt; 176 unsigned long val; 177 178 if (txt_match_uint (&ctx->txt, 10, &cnt) == 0) { 179 return (1); 180 } 181 182 if (txt_match_uint (&ctx->txt, 16, &val) == 0) { 183 return (1); 184 } 185 186 while (cnt > 0) { 187 if (txt_enc_bits_raw (ctx, val, 8)) { 188 return (1); 189 } 190 191 cnt -= 1; 192 } 193 194 return (0); 195} 196 197int txt_encode_pri0_raw (pri_text_t *ctx) 198{ 199 unsigned long val; 200 201 if (txt_match (&ctx->txt, "BIT", 1)) { 202 return (raw_enc_bit (ctx)); 203 } 204 else if (txt_match (&ctx->txt, "FILL", 1)) { 205 return (raw_enc_fill (ctx)); 206 } 207 else if (txt_match (&ctx->txt, "REP", 1)) { 208 return (raw_enc_rep (ctx)); 209 } 210 else if (txt_match_uint (&ctx->txt, 16, &val)) { 211 return (raw_enc_hex (ctx, val)); 212 } 213 214 return (-1); 215}