aboutsummaryrefslogtreecommitdiff
path: root/sim/z8k/inlines.h
blob: 1bf209ece49bf3cec08a86bfcaaade825d2234a6 (plain)
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
/* inline functions for Z8KSIM
   Copyright (C) 1992, 1993 Free Software Foundation, Inc.

This file is part of Z8KSIM

GNU CC 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 2, or (at your option)
any later version.

GNU CC 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 Z8KZIM; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

#ifndef INLINE
#define INLINE
#endif
#define UGT 0x0b
#define ULE 0x03
#define ULT 0x07
#define UGE 0x0f
#define SLOW 0
#define T 0x8
#define F 0x0
#define LT 0x1
#define GT 0xa
#define LE 0x2
#define EQ 0x6
#define NE 0xe
#define GE 0x9

 static int is_cond_true PARAMS((sim_state_type *context, int c)); 
 static void makeflags PARAMS((sim_state_type *context, int mask)); 

static INLINE
long
sitoptr (si)
long si;
{
  return ((si & 0xff000000) >> 8) | (si & 0xffff);
}
static INLINE long
ptrtosi (ptr)
long ptr;
{
  return ((ptr & 0xff0000) << 8) | (ptr & 0xffff);
}

static INLINE
void 
put_long_reg (context, reg, val)
     sim_state_type *context;
     int reg;
     int val;
{
  context->regs[reg].word = val >> 16;
  context->regs[reg + 1].word = val;
}

static INLINE
void 
put_quad_reg (context, reg, val1, val2)
     sim_state_type *context;
     int reg;
     int val1;
     int val2;
{
  context->regs[reg].word = val2 >> 16;
  context->regs[reg + 1].word = val2;
  context->regs[reg + 2].word = val1 >> 16;
  context->regs[reg + 3].word = val1;
}

static INLINE
void 
put_word_reg (context, reg, val)
     sim_state_type *context;
     int reg;
     int val;
{
  context->regs[reg].word = val;
}

static INLINE
SItype get_long_reg (context, reg)
     sim_state_type *context;
     int reg;
{
  USItype lsw = context->regs[reg + 1].word;
  USItype msw = context->regs[reg].word;

  return (msw << 16) | lsw;
}

#ifdef __GNUC__
static INLINE
struct UDIstruct
get_quad_reg (context, reg)
     sim_state_type *context;
     int reg;
{
  UDItype res;
  USItype lsw = get_long_reg (context, reg + 2);
  USItype msw = get_long_reg (context, reg);

  res.low = lsw;
  res.high = msw;
  return res;
}

#endif

static INLINE void
put_byte_mem_da (context, addr, value)
     sim_state_type *context;
     int addr;
     int value;
{
  ((unsigned char *) (context->memory))[addr] = value;
}

static INLINE
void 
put_byte_reg (context, reg, val)
     sim_state_type *context;
     int reg;
     int val;
{
  int old = context->regs[reg & 0x7].word;
  if (reg & 0x8)
    {
      old = old & 0xff00 | (val & 0xff);
    }
  else
    {
      old = old & 0x00ff | (val << 8);
    }
  context->regs[reg & 0x7].word = old;      
}

static INLINE
int 
get_byte_reg (context, reg)
     sim_state_type *context;
     int reg;
{
  if (reg & 0x8)
    return  context->regs[reg & 0x7].word & 0xff;
  else
    return  (context->regs[reg & 0x7].word >> 8) & 0xff;
}

static INLINE
void 
put_word_mem_da (context, addr, value)
     sim_state_type *context;
     int addr;
     int value;
{
  if (addr & 1)
    {
      context->exception = SIM_BAD_ALIGN;
      addr &= ~1;
    }
  put_byte_mem_da(context, addr, value>>8);
  put_byte_mem_da(context, addr+1, value);
}

static INLINE unsigned char
get_byte_mem_da (context, addr)
     sim_state_type *context;
     int addr;
{
  return ((unsigned char *) (context->memory))[addr];
}


#if 0
#define get_word_mem_da(context,addr)\
 *((unsigned short*)((char*)((context)->memory)+(addr)))

#else
#define get_word_mem_da(context,addr) (get_byte_mem_da(context, addr) << 8) | (get_byte_mem_da(context,addr+1))
#endif

#define get_word_reg(context,reg) (context)->regs[reg].word

static INLINE
SItype
get_long_mem_da (context, addr)
     sim_state_type *context;
     int addr;
{
  USItype lsw = get_word_mem_da(context,addr+2);
  USItype msw =  get_word_mem_da(context, addr);

  return (msw << 16) + lsw;
}

static INLINE
void 
put_long_mem_da (context, addr, value)
     sim_state_type *context;
     int addr;
     int value;
{
  put_word_mem_da(context,addr, value>>16);
  put_word_mem_da(context,addr+2, value);
}

static INLINE
int 
get_word_mem_ir (context, reg)
     sim_state_type *context;
     int reg;
{
  return get_word_mem_da (context, get_word_reg (context, reg));
}

static INLINE
void 
put_word_mem_ir (context, reg, value)
     sim_state_type *context;
     int reg;
     int value;
{

  put_word_mem_da (context, get_word_reg (context, reg), value);
}

static INLINE
int 
get_byte_mem_ir (context, reg)
     sim_state_type *context;
     int reg;
{
  return get_byte_mem_da (context, get_word_reg (context, reg));
}

static INLINE
void 
put_byte_mem_ir (context, reg, value)
     sim_state_type *context;
     int reg;
     int value;
{
  put_byte_mem_da (context, get_word_reg (context, reg), value);
}

static INLINE
int 
get_long_mem_ir (context, reg)
     sim_state_type *context;
     int reg;
{
  return get_long_mem_da (context, get_word_reg (context, reg));
}

static INLINE
void 
put_long_mem_ir (context, reg, value)
     sim_state_type *context;
     int reg;
     int value;
{

  put_long_mem_da (context, get_word_reg (context, reg), value);
}

static INLINE
void 
put_long_mem_x (context, base, reg, value)
     sim_state_type *context;
     int base;
     int reg;
     int value;
{
  put_long_mem_da (context, get_word_reg (context, reg) + base, value);
}

static INLINE
void 
put_word_mem_x (context, base, reg, value)
     sim_state_type *context;
     int base;
     int reg;
     int value;
{
  put_word_mem_da (context, get_word_reg (context, reg) + base, value);
}

static INLINE
void 
put_byte_mem_x (context, base, reg, value)
     sim_state_type *context;
     int base;
     int reg;
     int value;
{
  put_byte_mem_da (context, get_word_reg (context, reg) + base, value);
}

static INLINE
int 
get_word_mem_x (context, base, reg)
     sim_state_type *context;
     int base;
     int reg;
{
  return get_word_mem_da (context, base + get_word_reg (context, reg));
}

static INLINE
int 
get_byte_mem_x (context, base, reg)
     sim_state_type *context;
     int base;
     int reg;
{
  return get_byte_mem_da (context, base + get_word_reg (context, reg));
}

static INLINE
int 
get_long_mem_x (context, base, reg)
     sim_state_type *context;
     int base;
     int reg;
{
  return get_long_mem_da (context, base + get_word_reg (context, reg));
}


static
void
makeflags (context, mask)
     sim_state_type *context;
     int mask;
{

  PSW_ZERO = (context->dst & mask) == 0;
  PSW_SIGN = (context->dst >> (context->size - 1));

  if (context->broken_flags == TST_FLAGS)
    {
      extern char the_parity[];

      if (context->size == 8)
	{
	  PSW_OVERFLOW = the_parity[context->dst & 0xff];
	}
    }
  else
    {
      /* Overflow is set if both operands have the same sign and the
         result is of different sign.

         V =  A==B && R!=B  jumping logic
         (~(A^B))&(R^B)
         V =  (A^B)^(R^B)   boolean
         */

      PSW_OVERFLOW =
	((
	   (~(context->srca ^ context->srcb)
	    & (context->srca ^ context->dst))
	 ) >> (context->size - 1)
	);

      if (context->size < 32)
	{
	  PSW_CARRY = ((context->dst >> context->size)) & 1;
	}
      else
	{
	  /* carry is set when the result is smaller than a source */


	  PSW_CARRY =  (unsigned) context->dst > (unsigned) context->srca ;

	}
    }
  context->broken_flags = 0;
}


