fork of PCE focusing on macplus, supporting DaynaPort SCSI network emulation
at master 194 lines 4.1 kB view raw
1/***************************************************************************** 2 * pce * 3 *****************************************************************************/ 4 5/***************************************************************************** 6 * File name: src/utils/pti/space.c * 7 * Created: 2020-05-04 by Hampa Hug <hampa@hampa.ch> * 8 * Copyright: (C) 2020-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#include "space.h" 25 26#include <stdlib.h> 27#include <stdio.h> 28#include <string.h> 29 30#include <drivers/pti/pti.h> 31 32 33int pti_space_add_left (pti_img_t *img, unsigned long clk) 34{ 35 unsigned long i; 36 unsigned long tmp; 37 int level; 38 39 if (img->pulse_cnt > 0) { 40 pti_pulse_get (img->pulse[0], &tmp, &level); 41 42 if (level == 0) { 43 pti_pulse_set (img->pulse, tmp + clk, 0); 44 return (0); 45 } 46 } 47 48 if (pti_img_set_pulse_max (img, img->pulse_cnt + 1)) { 49 return (1); 50 } 51 52 i = img->pulse_cnt++; 53 54 while (i > 0) { 55 img->pulse[i] = img->pulse[i - 1]; 56 i -= 1; 57 } 58 59 pti_pulse_set (img->pulse, clk, 0); 60 61 return (0); 62} 63 64int pti_space_add_right (pti_img_t *img, unsigned long clk) 65{ 66 unsigned long tmp; 67 int level; 68 69 if (img->pulse_cnt > 0) { 70 pti_pulse_get (img->pulse[img->pulse_cnt - 1], &tmp, &level); 71 72 if (level == 0) { 73 pti_pulse_set (img->pulse + img->pulse_cnt - 1, tmp + clk, level); 74 return (0); 75 } 76 } 77 78 if (pti_img_add_pulse (img, clk, 0)) { 79 return (1); 80 } 81 82 return (0); 83} 84 85int pti_space_add (pti_img_t *img, unsigned long clk) 86{ 87 if (pti_space_add_left (img, clk)) { 88 return (1); 89 } 90 91 if (pti_space_add_right (img, clk)) { 92 return (1); 93 } 94 95 return (0); 96} 97 98int pti_space_auto (pti_img_t *img, unsigned long min) 99{ 100 unsigned long i; 101 unsigned long clk; 102 int level; 103 104 for (i = 0; i < img->pulse_cnt; i++) { 105 pti_pulse_get (img->pulse[i], &clk, &level); 106 107 if (clk >= min) { 108 pti_pulse_set (img->pulse + i, clk, 0); 109 } 110 } 111 112 return (0); 113} 114 115int pti_space_max (pti_img_t *img, unsigned long max) 116{ 117 unsigned long i; 118 unsigned long clk; 119 int level; 120 121 for (i = 0; i < img->pulse_cnt; i++) { 122 pti_pulse_get (img->pulse[i], &clk, &level); 123 124 if ((level == 0) && (clk > max)) { 125 pti_pulse_set (img->pulse + i, max, level); 126 } 127 } 128 129 return (0); 130} 131 132int pti_space_trim_left (pti_img_t *img) 133{ 134 unsigned long i, j; 135 unsigned long clk; 136 int level; 137 138 i = 0; 139 140 while (i < img->pulse_cnt) { 141 pti_pulse_get (img->pulse[i], &clk, &level); 142 143 if (level != 0) { 144 break; 145 } 146 147 i += 1; 148 } 149 150 if (i == 0) { 151 return (0); 152 } 153 154 j = 0; 155 156 while (i < img->pulse_cnt) { 157 img->pulse[j++] = img->pulse[i++]; 158 } 159 160 img->pulse_cnt = j; 161 162 return (0); 163} 164 165int pti_space_trim_right (pti_img_t *img) 166{ 167 unsigned long clk; 168 int level; 169 170 while (img->pulse_cnt > 0) { 171 pti_pulse_get (img->pulse[img->pulse_cnt - 1], &clk, &level); 172 173 if (level != 0) { 174 break; 175 } 176 177 img->pulse_cnt -= 1; 178 } 179 180 return (0); 181} 182 183int pti_space_trim (pti_img_t *img) 184{ 185 if (pti_space_trim_right (img)) { 186 return (1); 187 } 188 189 if (pti_space_trim_left (img)) { 190 return (1); 191 } 192 193 return (0); 194}