-
Notifications
You must be signed in to change notification settings - Fork 48
/
serial.cpp
1213 lines (996 loc) · 33.9 KB
/
serial.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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -----------------------------------------------------------------------------
// 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 "Altair8800.h"
#include "config.h"
#include "host.h"
#include "serial.h"
#include "filesys.h"
#include "prog_examples.h"
#include "cpucore.h"
#include "prog_ps2.h"
#include "timer.h"
#include "numsys.h"
#include "io.h"
#define SST_RDRF 0x01 // receive register full (character received)
#define SST_TDRE 0x02 // send register empty (ready for next byte)
#define SST_FNF 0x04 // used in simulator to signal file-not-found in CLOAD
#define SST_OVRN 0x20 // data overrun (received character when previous not read)
#define SST_OVRN2 0x40 // used in the simulator to signal that OVRN is upcoming
#define SST_INT 0x80 // interrupt signaled
#define SSC_SIOTP0 0x01 // revision of SIO board, bit 0
#define SSC_SIOTP1 0x02 // revision of SIO board, bit 1
#define SSC_INTTX 0x20 // transmit interrupt enabled
#define SSC_REALTIME 0x40 // force real-(simulation-)time operation (always use baud rate)
#define SSC_INTRX 0x80 // receive interrupt enabled
#define b2s numsys_byte2string
byte acr_cload_fid = 0;
uint32_t acr_cload_timeout = 0;
#if USE_SECOND_2SIO>0
#define NUM_SERIAL_DEVICES 6
static const uint32_t serial_device_interrupts[6] = {INT_SIO, INT_ACR, INT_2SIO1, INT_2SIO2, INT_2SIO3, INT_2SIO4};
#else
#define NUM_SERIAL_DEVICES 4
static const uint32_t serial_device_interrupts[4] = {INT_SIO, INT_ACR, INT_2SIO1, INT_2SIO2};
#endif
volatile byte serial_ctrl[NUM_SERIAL_DEVICES], serial_data[NUM_SERIAL_DEVICES];
volatile byte serial_status[NUM_SERIAL_DEVICES], serial_status_dev[NUM_SERIAL_DEVICES];
byte serial_fid[NUM_SERIAL_DEVICES];
static byte last_active_primary_device = CSM_SIO;
static void serial_replay(byte dev);
static void acr_read_next_byte();
static void serial_timer_interrupt_check_enable(byte dev = 0xff);
void serial_sio_register_io();
void serial_acr_register_io();
void serial_2sio_register_io(byte dev);
static const __FlashStringHelper *devname(byte dev)
{
switch( dev )
{
case CSM_SIO: return F("88-SIO");
case CSM_ACR: return F("ACR");
case CSM_2SIO1: return F("88-2SIO-1");
case CSM_2SIO2: return F("88-2SIO-2");
#if USE_SECOND_2SIO>0
case CSM_2SIO3: return F("2nd 88-2SIO-1");
case CSM_2SIO4: return F("2nd 88-2SIO-2");
#endif
}
return F("[invalid device]");
}
byte serial_last_active_primary_device()
{
return last_active_primary_device;
}
void set_serial_status(byte dev, byte status)
{
// check whether the interrupt line should be enabled
// either receive interrups are enabled and receive register is full
// or transmit interrupts are enabled and transmit register is empty
bool intr = ((serial_ctrl[dev] & SSC_INTRX) && (status & SST_RDRF) ||
(serial_ctrl[dev] & SSC_INTTX) && (status & SST_TDRE));
// set the INT status bit accordingly
if( intr ) status |= SST_INT;
else status &= ~SST_INT;
// set the status
serial_status[dev] = status;
// enable or disable the ALTAIR interrupt status
altair_interrupt(serial_device_interrupts[dev], intr);
// set the device status register
if( dev>=CSM_2SIO1 && dev<=CSM_2SIO4 )
serial_status_dev[dev] = status & ~SST_OVRN2;
else if( (serial_ctrl[dev]&(SSC_SIOTP0|SSC_SIOTP1))==1 || dev==CSM_ACR )
{
// 88-SIO rev 1
byte data = 0x00;
if( status & SST_OVRN ) data |= 0x10;
if( !(status & SST_RDRF) ) data |= 0x01;
if( !(status & SST_TDRE) ) data |= 0x80;
serial_status_dev[dev] = data;
}
else if( (serial_ctrl[dev]&(SSC_SIOTP0|SSC_SIOTP1))==0 )
{
// 88-SIO rev 0
byte data = 0x00;
if( (status & SST_RDRF) ) data |= 0x20;
if( (status & SST_TDRE) ) data |= 0x02;
serial_status_dev[dev] = data;
}
else
{
// console attached to Cromeco FDC
byte data = 0x00;
if( status & SST_OVRN ) data |= 0x02;
if( (status & SST_RDRF) ) data |= 0x40;
if( (status & SST_TDRE) ) data |= 0x80;
serial_status_dev[dev] = data;
}
// check if timer interrupts need to be enabled or disabled
serial_timer_interrupt_check_enable(dev);
}
void serial_update_hlda_led()
{
bool on = false;
for(byte dev=0; !on && dev<NUM_SERIAL_DEVICES; dev++)
if( serial_fid[dev] ) on = true;
if( on )
host_set_status_led_HLDA();
else
host_clr_status_led_HLDA();
}
void serial_replay_start(byte dev, bool example, byte filenum)
{
serial_acr_check_cload_timeout();
if( example )
{
// load example
if( prog_examples_read_start(filenum) )
{
serial_fid[dev] = 0xff;
DBG_FILEOPS4(3, F("loading example #"), b2s(filenum), F(" via "), devname(dev));
}
else
DBG_FILEOPS3(2, F("example #"), b2s(filenum), F(" does not exist"));
}
else
{
// load from file
serial_fid[dev] = filesys_open_read('D', filenum);
if( serial_fid[dev]>0 )
DBG_FILEOPS4(3, F("loading data file #"), b2s(filenum), F(" via "), devname(dev));
else
DBG_FILEOPS3(2, F("data file #"), b2s(filenum), F(" not found"));
}
// either start interrupt timer or prepare first byte for replay
if( serial_ctrl[dev] & (SSC_INTRX|SSC_REALTIME) )
serial_timer_interrupt_check_enable(dev);
else
serial_replay(dev);
serial_update_hlda_led();
}
void serial_capture_start(byte dev, byte filenum)
{
serial_acr_check_cload_timeout();
serial_fid[dev] = filesys_open_write('D', filenum);
if( serial_fid[dev] )
DBG_FILEOPS4(3, F("capturing from "), devname(dev), F(" to data file #"), b2s(filenum));
else
DBG_FILEOPS(1, F("unable to start capturing (storage full?)"));
serial_update_hlda_led();
}
bool serial_replay_running(byte dev)
{
return serial_fid[dev]>=0xfe || serial_fid[dev]>0 && filesys_is_read(serial_fid[dev]);
}
bool serial_capture_running(byte dev)
{
return serial_fid[dev]<0xfe && serial_fid[dev]>0 && filesys_is_write(serial_fid[dev]);
}
void serial_stop(byte dev)
{
if( serial_fid[dev]==0xfe )
{
DBG_FILEOPS(3, F("ejecting PS2 tape"));
serial_fid[dev] = 0;
}
else if( serial_fid[dev]==0xff )
{
DBG_FILEOPS(3, F("stopping example load"));
serial_fid[dev] = 0;
}
else if( serial_fid[dev]>0 )
{
if( filesys_is_read(serial_fid[dev]) )
DBG_FILEOPS(3, F("stopping data replay"));
else if( filesys_is_write(serial_fid[dev]) )
DBG_FILEOPS(3, F("ending capture"));
filesys_close(serial_fid[dev]);
serial_fid[dev] = 0;
}
serial_update_hlda_led();
}
void serial_close_files()
{
for(byte dev=0; dev<NUM_SERIAL_DEVICES; dev++)
if( serial_fid[dev]>0 ) serial_stop(dev);
if( acr_cload_fid>0 ) filesys_close(acr_cload_fid);
acr_cload_fid = 0;
serial_timer_interrupt_check_enable();
serial_update_hlda_led();
}
// -----------------------------------------------------------------------------------------------------------------------
void serial_register_ports()
{
serial_sio_register_io();
serial_acr_register_io();
for(byte dev=CSM_2SIO1; dev<NUM_SERIAL_DEVICES && dev<=CSM_2SIO4; dev++)
serial_2sio_register_io(dev);
}
void serial_set_config(byte dev)
{
if( dev==0xff )
{
serial_register_ports();
for(dev=0; dev<NUM_SERIAL_DEVICES; dev++) serial_set_config(dev);
}
else
{
if( config_serial_realtime(dev) )
serial_ctrl[dev] |= SSC_REALTIME;
else
serial_ctrl[dev] &= ~SSC_REALTIME;
if( dev==CSM_SIO ) serial_ctrl[dev] = (serial_ctrl[dev] & ~3) | (config_serial_siorev() & 3);
switch( dev )
{
case CSM_SIO : serial_sio_register_io(); break;
case CSM_ACR : serial_acr_register_io(); break;
case CSM_2SIO1 :
case CSM_2SIO2 :
case CSM_2SIO3 :
case CSM_2SIO4 : serial_2sio_register_io(dev); break;
}
}
}
void serial_reset(byte dev)
{
if( dev==0xff )
{
for(dev=0; dev<NUM_SERIAL_DEVICES; dev++) serial_reset(dev);
}
else
{
serial_ctrl[dev] = 0;
serial_set_config(dev);
set_serial_status(dev, SST_TDRE);
}
}
// called when serial data for a device is received
void serial_receive_data(byte dev, byte b)
{
byte status = serial_status[dev];
// store received data
if( status & SST_RDRF )
{
// overrun occurred
// according to 2SIO1 (and 6850 UART) documentation, the overrun is not
// signaled until the previous (properly received) byte has been read.
// so we set a flag here to signal that as soon as the current byte is
// read we have to signal the overrun condition.
status |= SST_OVRN2;
}
else
{
serial_data[dev] = b;
status |= SST_RDRF;
}
// set status register (also triggers interrupt if necessary)
set_serial_status(dev, status);
}
// called by the host if serial data received
void serial_receive_host_data(byte host_interface, byte b)
{
static unsigned long prevESC = 0;
if( b==27 && config_serial_input_enabled() && !host_read_status_led_WAIT() && host_interface==config_host_serial_primary() )
{
if( millis()-prevESC>50 && millis()-prevESC<250 )
{
// if we have serial input enabled then hitting
// the ESC key twice works as STOP
altair_interrupt(INT_SW_STOP);
}
else
{
for(byte dev=0; dev<NUM_SERIAL_DEVICES; dev++)
if( config_serial_map_sim_to_host(dev)==host_interface )
serial_receive_data(dev, b);
}
prevESC = millis();
}
else
{
prevESC = 0;
for(byte dev=0; dev<NUM_SERIAL_DEVICES; dev++)
if( config_serial_map_sim_to_host(dev)==host_interface )
serial_receive_data(dev, b);
}
}
static void serial_replay(byte dev)
{
byte fid = serial_fid[dev];
if( fid>0 && (fid==0xff || filesys_is_read(fid)) )
{
byte data;
if( fid==0xff )
{
// 0xff is special fid for example replay
if( prog_examples_read_next(dev, &data) )
serial_receive_data(dev, data);
else
{
DBG_FILEOPS(4, F("done loading"));
serial_fid[dev] = 0;
serial_update_hlda_led();
}
}
else
{
// play back captured data
if( filesys_read_char(fid, &data) )
{
DBG_FILEOPS2(5, F("replay data: "), int(data));
serial_receive_data(dev, data);
}
else
{
DBG_FILEOPS(4, F("no more data for replay"));
filesys_close(fid);
serial_fid[dev] = 0;
serial_update_hlda_led();
}
}
}
}
bool serial_available()
{
// this is the simulator reading serial inputs
host_check_interrupts();
// read devices from high to low. When written in serial_host_receive_data
// we apply changes from low to high. Going in the different direction
// ensures we are not seeing inputs twice if two devices are mapped to
// the primary host interface.
for(byte dev=3; dev<0xff; dev--)
if( config_serial_map_sim_to_host(dev)==config_host_serial_primary() )
return (serial_status[dev] & SST_RDRF)!=0;
return false;
}
int serial_read()
{
// this is the simulator reading serial inputs
int res = -1;
host_check_interrupts();
// read devices from high to low. When written in serial_host_receive_data
// we apply changes from low to high. Going in the different direction
// ensures we are not seeing inputs twice if two devices are mapped to
// the primary host interface.
for(byte dev=3; dev<0xff; dev--)
if( config_serial_map_sim_to_host(dev)==config_host_serial_primary() )
{
if( (serial_status[dev] & SST_RDRF) )
{
res = serial_data[dev];
for(dev=dev; dev<0xff; dev--)
if( config_serial_map_sim_to_host(dev)==config_host_serial_primary() )
set_serial_status(dev, 0);
}
return res;
}
return res;
}
static byte serial_read(byte dev)
{
// this is the simulated code reading serial data
host_check_interrupts();
if( config_serial_map_sim_to_host(dev)==config_host_serial_primary() )
last_active_primary_device = dev;
byte status = serial_status[dev] & ~(SST_RDRF | SST_OVRN);
if( status & SST_OVRN2 )
{
// there was an overrun condition and the last properly received byte
// has just been read => signal OVRN now
status |= SST_OVRN;
status &= ~SST_OVRN2;
//DBG_FILEOPS(1, F("OVRN"));
}
set_serial_status(dev, status);
// note that we're reading whatever is there, even
// if nothing new has arrived
return serial_data[dev];
}
void serial_write(byte dev, byte data)
{
byte host_interface = config_serial_map_sim_to_host(dev);
if( host_interface!=0xff )
{
host_serial_write(host_interface, data);
// if backspace is translated then force destructive backspace
// by sending BACKSPACE-SPACE-BACKSPACE instead of just BACKSPACE
if( data==8 && config_serial_backspace(dev, regPC)!=CSFB_NONE )
{ host_serial_write(host_interface, 32); host_serial_write(host_interface, 8); }
if( host_interface==config_host_serial_primary() )
last_active_primary_device = dev;
}
if( !host_read_status_led_WAIT() && serial_fid[dev]>0 && filesys_is_write(serial_fid[dev]) )
{
if( !filesys_write_char(serial_fid[dev], data) )
{
DBG_FILEOPS(1, F("capture storage exhausted"));
//filesys_close(capture_fid);
//capture_fid = 0;
//serial_update_hlda_led();
}
else
DBG_FILEOPS2(5, F("writing captured data: "), b2s(data));
}
}
void serial_timer_interrupt_check_enable(byte dev)
{
if( dev<0xff )
{
bool enable = ((serial_ctrl[dev] & (SSC_INTRX|SSC_REALTIME)) && serial_fid[dev]>0 && (serial_fid[dev]==0xff || filesys_is_read(serial_fid[dev]))
||
(serial_ctrl[dev] & (SSC_INTTX|SSC_REALTIME)) && !(serial_status[dev] & SST_TDRE));
bool enabled = timer_running(dev);
if( !enabled && enable )
timer_start(dev);
else if( enabled && !enable )
timer_stop(dev);
}
else
for(dev=0; dev<NUM_SERIAL_DEVICES; dev++)
serial_timer_interrupt_check_enable(dev);
}
static void serial_timer_interrupt(byte dev)
{
if( host_serial_available_for_write(config_serial_map_sim_to_host(dev))>0 )
{
// write out data scheduled to write (if necessary)
if( (serial_ctrl[dev] & (SSC_INTTX|SSC_REALTIME))!=0 )
{
// update status register (send buffer is empty now)
serial_status[dev] |= SST_TDRE;
}
// read serial playback data
serial_replay(dev);
// this schedules additional interrupts as necessary
set_serial_status(dev, serial_status[dev]);
}
else
serial_timer_interrupt_check_enable(dev);
}
static void serial_timer_interrupt_SIO() { serial_timer_interrupt(CSM_SIO); }
static void serial_timer_interrupt_ACR() { serial_timer_interrupt(CSM_ACR); }
static void serial_timer_interrupt_2SIO1() { serial_timer_interrupt(CSM_2SIO1); }
static void serial_timer_interrupt_2SIO2() { serial_timer_interrupt(CSM_2SIO2); }
#if USE_SECOND_2SIO>0
static void serial_timer_interrupt_2SIO3() { serial_timer_interrupt(CSM_2SIO3); }
static void serial_timer_interrupt_2SIO4() { serial_timer_interrupt(CSM_2SIO4); }
#endif
void serial_timer_interrupt_setup(byte dev)
{
if( dev<0xff )
{
// calculate the number of microseconds it would take to receive
// one byte given a specific baud rate (assuming 8 bit plus 1 start bit and 1 stop bit):
// (10 * 1000000) / baud_rate
timer_stop(dev);
uint32_t us_per_byte = 10000000lu / config_serial_playback_baud_rate(dev);
switch( dev )
{
case CSM_SIO: timer_setup(dev, us_per_byte, serial_timer_interrupt_SIO); break;
case CSM_ACR: timer_setup(dev, us_per_byte, serial_timer_interrupt_ACR); break;
case CSM_2SIO1: timer_setup(dev, us_per_byte, serial_timer_interrupt_2SIO1); break;
case CSM_2SIO2: timer_setup(dev, us_per_byte, serial_timer_interrupt_2SIO2); break;
#if USE_SECOND_2SIO>0
case CSM_2SIO3: timer_setup(dev, us_per_byte, serial_timer_interrupt_2SIO3); break;
case CSM_2SIO4: timer_setup(dev, us_per_byte, serial_timer_interrupt_2SIO4); break;
#endif
}
if( config_serial_realtime(dev) )
serial_ctrl[dev] |= SSC_REALTIME;
else
serial_ctrl[dev] &= ~SSC_REALTIME;
serial_timer_interrupt_check_enable(dev);
}
else
for(dev=0; dev<NUM_SERIAL_DEVICES; dev++)
serial_timer_interrupt_setup(dev);
}
static byte serial_map_characters_in(byte dev, byte data)
{
if( config_serial_ucase(dev, regPC) )
{
// only use upper-case letters
if( data>96 && data<123 ) data -= 32;
}
if( data==8 )
{
switch( config_serial_backspace(dev, regPC) )
{
case CSFB_UNDERSCORE:
if( data==8 ) data = '_';
break;
case CSFB_DELETE:
if( data==8 ) data = 127;
break;
}
}
return data;
}
static byte serial_map_characters_out(byte dev, byte data)
{
if( config_serial_7bit(dev, regPC) )
{
// only use lower 7 bits
data &= 0x7f;
}
byte b = config_serial_backspace(dev, regPC);
if( (data=='_' && b==CSFB_UNDERSCORE) || (data==127 && b==CSFB_DELETE) )
data = 8;
return data;
}
// -----------------------------------------------------------------------------------------------------------------------
// map between port number (16..23, 0x10..0x17) and CSM_2SIO1..CSM_2SIO4
#define PORT2DEV(port) ((port/2)+CSM_2SIO1-8)
#define DEV2PORT(dev) ((dev+8-CSM_2SIO1)*2)
byte serial_2sio_in_ctrl(byte port)
{
// read control register of 88-2SIO device
byte dev = PORT2DEV(port);
byte data = serial_status_dev[dev];
byte fid = serial_fid[dev];
// if transmit interrupts are enabled then the TDRE flag is handled
// by the timer. If not, we need to determine the flag setting here
// (the RDRF flag is handled separately in serial_receive_data)
if( !(serial_ctrl[dev] & (SSC_INTTX|SSC_REALTIME)) )
{
if( fid>0 && fid<0xff && filesys_is_write(fid) )
{
if( !filesys_eof(fid) )
data |= SST_TDRE;
else
data &= ~SST_TDRE;
}
else if( host_serial_available_for_write(config_serial_map_sim_to_host(dev)) )
data |= SST_TDRE;
else
data &= ~SST_TDRE;
}
return data;
}
byte serial_2sio_in_data(byte port)
{
byte data = 0;
byte dev = PORT2DEV(port);
// get character
data = serial_read(dev);
// map character (for BASIC etc)
data = serial_map_characters_in(dev, data);
// if interrupts are not enabled, prepare the next byte for replay now
if( !(serial_ctrl[dev] & (SSC_INTRX|SSC_REALTIME)) )
serial_replay(dev);
return data;
}
void serial_2sio_out_ctrl(byte port, byte data)
{
byte dev = PORT2DEV(port);
// write to control register of 88-2SIO1 device
if( !(serial_ctrl[dev] & SSC_INTRX) && (data & 0x80) )
{
DBG_FILEOPS2(4, F("ENABLING receive interrupts on 2SIO-"), dev==CSM_2SIO1 ? '1' : '2');
serial_ctrl[dev] |= SSC_INTRX;
}
else if( (serial_ctrl[dev] & SSC_INTRX) && !(data & 0x80) )
{
DBG_FILEOPS2(4, F("disabling receive interrupts on 2SIO-"), dev==CSM_2SIO1 ? '1' : '2');
serial_ctrl[dev] &= ~SSC_INTRX;
}
if( !(serial_ctrl[dev] & SSC_INTTX) && (data & 0x60)==0x20 )
{
DBG_FILEOPS2(4, F("ENABLING transmit interrupts on 2SIO-"), dev==CSM_2SIO1 ? '1' : '2');
serial_ctrl[dev] |= SSC_INTTX;
}
else if( (serial_ctrl[dev] & SSC_INTTX) && (data & 0x60)!=0x20 )
{
DBG_FILEOPS2(4, F("disabling transmit interrupts on 2SIO-"), dev==CSM_2SIO1 ? '1' : '2');
serial_ctrl[dev] &= ~SSC_INTTX;
}
if( (data & 0x03)==0x03 )
{
// master reset
serial_reset(dev);
}
// check serial status (interrupts)
set_serial_status(dev, serial_status[dev]);
}
void serial_2sio_out_data(byte port, byte data)
{
byte dev = PORT2DEV(port);
// map character (for BASIC etc)
data = serial_map_characters_out(dev, data);
// output character
serial_write(dev, data);
if( serial_ctrl[dev] & (SSC_INTTX|SSC_REALTIME) )
{
// transmit interrupts are enabled
// update status register (send buffer is NOT empty now)
// this also schedules a timer to set the TDRE flag again
set_serial_status(dev, serial_status[dev] & ~SST_TDRE);
}
}
void serial_2sio_register_io(byte dev)
{
byte port = DEV2PORT(dev);
bool mapped = config_serial_map_sim_to_host(dev)!=0xFF;
io_register_port_inp(port, mapped ? serial_2sio_in_ctrl : NULL);
io_register_port_inp(port+1, mapped ? serial_2sio_in_data : NULL);
io_register_port_out(port, mapped ? serial_2sio_out_ctrl : NULL);
io_register_port_out(port+1, mapped ? serial_2sio_out_data : NULL);
}
// ------------------------------------------------------------------------------------------------------------
byte serial_sio_in_ctrl(byte port)
{
byte data = serial_status_dev[CSM_SIO];
// if transmit interrupts are enabled then the TDRE flag is handled
// by the timer. If not, we need to determine the flag setting here
// (the RDRF flag is handled separately in serial_receive_data)
if( !(serial_ctrl[CSM_SIO] & (SSC_INTTX|SSC_REALTIME)) )
{
byte fid = serial_fid[CSM_SIO];
bool can_send = true;
if( fid>0 && fid<0xff && filesys_is_write(fid) )
{ if( filesys_eof(fid) ) can_send = false; }
else
{ if( !host_serial_available_for_write(config_serial_map_sim_to_host(CSM_SIO)) ) can_send = false; }
switch( serial_ctrl[CSM_SIO] & (SSC_SIOTP0|SSC_SIOTP1) )
{
case 0: // 88SIO rev0
data = data & ~0x02;
if( can_send ) data |= 0x02;
break;
case 1: // 88SIO rev1
data = data & ~0x80;
if( !can_send ) data |= 0x80;
break;
case 2: // Cromemco
data = data & ~0x80;
if( can_send ) data |= 0x80;
break;
}
}
return data;
}
byte serial_sio_in_data(byte port)
{
byte data = 0;
// get character
data = serial_read(CSM_SIO);
// map character (for BASIC etc)
data = serial_map_characters_in(CSM_SIO, data);
// if interrupts are not enabled, prepare the next byte for replay now
if( !(serial_ctrl[CSM_SIO] & (SSC_INTRX|SSC_REALTIME)) )
serial_replay(CSM_SIO);
return data;
}
void serial_sio_out_ctrl(byte port, byte data)
{
// port 0 of Cromemco serial card is baud rate (ignore)
if( (serial_ctrl[CSM_SIO] & (SSC_SIOTP0|SSC_SIOTP1))==2 )
return;
// write to control register of 88-SIO
if( !(serial_ctrl[CSM_SIO] & SSC_INTRX) && (data & 0x01) )
{
DBG_FILEOPS(4, F("ENABLING receive interrupts on SIO"));
serial_ctrl[CSM_SIO] |= SSC_INTRX;
}
else if( (serial_ctrl[CSM_SIO] & SSC_INTRX) && !(data & 0x01) )
{
DBG_FILEOPS(4, F("disabling receive interrupts on SIO"));
serial_ctrl[CSM_SIO] &= ~SSC_INTRX;
}
if( !(serial_ctrl[CSM_SIO] & SSC_INTTX) && (data & 0x02) )
{
DBG_FILEOPS(4, F("ENABLING transmit interrupts on SIO"));
serial_ctrl[CSM_SIO] |= SSC_INTTX;
}
else if( (serial_ctrl[CSM_SIO] & SSC_INTTX) && !(data & 0x02) )
{
DBG_FILEOPS(4, F("disabling transmit interrupts on SIO"));
serial_ctrl[CSM_SIO] &= ~SSC_INTTX;
}
// check whether we need to enable timer interrupts
serial_timer_interrupt_check_enable(CSM_SIO);
// clear interrupt flag in status
set_serial_status(CSM_SIO, serial_status[CSM_SIO] & ~SST_INT);
}
void serial_sio_out_data(byte port, byte data)
{
// map character (for BASIC etc)
data = serial_map_characters_out(CSM_SIO, data);
// output character
serial_write(CSM_SIO, data);
if( serial_ctrl[CSM_SIO] & (SSC_INTTX|SSC_REALTIME) )
{
// transmit interrupts are enabled
// update status register (send buffer is NOT empty now)
// this also schedules a timer to set the TDRE flag again
set_serial_status(CSM_SIO, serial_status[CSM_SIO] & ~SST_TDRE);
}
}
void serial_sio_register_io()
{
bool mapped = config_serial_map_sim_to_host(CSM_SIO)!=0xFF;
io_register_port_inp(0, mapped ? serial_sio_in_ctrl : NULL);
io_register_port_inp(1, mapped ? serial_sio_in_data : NULL);
io_register_port_out(0, mapped ? serial_sio_out_ctrl : NULL);
io_register_port_out(1, mapped ? serial_sio_out_data : NULL);
}
// ------------------------------------------------------------------------------------------------------------
bool serial_acr_mount_ps2()
{
if( serial_fid[CSM_ACR] && serial_fid[CSM_ACR]!=0xfe )
{
DBG_FILEOPS(2, F("cannot mount PS2 tape (other operation in progress)"));
return false;
}
else
{
// (re-) mount the PS2 tape
prog_ps2_read_start();
DBG_FILEOPS(3, F("mounting PS2 tape"));
serial_fid[CSM_ACR] = 0xfe;
acr_read_next_byte();
serial_update_hlda_led();
return true;
}
}
bool serial_acr_check_cload_timeout()
{
// timeout is 0.1 (simulated) seconds, i.e. 200000 cycles (at 2MHz)
if( acr_cload_timeout>0 && (timer_get_cycles()-acr_cload_timeout)>200000 )
{
// if the last write or read from BASIC was more than 0.1 seconds ago
// then this is a new read/write operation => close the previous file
if( acr_cload_fid>0 )
{
filesys_close(acr_cload_fid);
acr_cload_fid = 0;
DBG_FILEOPS(4, F("closing tape file due to timeout"));
}
set_serial_status(CSM_ACR, 0);
acr_cload_timeout = 0;
return true;
}
return false;
}
// ALTAIR Extended BASIC loading from tape via CLOAD
static void acr_read_next_cload_byte()
{
static byte tape_fname = 0;
bool go = true;
byte data;
// check for timeout from previous operation
serial_acr_check_cload_timeout();
// if we were writing before, close the file now
if( acr_cload_fid>0 && !filesys_is_read(acr_cload_fid) )
{
filesys_close(acr_cload_fid);
acr_cload_fid = 0;
}
// no file is open: either we closed it due to timeout or
// there was a FILE NOT FOUND error earlier. In either case,
// we need to start searching from the first file again.
if( acr_cload_fid==0 )
tape_fname = 0;
while( go )
{
if( acr_cload_fid>0 )
{
if( filesys_read_char(acr_cload_fid, &data) )
{
serial_receive_data(CSM_ACR, data);
go = false;
}
else
{
filesys_close(acr_cload_fid);
acr_cload_fid = 0;
}
}
if( go )
{
while( acr_cload_fid==0 && tape_fname<96 )
{
acr_cload_fid = filesys_open_read('B', 32+tape_fname);
if( acr_cload_fid ) DBG_FILEOPS2(4, F("reading BASIC CSAVE file: "), char(32+tape_fname));
tape_fname++;
}
if( acr_cload_fid==0 )
{
serial_status[CSM_ACR] |= SST_FNF;
break;
}
}
}
acr_cload_timeout = timer_get_cycles();
}