-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathp.c
More file actions
5392 lines (4972 loc) · 168 KB
/
p.c
File metadata and controls
5392 lines (4972 loc) · 168 KB
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
/*
* Copyright (c) 1993-1996 Julian Highfield. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Julian Highfield.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define INLINE
#include "t4debug.h"
/*
* p.c - hand inlined processor.c!
*
* The transputer emulator.
*
*/
#ifdef _MSC_VER
#include "gettimeofday.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef _MSC_VER
#include <sys/time.h>
#include <unistd.h>
#endif
#include <math.h>
#ifdef T4NANOMSG
#define NN_STATIC_LIB ON
#include <nanomsg/nn.h>
#include <nanomsg/pipeline.h>
#else
#include <errno.h>
#define AF_SP 0
#define NN_PUSH 0
#define NN_PULL 0
#define NN_POLLIN 1
#define NN_POLLOUT 2
#define NN_SOL_SOCKET 0
#define NN_SNDTIMEO 0
#define NN_DONTWAIT 0
int nn_errno() { return EINVAL; }
char *nn_strerror(int errnum) { return strerror (errnum); }
int nn_socket(int domain, int protocol) { return EINVAL; }
int nn_close(int s) { return EBADF; }
int nn_bind(int s, const char *addr) { return EINVAL; }
int nn_connect(int s, const char *addr) { return EINVAL; }
int nn_shutdown(int s, int how) { return EINVAL; }
int nn_send(int s, const void *buf, size_t len, int flags) { return EINVAL; }
int nn_recv(int s, void *buf, size_t len, int flags) { return EINVAL; }
int nn_setsockopt(int s, int lvl, int opt, const void *optval, size_t optvallen) { return EINVAL; }
struct nn_pollfd {
int fd;
short events, revents;
};
int nn_poll(struct nn_pollfd *fds, int nfds, int opt) { return EINVAL; }
#endif
#include "netcfg.h"
#ifdef T4SHLINKS
#include "shlink.h"
#else
void* shlink_attach (const char *fnm, int size){return NULL;}
int shlink_detach (void *addr){return EINVAL;}
void* shlink_alloc (const char *fnm, int size){return NULL;}
int shlink_free (void){return EINVAL;}
#endif
#include "processor.h"
#include "arithmetic.h"
#include "server.h"
#include "opcodes.h"
#ifdef __MWERKS__
#include "mac_input.h"
#endif
#undef TRUE
#undef FALSE
#define TRUE 0x0001
#define FALSE 0x0000
/* Processor specific parameters. */
int Txxx = 414;
uint32_t MemStart = 0x80000048;
/* Memory space. */
u_char *core;
uint32_t CoreSize = 2 * 1024;
uint32_t ExtMemStart = 0x80000800;
u_char *mem;
uint32_t MemSize = 1 << 21;
uint32_t MemWordMask = ((uint32_t)0x001ffffc);
uint32_t MemByteMask = ((uint32_t)0x001fffff);
uint32_t CLineTagsSize;
#define InvalidInstr_p ((uint32_t)0x2ffa2ffa)
#define Undefined_p ((uint32_t)0xdeadbeef)
uint32_t word_int (uint32_t);
/* Registers. */
uint32_t IPtr;
uint32_t WPtr;
uint32_t AReg;
uint32_t BReg;
uint32_t CReg;
uint32_t OReg;
uint32_t XReg;
uint32_t WdescReg;
uint32_t DReg; /* undocumented DReg/EReg */
uint32_t EReg;
#define FP_UNKNOWN 0
#define FP_REAL32 32
#define FP_REAL64 64
typedef struct _REAL {
uint32_t length; /* FP_REAL32 or FP_REAL64 */
uint32_t rsvd;
union {
fpreal32_t sn;
fpreal64_t db;
} u;
} REAL;
#define SN(reg) (reg.u.sn)
#define DB(reg) (reg.u.db)
REAL FAReg;
REAL FBReg;
REAL FCReg;
REAL FARegSave;
REAL FBRegSave;
REAL FCRegSave;
int FP_Error; /* not preserved over descheduling */
int RoundingMode; /* current rounding mode */
int ResetRounding; /* reset rounding mode ? */
uint32_t m2dSourceStride; /* move2d source stride */
uint32_t m2dDestStride; /* move2d destination stride */
uint32_t m2dLength; /* move2d length (no. of rows) */
/* Other registers. */
uint32_t ClockReg[2];
uint32_t TNextReg[2];
uint32_t TPtrLoc[2]; /* XXX 0x80000024 0x80000028 */
uint32_t FPtrReg[2];
uint32_t BPtrReg[2];
#define ProcessQEmpty ((NotProcess_p == FPtrReg[0]) && (NotProcess_p == FPtrReg[1]))
#define TimerQEmpty ((NotProcess_p == TPtrLoc[0]) && (NotProcess_p == TPtrLoc[1]))
uint32_t STATUSReg; /* Processor flags: GotoSNPBit, HaltOnError, Error */
#define ClearInterrupt writeword (0x8000002C, IdleProcess_p)
#define ReadInterrupt (word (0x8000002C) != IdleProcess_p)
#define GotoSNPBit 0x00000001
#define HaltOnErrorFlag 0x00000080
#define ErrorFlag 0x80000000
#define SetGotoSNP STATUSReg |= GotoSNPBit
#define ClearGotoSNP STATUSReg &= ~GotoSNPBit
#define ReadGotoSNP (STATUSReg & GotoSNPBit)
#define SetError STATUSReg |= ErrorFlag
#define ClearError STATUSReg &= ~ErrorFlag
#define ReadError (STATUSReg & ErrorFlag)
#define SetHaltOnError STATUSReg |= HaltOnErrorFlag
#define ClearHaltOnError STATUSReg &= ~HaltOnErrorFlag
#define ReadHaltOnError (STATUSReg & HaltOnErrorFlag)
#define Temp_s ( 0)
#define Iptr_s (-1)
#define Link_s (-2)
#define State_s (-3)
#define Pointer_s (-3)
#define TLink_s (-4)
#define Time_s (-5)
#define GetDescPriority(wdesc) ((wdesc) & 0x00000001)
#define GetDescWPtr(wdesc) ((wdesc) & 0xfffffffe)
#define BitsPerByte 8
#define BytesPerWord 4
#define ByteSelectMask 0x00000003
#define BitsPerWord (BitsPerByte * BytesPerWord)
#define WordsRead(addr,len) (((addr&(BytesPerWord-1))?1:0)+(len+(BytesPerWord-1))/BytesPerWord)
#define BytesRead(addr,len) (WordsRead(addr,len)*BytesPerWord)
#ifdef NDEBUG
#define writeword(a,x) writeword_int(a,x)
#define writebyte(a,x) writebyte_int(a,x)
#define byte(a) byte_int(a)
#define word(a) word_int(a)
#endif
/* Internal variables. */
u_char Instruction;
u_char Icode;
u_char Idata;
int Timers;
uint32_t t4_overflow;
uint32_t t4_carry;
uint32_t t4_normlen;
uint32_t t4_carry64; /* shl64 shifted out bit */
uint32_t ProcPriority;
#define TimersGo 1
#define TimersStop 0
int loop;
int count1;
int count2;
int count3;
int timeslice;
int delayCount1 = 5;
int32_t quit = FALSE;
int32_t quitstatus;
/* Signal handler. */
void handler (int);
u_char *SharedLinks;
u_char *SharedEvents;
#define StrPrio(p) ((p) ? "Lo" : "Hi")
void UpdateWdescReg (uint32_t wdesc)
{
WdescReg = wdesc;
WPtr = GetDescWPtr(wdesc);
ProcPriority = GetDescPriority(wdesc);
}
#define Wdesc WdescReg
#define Idle (NotProcess_p == WPtr)
#define IdleProcess_p (NotProcess_p | LoPriority)
/* External variables. */
extern int analyse;
extern int nodeid;
extern int verbose;
extern int serve;
extern int exitonerror;
extern int FromServerLen;
extern int emudebug;
extern int memdebug;
extern int memnotinit;
extern int msgdebug;
extern int cachedebug;
extern char NetConfigName[256];
LinkIface Link[4];
/* Macros. */
#define index(a,b) ((a)+(BytesPerWord*(b)))
/* Profile information. */
uint32_t instrprof[0x400];
/*
#00 - #FF primary instr.
#100 - #2FF secondary instr. OReg
#300 - #3FF fpentry
*/
uint32_t combinedprof[0x400][0x400];
typedef struct _InstrSlot {
uint32_t IPtr;
uint32_t NextIPtr;
uint32_t OReg;
u_char Icode;
u_char rsvd[1];
u_short Pcode;
#ifdef EMUDEBUG
u_char Instruction;
#endif
} InstrSlot;
typedef struct _ArgSlot {
uint32_t _Arg0, _Arg1;
} ArgSlot;
#define Arg0 Acache[islot]._Arg0
#define Arg1 Acache[islot]._Arg1
#define IC_NOADDR 0xDEADBEEFU
#ifndef T4CACHEBITS
#define T4CACHEBITS (14)
#endif
#define MAX_ICACHE (1<<T4CACHEBITS)
#define OprCombined(x,y)(((x) == 0xf0)&&((y)>0xff)&&((y)<0x17c))
InstrSlot Icache[MAX_ICACHE+1];
ArgSlot Acache[MAX_ICACHE];
u_char *CLineTags;
#define CLINE_SIZE ((uint32_t)(1 << T4CLINEBITS))
static uint32_t IHASH(uint32_t a)
{
return a & (MAX_ICACHE-1);
}
static u_char IsCached(uint32_t a)
{
a &= MemByteMask; a >>= T4CLINEBITS;
return CLineTags[a >> 3] & (1 << (a & 7));
}
static void SetCached(uint32_t a)
{
a &= MemByteMask; a >>= T4CLINEBITS;
CLineTags[a >> 3] |= (1 << (a & 7));
}
static void ClearCached(uint32_t a)
{
a &= MemByteMask; a >>= T4CLINEBITS;
CLineTags[a >> 3] &= ~(1 << (a & 7));
}
#if 1
#define INVALIDATE_ADDR(a) InvalidateAddr(a)
static uint32_t InvalidateAddr (uint32_t a)
{
int i;
if (IsCached(a)) {
ClearCached(a);
a &= ~(CLINE_SIZE - 1);
for (i = 0; i < CLINE_SIZE; a++, i++) {
uint32_t x = IHASH(a);
if (a == Icache[x].IPtr)
Icache[x].IPtr = IC_NOADDR;
}
return a;
}
return (a & ~(CLINE_SIZE-1)) + CLINE_SIZE;
}
static void InvalidateRange (uint32_t a, uint32_t n)
{
uint32_t ha;
ha = a + n;
while (a < ha)
a = InvalidateAddr (a);
}
#else
#define INVALIDATE_ADDR(a) InvalidateSlot(IHASH(a), a)
static void InvalidateSlot (uint32_t x, uint32_t a)
{
if (a == Icache[x].IPtr)
Icache[x].IPtr = IC_NOADDR;
}
static void InvalidateRange (uint32_t a, uint32_t n)
{
uint32_t i, x;
x = IHASH(a);
Again: if (x + n > MAX_ICACHE)
{
n -= (MAX_ICACHE - x);
for (; x < MAX_ICACHE;)
InvalidateSlot (x++, a++);
x = 0;
goto Again;
}
else
for (i = 0; i < n; i++)
InvalidateSlot (x++, a++);
}
#endif
#define NO_ICODE 0x400
static struct {
u_short code0, code1;
uint32_t ccode;
} combined[] = {
{ 0xd0 /* stl */, 0x70 /* ldl */, 0x100 },
{ 0x70 /* ldl */, 0x30 /* ldnl */, 0x101 },
{ 0xc0 /* eqc */, 0xa0 /* cj */, 0x102 },
{ 0x70 /* ldl */, 0x50 /* ldnlp */, 0x103 },
{ 0x70 /* ldl */, 0x70 /* ldl */, 0x104 },
{ 0x10 /* ldlp */, 0x18a /* fpldnldb */, 0x105 },
{ 0xb0 /* ajw */, 0x120 /* ret */, 0x106 },
{ 0x10 /* ldlp */, 0x40 /* ldc */, 0x107 },
{ 0x10 /* ldlp */, 0x70 /* ldl */, 0x108 },
{ 0xd0 /* stl */, 0xd0 /* stl */, 0x109 },
{ 0x70 /* ldl */, 0x173 /* cflerr */, 0x10a },
{ 0x10 /* ldlp */, 0x188 /* fpstnlsn */, 0x10b },
{ 0x10 /* ldlp */, 0x18e /* fpldnlsn */, 0x10c },
{ 0x70 /* ldl */, 0x80 /* adc */, 0x10d },
{ 0x70 /* ldl */, 0xe0 /* stnl */, 0x10e },
{ 0x40 /* ldc */, 0x70 /* ldl */, 0x10f },
{ 0xd0 /* stl */, 0x00 /* j */, 0x110 },
{ 0x109 /* gt */, 0xa0 /* cj */, 0x111 },
{ 0x10a /* wsub */, 0xe0 /* stnl */, 0x112 },
{ 0x70 /* ldl */, 0x10a /* wsub */, 0x113 },
{ 0x15a /* dup */, 0xd0 /* stl */, 0x114 },
{ 0x142 /* mint */, 0x146 /* and */, 0x115 },
{ NO_ICODE, NO_ICODE, NO_ICODE }
};
static u_char combinations[0x400 * 0x400];
/* Support functions. */
#ifdef _MSC_VER
#define t4_bitcount(x) __popcnt (x)
#endif
#ifdef __GNUC__
#define t4_bitcount(x) __builtin_popcount (x)
#endif
#ifndef t4_bitcount
uint32_t t4_bitcount(uint32_t x)
{
uint32_t result;
result = 0;
while (x)
{
if (x & 1)
result++;
x >>= 1;
}
return result;
}
#endif
#ifdef __clang__
#define t4_bitreverse(x) __builtin_bitreverse32 (x)
#endif
#ifndef t4_bitreverse
uint32_t t4_bitreverse (uint32_t x)
{
unsigned int s = BitsPerWord;
uint32_t mask = ~0;
while ((s >>= 1) > 0)
{
mask ^= mask << s;
x = ((x >> s) & mask) | ((x << s) & ~mask);
}
return x;
}
#endif
void fp_drop (void)
{
FAReg = FBReg;
FBReg = FCReg;
}
void fp_drop2 (void)
{
FAReg = FCReg;
FBReg = FCReg;
}
/* Pop a REAL64 from the floating point stack. */
void fp_popdb (fpreal64_t *fp)
{
#ifndef NDEBUG
if (FAReg.length == FP_REAL64)
#endif
*fp = DB(FAReg);
#ifndef NDEBUG
else
{
printf ("-W-EMUFPU: Warning - FAReg is not REAL64! (fp_popdb)\n");
*fp = DUndefined;
}
#endif
fp_drop ();
}
/* Peek two REAL64s on the floating point stack. */
void fp_peek2db (fpreal64_t *fb, fpreal64_t *fa)
{
#ifndef NDEBUG
if (FBReg.length == FP_REAL64 && FAReg.length == FP_REAL64)
{
#endif
*fb = DB(FBReg);
*fa = DB(FAReg);
#ifndef NDEBUG
}
else
{
printf ("-W-EMUFPU: Warning - FBReg/FAReg are not REAL64! (fp_peek2db)\n");
*fb = DUndefined;
*fa = DUndefined;
}
#endif
}
/* Pop two REAL64s from the floating point stack. */
void fp_pop2db (fpreal64_t *fb, fpreal64_t *fa)
{
fp_peek2db (fb, fa);
fp_drop2 ();
}
/* Push a REAL64 to the floating point stack. */
void fp_pushdb (fpreal64_t fp)
{
FCReg = FBReg;
FBReg = FAReg;
FAReg.length = FP_REAL64;
DB(FAReg) = fp;
}
/* Pop a REAL32 from the floating point stack. */
void fp_popsn (fpreal32_t *fp)
{
#ifndef NDEBUG
if (FP_REAL32 == FAReg.length)
#endif
*fp = SN(FAReg);
#ifndef NDEBUG
else
{
printf ("-W-EMUFPU: Warning - FAReg is not REAL32! (fp_popsn)\n");
*fp = RUndefined;
}
#endif
fp_drop ();
}
/* Peek two REAL32s on the floating point stack. */
void fp_peek2sn (fpreal32_t *fb, fpreal32_t *fa)
{
#ifndef NDEBUG
if (FBReg.length == FP_REAL32 && FAReg.length == FP_REAL32)
{
#endif
*fb = SN(FBReg);
*fa = SN(FAReg);
#ifndef NDEBUG
}
else
{
printf ("-W-EMUFPU: Warning - FBReg/FAReg are not REAL64!\n");
*fb = RUndefined;
*fa = RUndefined;
}
#endif
}
/* Pop two REAL32s from the floating point stack. */
void fp_pop2sn (fpreal32_t *fb, fpreal32_t *fa)
{
fp_peek2sn (fb, fa);
fp_drop2 ();
}
/* Push a REAL32 to the floating point stack. */
void fp_pushsn (fpreal32_t fp)
{
FCReg = FBReg;
FBReg = FAReg;
FAReg.length = FP_REAL32;
SN(FAReg) = fp;
}
/* Do a binary floating point operation. */
#ifdef T4RELEASE
#define fp_dobinary(dbop, snop) \
{ \
fpreal64_t dbtemp1, dbtemp2; \
fpreal32_t sntemp1, sntemp2; \
\
ResetRounding = TRUE; \
\
if (FP_REAL64 == FAReg.length) \
{ \
fp_pop2db (&dbtemp1, &dbtemp2); \
fp_pushdb (dbop (dbtemp1, dbtemp2)); \
} \
else \
{ \
fp_pop2sn (&sntemp1, &sntemp2); \
fp_pushsn (snop (sntemp1, sntemp2)); \
} \
}
#else
void fp_dobinary (fpreal64_t (*dbop)(fpreal64_t,fpreal64_t),
fpreal32_t (*snop)(fpreal32_t,fpreal32_t))
{
fpreal64_t dbtemp1, dbtemp2;
fpreal32_t sntemp1, sntemp2;
ResetRounding = TRUE;
switch (FAReg.length)
{
case FP_REAL64:
fp_pop2db (&dbtemp1, &dbtemp2);
fp_pushdb (dbop (dbtemp1, dbtemp2));
break;
case FP_REAL32:
fp_pop2sn (&sntemp1, &sntemp2);
fp_pushsn (snop (sntemp1, sntemp2));
break;
default :
/* Just pop 2 items and set FAReg to unknown. */
printf ("-W-EMUFPU: Warning - FAReg is undefined! (fp_dobinary)\n");
fp_drop2 ();
fp_pushdb (DUndefined);
FAReg.length = FP_UNKNOWN;
break;
}
}
#endif
/* Do a binary floating point operation. */
int fp_binary2word (int (*dbop)(fpreal64_t,fpreal64_t),
int (*snop)(fpreal32_t,fpreal32_t))
{
fpreal64_t dbtemp1, dbtemp2;
fpreal32_t sntemp1, sntemp2;
int result;
ResetRounding = TRUE;
switch (FAReg.length)
{
case FP_REAL64:
fp_pop2db (&dbtemp1, &dbtemp2);
result = dbop (dbtemp1, dbtemp2);
break;
case FP_REAL32:
fp_pop2sn (&sntemp1, &sntemp2);
result = snop (sntemp1, sntemp2);
break;
default :
/* Just pop 2 items and set FAReg to unknown. */
printf ("-W-EMUFPU: Warning - FAReg is undefined! (fp_binary2word)\n");
fp_drop2 ();
result = FALSE;
break;
}
return result;
}
/* Do an unary floating point operation. */
#ifdef T4RELEASE
#define fp_dounary(dbop, snop) \
{ \
fpreal64_t dbtemp; \
fpreal32_t sntemp; \
\
ResetRounding = TRUE; \
\
if (FP_REAL64 == FAReg.length) \
{ \
fp_popdb (&dbtemp); \
fp_pushdb (dbop (dbtemp)); \
} \
else \
{ \
fp_popsn (&sntemp); \
fp_pushsn (snop (sntemp)); \
} \
}
#else
void fp_dounary (fpreal64_t (*dbop)(fpreal64_t), fpreal32_t (*snop)(fpreal32_t))
{
fpreal64_t dbtemp;
fpreal32_t sntemp;
ResetRounding = TRUE;
switch (FAReg.length)
{
case FP_REAL64:
fp_popdb (&dbtemp);
fp_pushdb (dbop (dbtemp));
break;
case FP_REAL32:
fp_popsn (&sntemp);
fp_pushsn (snop (sntemp));
break;
default :
/* Just pop 2 items and set FAReg to unknown. */
printf ("-W-EMUFPU: Warning - FAReg is undefined! (fp_dounary)\n");
fp_drop ();
fp_pushdb (DUndefined);
FAReg.length = FP_UNKNOWN;
break;
}
}
#endif
struct timeval LastTOD; /* Time-of-day */
/* Update time-of-day. */
void update_tod (struct timeval *tp)
{
int rc;
rc = gettimeofday (tp, (void *)0);
if (rc < 0)
{
printf ("-W-EMU414: Failed to get time value.\n");
*tp = LastTOD;
tp->tv_usec++;
if (0 == tp->tv_usec)
tp->tv_sec++;
}
}
#define C_UNKNOWN -1
#define C_POKE 0
#define C_PEEK 1
#define C_BOOT 2
static uint32_t BootLink = 0;
static int CtrlByte = C_UNKNOWN;
int handleboot (Channel *chan, u_char *data, int ndata)
{
uint32_t address, value;
Channel *outchan;
EMUDBG2 ("-I-EMUDBG: Handle boot, control byte = %d.\n", CtrlByte);
if (C_UNKNOWN == CtrlByte)
{
CtrlByte = data[0];
chan->Address = MemStart;
if (C_POKE == CtrlByte)
chan->Length = 8;
else if (C_PEEK == CtrlByte)
chan->Length = 4;
else
{
chan->Length = CtrlByte;
CtrlByte = C_BOOT;
BootLink = chan->LinkAddress;
}
EMUDBG2 ("-I-EMUDBG: Control byte #%02X.\n", CtrlByte);
data++; ndata--;
}
if (chan->Length)
{
int len = ndata;
if (chan->Length < len)
len = chan->Length;
writebytes_int (chan->Address, data, len);
chan->Address += len; chan->Length -= len;
}
if (0 == chan->Length)
{
switch (CtrlByte)
{
case C_POKE:
address = word_int (MemStart);
value = word_int (MemStart + 4);
writeword_int (address, value);
CtrlByte = C_UNKNOWN;
break;
case C_PEEK:
address = word_int (MemStart);
value = word_int (address);
data[0] = value & 255; value >>= 8;
data[1] = value & 255; value >>= 8;
data[2] = value & 255; value >>= 8;
data[3] = value & 255;
outchan = &Link[chan->Link].Out;
ndata = nn_send (outchan->sock, data, ndata, 0);
if (4 != ndata)
{
printf ("-E-EMU414: Failed to send %d bytes on Link%dOut (%s) @ handleboot()\n", ndata, outchan->Link, nn_strerror (nn_errno ()));
handler (-1);
}
CtrlByte = C_UNKNOWN;
break;
case C_BOOT:
UpdateWdescReg (chan->Address | ProcPriority);
CtrlByte = C_UNKNOWN;
return 0;
}
}
return 1;
}
int channel_ready (Channel *chan)
{
struct nn_pollfd pfd[1];
int ret;
EMUDBG2 ("-I-EMUDBG: ChannelReady. Link%dIn ready ?\n", chan->Link);
if (chan->schbuf)
{
ret = 0 != SCHLength(chan);
goto Exit;
}
pfd[0].fd = chan->sock;
pfd[0].events = NN_POLLIN;
pfd[0].revents = 0;
EMUDBG2 ("-I-EMUDBG: Polling Link%dIn.\n", chan->Link);
ret = nn_poll (pfd, 1, LTO_POLL);
if (-1 == ret) /* error */
{
printf ("-E-EMU414: Failed polling Link%dIn (%s)\n", chan->Link, nn_strerror (nn_errno ()));
handler (-1);
}
Exit:
if (0 == ret) /* timeout */
{
MSGDBG2 ("-I-EMUDBG: Link%dIn timeout.\n", chan->Link);
return 1;
}
return 0;
}
int channel_recvP (Channel *chan, u_char *data, int doWait)
{
int ret;
if (chan->schbuf)
{
if (0 == SCHLength(chan))
{
errno = EAGAIN;
ret = -1;
}
else
{
ret = SCHLength(chan);
memcpy (data, &chan->schbuf[SCH_DATA], ret);
SCHLength(chan) = 0;
}
}
else
ret = nn_recv (chan->sock, data, MAX_DATA, doWait ? 0 :NN_DONTWAIT);
if (-1 == ret)
{
if ((EAGAIN == errno) && !doWait)
return -1;
printf ("-E-EMU414: channel_recvP: Receive failed on Link%dIn (%s)\n",
chan->Link,
nn_strerror (nn_errno ()));
handler (-1);
}
MSGDBG4 ("-I-EMUDBG: channel_recvP: Received %d bytes on Link%dIn (#%08X).\n",
ret,
chan->Link,
chan->LinkAddress);
return ret;
}
int channel_recvmemP (Channel *chan, u_char *data, int doMemWrite, int doWait)
{
int ret;
u_char *buf;
buf = doMemWrite ? memrange (chan->Address, data, MAX_DATA) : data;
ret = channel_recvP (chan, buf, doWait);
if (ret < 0)
return ret;
if (doMemWrite)
{
if (buf == data)
writebytes_int (chan->Address, data, ret);
else
InvalidateRange (chan->Address, ret);
chan->Address += ret; chan->Length -= ret;
}
return ret;
}
int channel_sendP (Channel *chan, u_char *data, int ndata, int doWait)
{
int ret;
ret = 0;
if (chan->schbuf)
{
if (SCHLength(chan))
{
errno = EAGAIN;
ret = -1;
}
else
{
#ifndef NDEBUG
if (ndata > MAX_DATA)
{
printf ("-E-EMU414: channel_sendP: schbuf[] overflow! (%d).\n", ndata);
}
#endif
memcpy (&chan->schbuf[SCH_DATA], data, ndata);
SCHLength(chan) = ndata;
ret = ndata;
}
}
else
ret = nn_send (chan->sock, data, ndata, doWait ? 0 : NN_DONTWAIT);
if (-1 == ret)
{
if ((EAGAIN == errno) && !doWait)
return -1;
printf ("-E-EMU414: channel_sendP: Send failed on Link%dOut (%s).\n",
chan->Link,
nn_strerror (nn_errno ()));
handler (-1);
}
if (ret != ndata)
{
printf ("-E-EMU414: channel_sendP: Failed to send %d bytes on Link%dOut (%s).\n",
ndata,
chan->Link,
nn_strerror (nn_errno ()));
handler (-1);
}
MSGDBG4 ("-I-EMUDBG: channel_sendP: Sent %d bytes on Link%dOut (#%08X).\n",
ndata,
chan->Link,
chan->LinkAddress);
return ndata;
}
int channel_sendmemP (Channel *chan, int doWait)
{
u_char data[MAX_DATA];
int ndata;
u_char *buf;
ndata = MAX_DATA;
if (chan->Length < MAX_DATA)
ndata = chan->Length;
buf = bytes_int (chan->Address, data, ndata);
chan->Address += ndata; chan->Length -= ndata;
return channel_sendP (chan, buf, ndata, doWait);
}
/* Receive at most MAX_DATA bytes without waiting. */