[3ae31e9] | 1 | /*
|
---|
| 2 | ============================================================================
|
---|
| 3 | newsine.c -- Buchla 700 sine PROM generator
|
---|
| 4 | see VER for version message -- written by: D.N. Lynx Crowe
|
---|
| 5 |
|
---|
| 6 | Creates two Motorola S-Record files for the Sine PROM:
|
---|
| 7 | one for the MS bytes, and one for the LS bytes.
|
---|
| 8 | ============================================================================
|
---|
| 9 | */
|
---|
| 10 |
|
---|
| 11 | #define VER "Version 10 -- 1987-12-19"
|
---|
| 12 |
|
---|
| 13 | #include "stdio.h"
|
---|
| 14 | #include "limits.h"
|
---|
| 15 | #include "math.h"
|
---|
| 16 | #include "stddefs.h"
|
---|
| 17 |
|
---|
| 18 | #define VOID void
|
---|
| 19 |
|
---|
| 20 | #define TWOPI ((double)2.0 * (double)PI) /* 2 * PI */
|
---|
| 21 |
|
---|
| 22 | #define ISHIFT ((double)PI * (double)0.625) /* phase shift */
|
---|
| 23 | #define OSCALE ((double)30000.0) /* output scale factor */
|
---|
| 24 |
|
---|
| 25 | #define BRLEN 15 /* address length */
|
---|
| 26 | #define TABLEN 32768 /* sine table length (integer) */
|
---|
| 27 | #define DTABLEN ((double)32768.0) /* sine table length (real) */
|
---|
| 28 |
|
---|
| 29 | #define LO_FILE "SINELO.MOT" /* LS byte file name */
|
---|
| 30 | #define HI_FILE "SINEHI.MOT" /* MS byte file name */
|
---|
| 31 | #define THE_LOG "SINE.LOG" /* log file name */
|
---|
| 32 |
|
---|
| 33 | #define RECLEN 32 /* 32 bytes per S-record */
|
---|
| 34 | #define BUFLEN 512 /* LS/MS buffer length */
|
---|
| 35 |
|
---|
| 36 | char *fname1; /* file name for LS bytes */
|
---|
| 37 | char *fname2; /* file name for MS bytes */
|
---|
| 38 | char *fname3; /* file name for log */
|
---|
| 39 |
|
---|
| 40 | int t[TABLEN]; /* sine table */
|
---|
| 41 |
|
---|
| 42 | char hi_buf[BUFLEN]; /* buffer for MS bytes */
|
---|
| 43 | char lo_buf[BUFLEN]; /* buffer for LS bytes */
|
---|
| 44 |
|
---|
| 45 | int csum; /* current record checksum */
|
---|
| 46 |
|
---|
| 47 | unsigned bitmask[] = { /* bit mask table for bitrev */
|
---|
| 48 |
|
---|
| 49 | 0x0001, 0x0002, 0x0004, 0x0008,
|
---|
| 50 | 0x0010, 0x0020, 0x0040, 0x0080,
|
---|
| 51 | 0x0100, 0x0200, 0x0400, 0x0800,
|
---|
| 52 | 0x1000, 0x2000, 0x4000, 0x8000
|
---|
| 53 | };
|
---|
| 54 |
|
---|
| 55 | char hexdig[] = "0123456789ABCDEF"; /* hex table */
|
---|
| 56 |
|
---|
| 57 | /* |
---|
| 58 |
|
---|
| 59 | */
|
---|
| 60 |
|
---|
| 61 | /*
|
---|
| 62 | ============================================================================
|
---|
| 63 | msdone() -- outputs the final S-Record
|
---|
| 64 | ============================================================================
|
---|
| 65 | */
|
---|
| 66 |
|
---|
| 67 | VOID
|
---|
| 68 | msdone(fp)
|
---|
| 69 | FILE *fp;
|
---|
| 70 | {
|
---|
| 71 | fprintf(fp, "S904000000FB\n"); /* end of S-records */
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | /*
|
---|
| 75 | ============================================================================
|
---|
| 76 | outhex(fp,val) -- output a byte in ASCII hex
|
---|
| 77 |
|
---|
| 78 | Outputs the byte 'val' on file 'fp' in ASCII hex and updates
|
---|
| 79 | the accumulated checksum csum.
|
---|
| 80 | ============================================================================
|
---|
| 81 | */
|
---|
| 82 |
|
---|
| 83 | VOID
|
---|
| 84 | outhex(fp,val)
|
---|
| 85 | FILE *fp; /* file pointer for output */
|
---|
| 86 | unsigned int val; /* byte to be output */
|
---|
| 87 | {
|
---|
| 88 | fputc(hexdig[(val >> 4) & 0x0F], fp);
|
---|
| 89 | fputc(hexdig[val & 0x0F], fp);
|
---|
| 90 | csum += (val & 0x0FF);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | /* |
---|
| 94 |
|
---|
| 95 | */
|
---|
| 96 |
|
---|
| 97 | /*
|
---|
| 98 | ============================================================================
|
---|
| 99 | outrec(fp,adr,len,buf) -- output a Motorola S-record
|
---|
| 100 |
|
---|
| 101 | Outputs len bytes from buffer buf to file fp with S-record
|
---|
| 102 | address adr.
|
---|
| 103 | ============================================================================
|
---|
| 104 | */
|
---|
| 105 |
|
---|
| 106 | VOID
|
---|
| 107 | outrec(fp,adr,len,buf)
|
---|
| 108 | FILE *fp; /* file pointer for output */
|
---|
| 109 | long adr; /* beginning address for S-record */
|
---|
| 110 | int len; /* length of data in record */
|
---|
| 111 | char *buf; /* buffer address of record */
|
---|
| 112 | {
|
---|
| 113 | int i;
|
---|
| 114 |
|
---|
| 115 | csum = 0; /* zero the checksum */
|
---|
| 116 | fprintf(fp, "S2"); /* record header */
|
---|
| 117 | outhex(fp, (unsigned int)(len+4)); /* record length */
|
---|
| 118 |
|
---|
| 119 | /* record address */
|
---|
| 120 | outhex(fp, (unsigned int)((adr >> 16) & 0x0FFL));
|
---|
| 121 | outhex(fp, (unsigned int)((adr >> 8) & 0x0FFL));
|
---|
| 122 | outhex(fp, (unsigned int)(adr & 0x0FFL));
|
---|
| 123 |
|
---|
| 124 | /* data */
|
---|
| 125 | for (i = 0; i < len; i++)
|
---|
| 126 | outhex(fp, (unsigned int)*buf++);
|
---|
| 127 |
|
---|
| 128 | outhex(fp, (~csum) & 0x0FFL); /* checksum */
|
---|
| 129 | fprintf(fp, "\n"); /* CR/LF */
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | /* |
---|
| 133 |
|
---|
| 134 | */
|
---|
| 135 |
|
---|
| 136 | /*
|
---|
| 137 | ============================================================================
|
---|
| 138 | msrec(fp,adr,len,buf) -- output data in Motorola S-record format
|
---|
| 139 |
|
---|
| 140 | Outputs len bytes of data from buffer buf to file fp with an initial
|
---|
| 141 | S-record address of adr.
|
---|
| 142 | ============================================================================
|
---|
| 143 | */
|
---|
| 144 |
|
---|
| 145 | VOID
|
---|
| 146 | msrec(fp,adr,len,buf)
|
---|
| 147 | FILE *fp; /* file pointer for output */
|
---|
| 148 | long adr; /* beginning address for S-records */
|
---|
| 149 | long len; /* length of data in buffer */
|
---|
| 150 | char *buf; /* buffer address */
|
---|
| 151 | {
|
---|
| 152 | long recadr = adr;
|
---|
| 153 | char *rp = buf;
|
---|
| 154 | int j;
|
---|
| 155 | long i = len;
|
---|
| 156 |
|
---|
| 157 | while (i) { /* while there's data ... */
|
---|
| 158 |
|
---|
| 159 | if (i GE RECLEN) { /* full record */
|
---|
| 160 |
|
---|
| 161 | outrec(fp, recadr, RECLEN, rp);
|
---|
| 162 | i -= RECLEN;
|
---|
| 163 | rp += RECLEN;
|
---|
| 164 | recadr += RECLEN;
|
---|
| 165 |
|
---|
| 166 | } else { /* final short record */
|
---|
| 167 |
|
---|
| 168 | j = i;
|
---|
| 169 | outrec(fp, recadr, j ,rp);
|
---|
| 170 | i = 0;
|
---|
| 171 |
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | /* |
---|
| 177 |
|
---|
| 178 | */
|
---|
| 179 |
|
---|
| 180 | /*
|
---|
| 181 | =============================================================================
|
---|
| 182 | bitrev(bitsin, nbits) -- reverses the rightmost nbits of bitsin.
|
---|
| 183 |
|
---|
| 184 | Any bits to the left of the reversed bits in the result will be zeros.
|
---|
| 185 | =============================================================================
|
---|
| 186 | */
|
---|
| 187 |
|
---|
| 188 | unsigned
|
---|
| 189 | bitrev(bitsin, nbits)
|
---|
| 190 | unsigned bitsin, nbits;
|
---|
| 191 | {
|
---|
| 192 | unsigned m, n;
|
---|
| 193 |
|
---|
| 194 | n = 0;
|
---|
| 195 |
|
---|
| 196 | for (m = 0; m < nbits; m++)
|
---|
| 197 | if (bitsin & bitmask[m])
|
---|
| 198 | n |= bitmask[nbits-1-m];
|
---|
| 199 |
|
---|
| 200 | return(n);
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | /* |
---|
| 204 |
|
---|
| 205 | */
|
---|
| 206 |
|
---|
| 207 | /*
|
---|
| 208 | =============================================================================
|
---|
| 209 | main function -- sine.c -- Buchla 700 sine PROM generator
|
---|
| 210 | =============================================================================
|
---|
| 211 | */
|
---|
| 212 |
|
---|
| 213 | main()
|
---|
| 214 | {
|
---|
| 215 | FILE *fp1, *fp2, *fp3;
|
---|
| 216 | register long i, j, nr;
|
---|
| 217 | double k, q, ip, os;
|
---|
| 218 |
|
---|
| 219 | printf("Buchla 700 Sine PROM Generator %s\n", VER);
|
---|
| 220 |
|
---|
| 221 | q = TWOPI / DTABLEN; /* step size */
|
---|
| 222 | ip = ISHIFT; /* offset */
|
---|
| 223 | os = OSCALE; /* output scaling */
|
---|
| 224 |
|
---|
| 225 | fname1 = LO_FILE; /* LS byte file */
|
---|
| 226 | fname2 = HI_FILE; /* MS byte file */
|
---|
| 227 | fname3 = THE_LOG; /* log file */
|
---|
| 228 |
|
---|
| 229 | if( (fp1 = fopen(fname1, "wa")) == NULL) {
|
---|
| 230 |
|
---|
| 231 | printf("sine: ERROR - couldn't open [%s]\n", fname1);
|
---|
| 232 | exit(1);
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | if( (fp2 = fopen(fname2, "wa")) == NULL) {
|
---|
| 236 |
|
---|
| 237 | printf("sine: ERROR - couldn't open [%s]\n", fname2);
|
---|
| 238 | fclose(fp1);
|
---|
| 239 | exit(1);
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | if( (fp3 = fopen(fname3, "wa")) == NULL) {
|
---|
| 243 |
|
---|
| 244 | printf("sine: ERROR - couldn't open [%s]\n", fname3);
|
---|
| 245 | fclose(fp1);
|
---|
| 246 | fclose(fp2);
|
---|
| 247 | exit(1);
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | printf(" size = %ld, step = %f, offset = %f, scaling = %f\n",
|
---|
| 251 | (long)TABLEN, q, ip, os);
|
---|
| 252 |
|
---|
| 253 | fprintf(fp3, "Buchla 700 Sine PROM Generator %s\n", VER);
|
---|
| 254 | fprintf(fp3, " size = %ld, step = %f, offset = %f, scaling = %f\n\n",
|
---|
| 255 | (long)TABLEN, q, ip, os);
|
---|
| 256 | fprintf(fp3, "Addr Rev Data Value\n");
|
---|
| 257 |
|
---|
| 258 | printf(" output on %s and %s, log on %s\n\n", fname1, fname2, fname3);
|
---|
| 259 |
|
---|
| 260 | /* |
---|
| 261 |
|
---|
| 262 | */
|
---|
| 263 | printf("Beginning calculation phase.\n\n");
|
---|
| 264 |
|
---|
| 265 | printf("Addr Rev Data Value\n");
|
---|
| 266 |
|
---|
| 267 | for(i = 0; i < TABLEN; i++) {
|
---|
| 268 |
|
---|
| 269 | k = sin(((double)i * q) + ip);
|
---|
| 270 |
|
---|
| 271 | if( k >= 1.0 )
|
---|
| 272 | k = 0.9999999999;
|
---|
| 273 |
|
---|
| 274 | t[bitrev(i, BRLEN)] = k * os;
|
---|
| 275 |
|
---|
| 276 | if (! (i & 0x03FF)) {
|
---|
| 277 |
|
---|
| 278 | printf("%04x %04x %04x %f\n",
|
---|
| 279 | i, bitrev(i, BRLEN), t[i], k);
|
---|
| 280 |
|
---|
| 281 | fprintf(fp3, "%04x %04x %04x %f\n",
|
---|
| 282 | i, bitrev(i, BRLEN), t[i], k);
|
---|
| 283 | }
|
---|
| 284 | }
|
---|
| 285 | /* |
---|
| 286 |
|
---|
| 287 | */
|
---|
| 288 | printf("\nSine table created, starting output phase.\n");
|
---|
| 289 |
|
---|
| 290 | i = 0L; /* initialize sine table index */
|
---|
| 291 | nr = 0L; /* initialize output address */
|
---|
| 292 |
|
---|
| 293 | while (i < (long)TABLEN) {
|
---|
| 294 |
|
---|
| 295 | for (j = 0; j < (long)BUFLEN; j++) { /* split the words */
|
---|
| 296 |
|
---|
| 297 | lo_buf[j] = t[i] & 0x00FF;
|
---|
| 298 | hi_buf[j] = (t[i] >> 8) & 0x00FF;
|
---|
| 299 | i++;
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | msrec(fp1, nr, (long)BUFLEN, lo_buf); /* write the S-Records */
|
---|
| 303 | msrec(fp2, nr, (long)BUFLEN, hi_buf);
|
---|
| 304 |
|
---|
| 305 | printf("S-Record %5ld complete\n", nr);
|
---|
| 306 |
|
---|
| 307 | nr += (long)BUFLEN;
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | msdone(fp1); /* write final S-Records */
|
---|
| 311 | msdone(fp2);
|
---|
| 312 |
|
---|
| 313 | fflush(fp1); /* flush the files */
|
---|
| 314 | fflush(fp2);
|
---|
| 315 | fflush(fp3);
|
---|
| 316 |
|
---|
| 317 | fclose(fp1); /* close the files */
|
---|
| 318 | fclose(fp2);
|
---|
| 319 | fclose(fp3);
|
---|
| 320 |
|
---|
| 321 | exit(0); /* exit back to the operating system */
|
---|
| 322 | }
|
---|