-
Notifications
You must be signed in to change notification settings - Fork 48
/
printer.cpp
529 lines (424 loc) · 12.3 KB
/
printer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
// -----------------------------------------------------------------------------
// Altair 8800 Simulator
// Copyright (C) 2017 David Hansel
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// -----------------------------------------------------------------------------
#include "printer.h"
#include "config.h"
#include "host.h"
#include "timer.h"
#include "cpucore.h"
#include "Altair8800.h"
#include "io.h"
#if USE_PRINTER==0
void printer_setup() {}
void printer_register_ports() {}
#else
#define PST_BUFFER_FULL 0x01
#define PST_PRINTING 0x02
#define PST_LINEFEED 0x08
#define STLF_CR 3
#define STLF_CRLF 2
#define STLF_LF 1
#define STLF_DONE 0
#define BUFFER_SIZE 132
static byte status = 0x00;
static bool interrupt_enabled = false;
static byte buffer_size = 0, buffer_counter = 0, linefeed_status = 0;
static byte buffer[BUFFER_SIZE];
static bool print_character(byte c, unsigned long delay)
{
bool res = false;
byte ser = config_printer_map_to_host_serial();
if( host_serial_available_for_write(ser) )
{
// printer is assigned to serial device and device is ready
host_serial_write(ser, c);
timer_start(TIMER_PRINTER, delay);
res = true;
}
else
{
// serial output not assigned or not ready => check again in 10ms
timer_start(TIMER_PRINTER, 1000000/100);
}
return res;
}
static void print_next_character()
{
status &= ~(PST_PRINTING|PST_LINEFEED);
bool rt = config_printer_realtime() || interrupt_enabled;
if( buffer_counter<buffer_size )
{
status |= PST_PRINTING;
// 1000000us/100 delay => 100 characters per second
if( print_character(buffer[buffer_counter], rt ? 1000000/100 : 1) )
{
buffer_counter++;
if( buffer_counter == buffer_size )
{
// done printing the buffer
buffer_size=0;
status &= ~PST_BUFFER_FULL;
}
}
}
else if( linefeed_status==STLF_CRLF )
{
status |= PST_LINEFEED;
// 1000000us/2 delay => 1/10 second for carriage return
if( print_character(0x0d, rt ? 1000000/10 : 1) )
linefeed_status=STLF_LF;
}
else if( linefeed_status==STLF_CR )
{
status |= PST_LINEFEED;
// 1000000us/2 delay => 1/10 second for carriage return
if( print_character(0x0d, rt ? 1000000/10 : 1) )
linefeed_status=STLF_DONE;
}
else if( linefeed_status==STLF_LF )
{
status |= PST_LINEFEED;
// 1000000us/10 delay => 1/10 second for line feed
if( print_character(0x0a, rt ? 1000000/10 : 1) )
linefeed_status=STLF_DONE;
}
}
static void printer_interrupt()
{
if( (status & (PST_PRINTING|PST_LINEFEED)) )
print_next_character();
if( !(status & (PST_PRINTING|PST_LINEFEED)) && (status & PST_BUFFER_FULL) )
{
buffer_counter = 0;
linefeed_status = STLF_CRLF;
print_next_character();
}
if( interrupt_enabled && !(status & (PST_PRINTING|PST_LINEFEED)) )
altair_interrupt(INT_LPC);
}
// ----- Okidata printer
// NOTE: I found very little documentation on the Okidata printer.
// All I have is some information taken from the printer emulation of
// the MITS Altair Emulator.
// The rest I had to guess based on issuing actual LPRINT/LLIST
// commands in BASIC and CP/M and seeing what happens.
// What I have here works fine for the cases I tried but is
// certainly nowhere close to being accurate.
void printer_oki_out_ctrl(byte data)
{
if( interrupt_enabled )
altair_interrupt(INT_LPC, false);
switch( data )
{
case 0x01:
{
// command 0x01: print whatever is in the buffer (if buffer is not empty)
if( (status & (PST_PRINTING|PST_LINEFEED))==0 && buffer_size>0 )
{
buffer_counter = 0;
linefeed_status = STLF_CRLF;
print_next_character();
}
break;
}
case 0x02:
{
// command 0x02: do a line feed
if( (status & (PST_PRINTING|PST_LINEFEED))==0 )
{
linefeed_status = STLF_LF;
print_next_character();
}
break;
}
case 0x04:
{
// command 0x04: clear buffer
buffer_size=0;
break;
}
}
}
void printer_oki_out_data(byte data)
{
if( interrupt_enabled )
altair_interrupt(INT_LPC, false);
// Okidata printer only uses lower 6 bits, with
// 0x00-0x1f: upper case letters
// 0x20-0x3f: punctuation and numbers
data &= 0x3F;
if( data<0x20 ) data += 0x40;
if( (status & PST_PRINTING)==0 && buffer_size<80 )
{
buffer[buffer_size++] = data;
if( buffer_size==80 )
{
status |= PST_BUFFER_FULL;
if( (status & PST_LINEFEED)==0 )
{
buffer_counter = 0;
linefeed_status = STLF_CRLF;
print_next_character();
}
}
}
}
byte printer_oki_in_ctrl()
{
// inverted logic for status register
return ~status;
}
void printer_oki_interrupt()
{
printer_interrupt();
}
void printer_oki_setup()
{
timer_setup(TIMER_PRINTER, 1000000, printer_oki_interrupt);
interrupt_enabled = false;
status = 0x00;
}
// ----- C700 printer
// Documentation for the C700 printer is at:
// http://altairclone.com/downloads/manuals/88-C700%20(Centronics).pdf
static byte printer_c700_selected = false;
void printer_c700_out_ctrl(byte data)
{
if( interrupt_enabled )
altair_interrupt(INT_LPC, false);
// bit 0: 0=PRIME (reset print buffer)
if( (data & 0x01)==0 )
{
buffer_counter = 0;
buffer_size = 0;
}
// bit 1: 1=enable interrupts
interrupt_enabled = (data & 0x02)!=0;
if( interrupt_enabled )
{
if( !timer_running(TIMER_PRINTER) )
timer_start(TIMER_PRINTER, 10);
}
else
{
if( timer_running(TIMER_PRINTER) )
timer_stop(TIMER_PRINTER);
}
}
void printer_c700_out_data(byte data)
{
if( data == 0x11 )
{
// select printer
printer_c700_selected = true;
}
else if( printer_c700_selected )
{
if( data==0x0a )
{
// line feed
linefeed_status = STLF_LF;
print_next_character();
}
else if( data==0x0d )
{
// carriage return
buffer_counter = 0;
linefeed_status = STLF_CRLF;
print_next_character();
}
else if( data==0x0e )
{
// SO code (wide character printing)
// currently not implemented
}
else if( data == 0x13 )
{
// de-select printer
printer_c700_selected = false;
}
else if( data==0x7f )
{
// DEL (clears buffer)
buffer_counter = 0;
buffer_size = 0;
}
else if( (status & PST_PRINTING)==0 && buffer_size<BUFFER_SIZE )
{
buffer[buffer_size++] = data;
if( buffer_size==BUFFER_SIZE )
{
status |= PST_BUFFER_FULL;
if( (status & PST_LINEFEED)==0 )
{
buffer_counter = 0;
linefeed_status = STLF_CRLF;
print_next_character();
}
}
}
}
}
byte printer_c700_in_ctrl()
{
byte res = 0x00;
// bit 0: 0=not ready, 1=ready
// bit 1: 0=not busy, 1=busy
if( status & (PST_PRINTING|PST_LINEFEED) )
res |= 0x02;
else
res |= 0x01;
// bit 2: 1=out of paper (we always have paper)
// bit 3: 0=printer selected, 1=printer not selected
if( !printer_c700_selected ) res |= 0x08;
// bit 4: 1=fault (we never have a fault)
// bit 6: interrupts enabled
if( interrupt_enabled ) res |= 0x40;
// bit 7: 1=interrupt has occurred
if( altair_interrupt_active(INT_LPC) ) res |= 0x80;
return res;
}
void printer_c700_interrupt()
{
printer_interrupt();
}
void printer_c700_setup()
{
timer_setup(TIMER_PRINTER, 1000000, printer_c700_interrupt);
status = 0x00;
printer_c700_selected = false;
}
// ----- Generic
// for the generic emulation we use the buffer as a ring-buffer
// not defining new buffer pointer variables to save host memory
#define buffer_start buffer_counter
#define buffer_end buffer_size
void printer_generic_out_ctrl(byte data)
{}
void printer_generic_out_data(byte data)
{
if( config_printer_realtime() )
{
if( !(status & PST_BUFFER_FULL) )
{
buffer[buffer_end] = data;
buffer_end = (buffer_end+1) % BUFFER_SIZE;
if( ((buffer_end+1) % BUFFER_SIZE) == buffer_start )
status |= PST_BUFFER_FULL;
}
// process first character as soon as possible
if( !timer_running(TIMER_PRINTER) )
timer_start(TIMER_PRINTER, 1);
}
else
host_serial_write(config_printer_map_to_host_serial(), data);
}
byte printer_generic_in_ctrl()
{
bool busy;
if( config_printer_realtime() )
busy = (status & PST_BUFFER_FULL);
else
busy = host_serial_available_for_write(config_printer_map_to_host_serial())==0;
return config_printer_generic_get_status(busy);
}
void printer_generic_interrupt()
{
byte ser = config_printer_map_to_host_serial();
if( !host_serial_available_for_write(ser) )
{
// serial output not assigned or not ready => check again in 10ms
timer_start(TIMER_PRINTER, 1000000/100);
}
else
{
byte data = buffer[buffer_start];
buffer_start = (buffer_start+1) % BUFFER_SIZE;
status &= ~PST_BUFFER_FULL;
host_serial_write(ser, data);
if( buffer_start!=buffer_end )
{
// buffer is not empty yet => schedule another inerrupt
int delay = (data==0x0d || data==0x0a) ? (1000000/10) : (1000000/100);
timer_start(TIMER_PRINTER, delay);
}
}
}
void printer_generic_setup()
{
timer_setup(TIMER_PRINTER, 1000000, printer_generic_interrupt);
status = 0x00;
buffer_end = buffer_start = 0;
}
// ---------------------------------------------------------------------------------------------
void printer_out_ctrl(byte port, byte data)
{
//printf("%04X: OUT 02, %02X\n", regPC, data);
switch( config_printer_type() )
{
case CP_OKI: printer_oki_out_ctrl(data); break;
case CP_C700: printer_c700_out_ctrl(data); break;
case CP_GENERIC: printer_generic_out_ctrl(data); break;
}
}
void printer_out_data(byte port, byte data)
{
//printf("%04X: OUT 03, %02X\n", regPC, data);
switch( config_printer_type() )
{
case CP_OKI: printer_oki_out_data(data); break;
case CP_C700: printer_c700_out_data(data); break;
case CP_GENERIC: printer_generic_out_data(data); break;
}
}
byte printer_in_ctrl(byte port)
{
byte data = 0xff;
switch( config_printer_type() )
{
case CP_OKI: data = printer_oki_in_ctrl(); break;
case CP_C700: data = printer_c700_in_ctrl(); break;
case CP_GENERIC: data = printer_generic_in_ctrl(); break;
}
//printf("%04X: IN 02 <= %02X\n", regPC, data);
return data;
}
void printer_register_ports()
{
bool mapped = (config_printer_type() != CP_NONE) && (config_printer_map_to_host_serial() != 0xff);
io_register_port_inp(2, mapped ? printer_in_ctrl : NULL);
io_register_port_out(2, mapped ? printer_out_ctrl : NULL);
io_register_port_out(3, mapped ? printer_out_data : NULL);
}
void printer_setup()
{
if( interrupt_enabled )
altair_interrupt(INT_LPC, false);
interrupt_enabled = false;
buffer_size = 0;
buffer_counter = 0;
linefeed_status = 0;
printer_register_ports();
switch( config_printer_type() )
{
case CP_OKI: return printer_oki_setup(); break;
case CP_C700: return printer_c700_setup(); break;
case CP_GENERIC: return printer_generic_setup(); break;
}
}
#endif