/* There are two ways to calculate the flags.  We can
   either always calculate them and so the cc will always
   be correct, or we can only keep the arguments around and
   calc the flags when they're actually going to be used. */

/* Right now we always calc the flags - I think it may be faster*/


#define NORMAL_FLAGS(c,s,d,sa,sb,sub) 	\
    if (s == 8)                \
      normal_flags_8(c,d,sa,sb,sub); \
    else if (s == 16)                \
      normal_flags_16(c,d,sa,sb,sub); \
    else if (s == 32)                \
      normal_flags_32(c,d,sa,sb,sub); 

static INLINE
void 
normal_flags (context, size, dst, srca, srcb)
     sim_state_type *context;
     int size;
     int dst;
     int srca;
     int srcb;
{
  context->srca = srca;
  context->srcb = srcb;
  context->dst = dst;
  context->size = size;
  context->broken_flags = CMP_FLAGS;
}

static INLINE
void 
TEST_NORMAL_FLAGS (context, size, dst)
     sim_state_type *context;
     int size;
     int dst;
{
  context->dst = dst;
  context->size = size;
  context->broken_flags = TST_FLAGS;
}

static INLINE
void 
put_ptr_long_reg (context, reg, val)
     sim_state_type *context;
     int reg;
     int val;
{
  context->regs[reg].word = (val >> 8) & 0x7f00;
  context->regs[reg + 1].word = val;
}

static INLINE
long 
get_ptr_long_reg (context, reg)
     sim_state_type *context;
     int reg;
{
  int val;

  val = (context->regs[reg].word << 8) | context->regs[reg + 1].word;
  return val;
}

static INLINE
long 
get_ptr_long_mem_ir (context, reg)
sim_state_type *context;
int reg;
{
  return sitoptr (get_long_mem_da (context, get_ptr_long_reg (context, reg)));
}

static INLINE
long 
get_ptr_long_mem_da (context, addr)
sim_state_type *context;
long addr; 
{
  return sitoptr (get_long_mem_da (context, addr));
}

static INLINE
void 
put_ptr_long_mem_da (context, addr, ptr)
sim_state_type *context;
long addr; 
long ptr;
{
  put_long_mem_da (context, addr, ptrtosi (ptr));

}