file
stringlengths 18
26
| data
stringlengths 3
1.04M
|
---|---|
the_stack_data/65220.c | // MUSL memset implementation:
// https://github.com/esmil/musl/blob/master/src/string/memset.c
#include <stdint.h>
#include <string.h>
void* __attribute__((weak)) memset(void* dest, int c, size_t n)
{
unsigned char* s = dest;
size_t k;
/* Fill head and tail with minimal branching. Each
* conditional ensures that all the subsequently used
* offsets are well-defined and in the dest region. */
if(!n)
{
return dest;
}
s[0] = s[n - 1] = (unsigned char)c;
if(n <= 2)
{
return dest;
}
s[1] = s[n - 2] = (unsigned char)c;
s[2] = s[n - 3] = (unsigned char)c;
if(n <= 6)
{
return dest;
}
s[3] = s[n - 4] = (unsigned char)c;
if(n <= 8)
{
return dest;
}
/* Advance pointer to align it at a 4-byte boundary,
* and truncate n to a multiple of 4. The previous code
* already took care of any head/tail that get cut off
* by the alignment. */
k = -(uintptr_t)s & 3;
s += k;
n -= k;
n &= (unsigned long)-4;
n /= 4;
// Cast to void first to prevent alignment warning
uint32_t* ws = (uint32_t*)(void*)s;
uint32_t wc = c & 0xFF;
wc |= ((wc << 8) | (wc << 16) | (wc << 24));
/* Pure C fallback with no aliasing violations. */
for(; n; n--, ws++)
{
{
*ws = wc;
}
}
return dest;
}
|
the_stack_data/165766085.c | // Copyright (c) 2015 MIT License by 6.172 Staff
#include <stdint.h>
#include <stdlib.h>
#include <math.h>
#define SIZE (1L << 32)
void test(uint8_t* restrict a, uint8_t* restrict b) {
a = __builtin_assume_aligned(a, 16);
b = __builtin_assume_aligned(b, 16);
for (uint64_t i = 0; i < SIZE; i++) {
a[i] += b[i];
}
}
|
the_stack_data/85766.c | #include <stdio.h>
int main(void) {
int i = 1;
while(i < 21) {
printf("%d - ", i);
if(i % 2 == 0) {
printf("Even\n");
} else {
printf("Odd\n");
}
i++;
}
return 0;
}
|
the_stack_data/644454.c | #define NULL ((void*)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t; typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1
/* Forward declarations */
typedef struct TYPE_4__ TYPE_2__ ;
typedef struct TYPE_3__ TYPE_1__ ;
/* Type definitions */
typedef int /*<<< orphan*/ u64 ;
struct net_device {int dummy; } ;
struct ksz_port {int first_port; int mib_port_cnt; } ;
struct ksz_hw {int mib_cnt; TYPE_1__* port_mib; } ;
struct ethtool_stats {int n_stats; } ;
struct dev_priv {struct ksz_port port; struct dev_info* adapter; } ;
struct dev_info {TYPE_2__* counter; int /*<<< orphan*/ mib_read; int /*<<< orphan*/ lock; struct ksz_hw hw; } ;
struct TYPE_4__ {int read; int /*<<< orphan*/ counter; } ;
struct TYPE_3__ {scalar_t__ state; scalar_t__ cnt_ptr; } ;
/* Variables and functions */
int HZ ;
int SWITCH_PORT_NUM ;
int TOTAL_PORT_COUNTER_NUM ;
int /*<<< orphan*/ get_mib_counters (struct ksz_hw*,int,int,int /*<<< orphan*/ *) ;
scalar_t__ media_connected ;
int /*<<< orphan*/ mutex_lock (int /*<<< orphan*/ *) ;
int /*<<< orphan*/ mutex_unlock (int /*<<< orphan*/ *) ;
struct dev_priv* netdev_priv (struct net_device*) ;
int /*<<< orphan*/ schedule_work (int /*<<< orphan*/ *) ;
int wait_event_interruptible_timeout (int /*<<< orphan*/ ,int,int) ;
__attribute__((used)) static void netdev_get_ethtool_stats(struct net_device *dev,
struct ethtool_stats *stats, u64 *data)
{
struct dev_priv *priv = netdev_priv(dev);
struct dev_info *hw_priv = priv->adapter;
struct ksz_hw *hw = &hw_priv->hw;
struct ksz_port *port = &priv->port;
int n_stats = stats->n_stats;
int i;
int n;
int p;
int rc;
u64 counter[TOTAL_PORT_COUNTER_NUM];
mutex_lock(&hw_priv->lock);
n = SWITCH_PORT_NUM;
for (i = 0, p = port->first_port; i < port->mib_port_cnt; i++, p++) {
if (media_connected == hw->port_mib[p].state) {
hw_priv->counter[p].read = 1;
/* Remember first port that requests read. */
if (n == SWITCH_PORT_NUM)
n = p;
}
}
mutex_unlock(&hw_priv->lock);
if (n < SWITCH_PORT_NUM)
schedule_work(&hw_priv->mib_read);
if (1 == port->mib_port_cnt && n < SWITCH_PORT_NUM) {
p = n;
rc = wait_event_interruptible_timeout(
hw_priv->counter[p].counter,
2 == hw_priv->counter[p].read,
HZ * 1);
} else
for (i = 0, p = n; i < port->mib_port_cnt - n; i++, p++) {
if (0 == i) {
rc = wait_event_interruptible_timeout(
hw_priv->counter[p].counter,
2 == hw_priv->counter[p].read,
HZ * 2);
} else if (hw->port_mib[p].cnt_ptr) {
rc = wait_event_interruptible_timeout(
hw_priv->counter[p].counter,
2 == hw_priv->counter[p].read,
HZ * 1);
}
}
get_mib_counters(hw, port->first_port, port->mib_port_cnt, counter);
n = hw->mib_cnt;
if (n > n_stats)
n = n_stats;
n_stats -= n;
for (i = 0; i < n; i++)
*data++ = counter[i];
} |
the_stack_data/89198990.c | #ifdef ENABLE_CODE_MCBITS // don't want this file in Visual Studio if libsodium is not present
#include <assert.h>
#include <stdint.h>
#include <string.h>
#include "params.h"
#include <oqs/rand.h>
#include <oqs/sha3.h>
#include <sodium/crypto_onetimeauth_poly1305.h>
#include <sodium/crypto_stream_salsa20.h>
#include <sodium/randombytes.h>
// clang-format off
// (order of include matters)
#include "util.c"
#include "transpose.c"
#include "benes.c"
#include "gf.c"
#include "vec.c"
#include "bm.c"
#include "fft.c"
#include "fft_tr.c"
#include "sk_gen.c"
#include "pk_gen.c"
#include "encrypt.c"
#include "decrypt.c"
// clang-format on
int oqs_kex_mcbits_encrypt(
unsigned char *c, size_t *clen,
const unsigned char *m, unsigned long long mlen,
const unsigned char *pk,
OQS_RAND *r) {
unsigned char e[1 << (GFBITS - 3)];
unsigned char key[64];
unsigned char nonce[8] = {0};
//
#define ct (c + SYND_BYTES)
#define tag (ct + mlen)
encrypt(c, e, pk, r);
//crypto_hash_keccakc1024(key, e, sizeof(e)); TODO is this ok to replace with the below?
OQS_SHA3_sha3512(key, e, sizeof(e));
crypto_stream_salsa20_xor(ct, m, mlen, nonce, key);
crypto_onetimeauth_poly1305(tag, ct, mlen, key + 32);
*clen = SYND_BYTES + mlen + 16;
#undef ct
#undef tag
return 0;
}
int oqs_kex_mcbits_decrypt(
unsigned char *m, size_t *mlen,
const unsigned char *c, unsigned long long clen,
const unsigned char *sk) {
int ret;
int ret_verify;
int ret_decrypt;
unsigned char key[64];
unsigned char nonce[8] = {0};
unsigned char e[1 << (GFBITS - 3)];
//
if (clen < SYND_BYTES + 16)
return -1;
else
*mlen = clen - SYND_BYTES - 16;
#define ct (c + SYND_BYTES)
#define tag (ct + *mlen)
ret_decrypt = decrypt(e, sk, c);
//crypto_hash_keccakc1024(key, e, sizeof(e)); TODO is this ok to replace with the below?
OQS_SHA3_sha3512(key, e, sizeof(e));
ret_verify = crypto_onetimeauth_poly1305_verify(tag, ct, *mlen, key + 32);
crypto_stream_salsa20_xor(m, ct, *mlen, nonce, key);
ret = ret_verify | ret_decrypt;
#undef ct
#undef tag
return ret;
}
int oqs_kex_mcbits_gen_keypair(
unsigned char *pk,
unsigned char *sk,
OQS_RAND *r
) {
while (1) {
sk_gen(sk, r);
if (pk_gen(pk, sk) == 0)
break;
}
return 0;
}
#endif
|
the_stack_data/165769093.c | #include <stdio.h>
struct q {
int b;
struct q2 {
struct q3 {
int h;
} h;
} a;
struct q4 {
int h;
} h;
unsigned long long q;
struct q4 y;
};
int main() {
struct q a /*= {1, 2, 3}*/;
a.b = 1;
a.a.h.h = 2;
a.h.h = 3;
a.y.h = 7;
printf("%d\n", a.y.h);
return 0;
} |
the_stack_data/1255671.c | /* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isspace.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mscot <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/03 17:44:06 by mscot #+# #+# */
/* Updated: 2020/11/03 17:44:10 by mscot ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isspace(char c)
{
return ((c >= 9 && c <= 13) || c == 32);
}
|
the_stack_data/144349.c | /* This testcase is part of GDB, the GNU debugger.
Copyright 2009-2019 Free Software Foundation, Inc.
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, see <http://www.gnu.org/licenses/>. */
struct s
{
int a;
int b;
};
struct SS
{
union { int x; char y; };
union { int a; char b; };
};
typedef struct s TS;
TS ts;
int aligncheck;
#ifdef __cplusplus
struct C
{
int c;
int d;
int
a_method (int x, char y)
{
return x + y;
}
int
a_const_method (int x, char y) const
{
return x + y;
}
static int
a_static_method (int x, char y)
{
return x + y;
}
};
struct D : C
{
int e;
int f;
};
template<typename T, int I, int C::*MP>
struct Temargs
{
};
Temargs<D, 23, &C::c> temvar;
#endif
enum E
{ v1, v2, v3
};
struct s vec_data_1 = {1, 1};
struct s vec_data_2 = {1, 2};
static int
a_function (int x, char y)
{
return x + y;
}
int
main ()
{
int ar[2] = {1,2};
struct s st;
struct SS ss;
#ifdef __cplusplus
C c;
c.c = 1;
c.d = 2;
D d;
d.e = 3;
d.f = 4;
c.a_method (0, 1);
c.a_const_method (0, 1);
C::a_static_method (0, 1);
#endif
enum E e;
st.a = 3;
st.b = 5;
e = v2;
ss.x = 100;
a_function (0, 1);
return 0; /* break to inspect struct and array. */
}
|
the_stack_data/67325725.c | #include <stdio.h>
#define lower 0
#define upper 300
#define step 20
main()
{
int fahr,celsius;
fahr=lower;
while(fahr<=upper){
celsius=5*(fahr-32.0)/9;
printf("%d\t %d\n",fahr,celsius);
fahr=fahr+step;
}
}
|
the_stack_data/45450964.c | //Chris Wells 2015
//956335
//September 29, 2014
//oddoreven.c
//Purpose: To determine if a number is even or odd.
#include <stdio.h>
//Begin main method
void main()
{
int a; //Declare integer variable
//Request variable from user
printf("Please enter an integer that is greater than 0:");
scanf("%d", &a);
if( a <= 0 ) //Out of range
printf("The number you entered is out of range, please enter an integer greater than 0.");
else if( a % 2 == 0 ) //Even
printf("The number is even.");
else if( a % 2 == 1 ) //Odd
printf("The number is odd.");
} |
the_stack_data/888351.c | #include<stdio_ext.h>
#define TAM 20
void main(){
char G[TAM], R[TAM];
int acerto = 0,i,j;
printf("---Informe o gabarito da prova---\n");
for(int i=0; i<TAM; i++){
printf("Questao %d: ", i+1);
__fpurge(stdin);
scanf("%c", &G[i]);
}
for(i=0;i<50; i++){
printf("--Aluno %d--\n", i+1);
acerto = 0;
for(j=0; j<50; j++){
printf("Questao %d: ", j+1);
__fpurge(stdin);
scanf("%c", &R[j]);
if(R[j] == G[j]){
printf("Acertou!\n");
acerto++;
}
}
if(acerto >= 6){
printf("Aluno %d APROVADO com %d acertos!\n", i, acerto);
}else{
printf("Aluno %d REPROVADO com %d acertos!\n", i, acerto);
}
}
}
|
the_stack_data/68888338.c | /* Multi-Z80 32 Bit emulator */
/* Copyright 1996, 1997, 1998, 1999, 2000 Neil Bradley, All rights reserved
*
* License agreement:
*
* (MZ80 Refers to both the assembly and C code emitted by makeZ80.c and
* makeZ80.c itself)
*
* MZ80 May be distributed in unmodified form to any medium.
*
* MZ80 May not be sold, or sold as a part of a commercial package without
* the express written permission of Neil Bradley ([email protected]). This
* includes shareware.
*
* Modified versions of MZ80 may not be publicly redistributed without author
* approval ([email protected]). This includes distributing via a publicly
* accessible LAN. You may make your own source modifications and distribute
* MZ80 in source or object form, but if you make modifications to MZ80
* then it should be noted in the top as a comment in makeZ80.c.
*
* MZ80 Licensing for commercial applications is available. Please email
* [email protected] for details.
*
* Synthcom Systems, Inc, and Neil Bradley will not be held responsible for
* any damage done by the use of MZ80. It is purely "as-is".
*
* If you use MZ80 in a freeware application, credit in the following text:
*
* "Multi-Z80 CPU emulator by Neil Bradley ([email protected])"
*
* must accompany the freeware application within the application itself or
* in the documentation.
*
* Legal stuff aside:
*
* If you find problems with MZ80, please email the author so they can get
* resolved. If you find a bug and fix it, please also email the author so
* that those bug fixes can be propogated to the installed base of MZ80
* users. If you find performance improvements or problems with MZ80, please
* email the author with your changes/suggestions and they will be rolled in
* with subsequent releases of MZ80.
*
* The whole idea of this emulator is to have the fastest available 32 bit
* Multi-Z80 emulator for the PC, giving maximum performance.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define VERSION "3.4"
#define TRUE 0xff
#define FALSE 0x0
#define INVALID 0xff
#define UINT32 unsigned long int
#define UINT8 unsigned char
#define TIMING_REGULAR 0x00
#define TIMING_XXCB 0x01
#define TIMING_CB 0xcb
#define TIMING_DDFD 0xdd
#define TIMING_ED 0xed
#define TIMING_EXCEPT 0x02
FILE *fp = NULL;
char string[150];
char cpubasename[150];
static char mz80Index[50];
static char mz80IndexHalfHigh[50];
static char mz80IndexHalfLow[50];
char majorOp[50];
char procname[150];
UINT32 dwGlobalLabel = 0;
enum
{
MZ80_ASSEMBLY_X86,
MZ80_C,
MZ80_UNKNOWN
};
UINT8 bPlain = FALSE;
UINT8 bNoTiming = FALSE;
UINT8 bUseStack = 0;
UINT8 bCurrentMode = TIMING_REGULAR; // Current timing mode
UINT8 b16BitIo = FALSE;
UINT8 bThroughCallHandler = FALSE;
UINT8 bOS2 = FALSE;
UINT8 bWhat = MZ80_UNKNOWN;
void ProcBegin(UINT32 dwOpcode);
UINT8 *pbLocalReg[8] =
{
"ch",
"cl",
"dh",
"dl",
"bh",
"bl",
"dl",
"al"
};
UINT8 *pbLocalRegC[8] =
{
"cpu.z80B",
"cpu.z80C",
"cpu.z80D",
"cpu.z80E",
"cpu.z80H",
"cpu.z80L",
"barf",
"cpu.z80A"
};
UINT8 *pbPushReg[8] =
{
"cl",
"ch",
"byte [_z80de]",
"byte [_z80de + 1]",
"bl",
"bh",
"ah",
"al"
};
UINT8 *pbFlags[8] =
{
"nz",
"z",
"nc",
"c",
"po",
"pe",
"ns",
"s"
};
UINT8 *pbRegPairC[] =
{
"cpu.z80BC",
"cpu.z80DE",
"cpu.z80HL",
"cpu.z80sp"
};
UINT8 *pbFlagsC[8] =
{
"(!(cpu.z80F & Z80_FLAG_ZERO))",
"(cpu.z80F & Z80_FLAG_ZERO)",
"(!(cpu.z80F & Z80_FLAG_CARRY))",
"(cpu.z80F & Z80_FLAG_CARRY)",
"(!(cpu.z80F & Z80_FLAG_OVERFLOW_PARITY))",
"(cpu.z80F & Z80_FLAG_OVERFLOW_PARITY)",
"(!(cpu.z80F & Z80_FLAG_SIGN))",
"(cpu.z80F & Z80_FLAG_SIGN)"
};
UINT8 *pbMathReg[8] =
{
"ch",
"cl",
"byte [_z80de + 1]",
"byte [_z80de]",
"bh",
"bl",
"INVALID",
"al"
};
UINT8 *pbMathRegC[8] =
{
"cpu.z80B",
"cpu.z80C",
"cpu.z80D",
"cpu.z80E",
"cpu.z80H",
"cpu.z80L",
"bTemp",
"cpu.z80A"
};
UINT8 *pbRegPairs[4] =
{
"cx", // BC
"word [_z80de]", // DE
"bx", // HL
"word [_z80sp]" // SP
};
UINT8 *pbRegPairsC[4] =
{
"cpu.z80BC", // BC
"cpu.z80DE", // DE
"cpu.z80HL", // HL
"cpu.z80sp" // SP
};
UINT8 *pbPopRegPairs[4] =
{
"cx", // BC
"word [_z80de]", // DE
"bx", // HL
"ax" // SP
};
UINT8 *pbPopRegPairC[4] =
{
"cpu.z80BC",
"cpu.z80DE",
"cpu.z80HL",
"cpu.z80AF"
};
UINT8 *pbIndexedRegPairs[4] =
{
"cx", // BC
"word [_z80de]", // DE
"di", // IX/IY
"word [_z80sp]" // SP
};
// Timing tables
UINT8 bTimingRegular[0x100] =
{
0x04, 0x0a, 0x07, 0x06, 0x04, 0x04, 0x07, 0x04, 0x04, 0x0b, 0x07, 0x06, 0x04, 0x04, 0x07, 0x04,
0x08, 0x0a, 0x07, 0x06, 0x04, 0x04, 0x07, 0x04, 0x0c, 0x0b, 0x07, 0x06, 0x04, 0x04, 0x07, 0x04,
0x07, 0x0a, 0x10, 0x06, 0x04, 0x04, 0x07, 0x04, 0x07, 0x0b, 0x10, 0x06, 0x04, 0x04, 0x07, 0x04,
0x07, 0x0a, 0x0d, 0x06, 0x0b, 0x0b, 0x0a, 0x04, 0x07, 0x0b, 0x0d, 0x06, 0x04, 0x04, 0x07, 0x04,
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x07, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x07, 0x04,
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x07, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x07, 0x04,
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x07, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x07, 0x04,
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x04, 0x07, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x07, 0x04,
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x07, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x07, 0x04,
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x07, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x07, 0x04,
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x07, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x07, 0x04,
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x07, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x07, 0x04,
0x05, 0x0a, 0x0a, 0x0a, 0x0a, 0x0b, 0x07, 0x0b, 0x05, 0x0a, 0x0a, 0x00, 0x0a, 0x11, 0x07, 0x0b,
0x05, 0x0a, 0x0a, 0x0b, 0x0a, 0x0b, 0x07, 0x0b, 0x05, 0x04, 0x0a, 0x0b, 0x0a, 0x00, 0x07, 0x0b,
0x05, 0x0a, 0x0a, 0x13, 0x0a, 0x0b, 0x07, 0x0b, 0x05, 0x04, 0x0a, 0x04, 0x0a, 0x00, 0x07, 0x0b,
0x05, 0x0a, 0x0a, 0x04, 0x0a, 0x0b, 0x07, 0x0b, 0x05, 0x06, 0x0a, 0x04, 0x0a, 0x00, 0x07, 0x0b
};
UINT8 bTimingCB[0x100] =
{
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0f, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0f, 0x08,
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0f, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0f, 0x08,
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0f, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0f, 0x08,
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0f, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0f, 0x08,
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0c, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0c, 0x08,
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0c, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0c, 0x08,
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0c, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0c, 0x08,
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0c, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0c, 0x08,
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0f, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0f, 0x08,
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0f, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0f, 0x08,
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0f, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0f, 0x08,
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0f, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0f, 0x08,
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0f, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0f, 0x08,
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0f, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0f, 0x08,
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0f, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0f, 0x08,
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0f, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0f, 0x08
};
UINT8 bTimingXXCB[0x100] =
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x00,
0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14,
0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14,
0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14,
0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x00
};
UINT8 bTimingDDFD[0x100] =
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x0e, 0x14, 0x0a, 0x09, 0x09, 0x09, 0x00, 0x00, 0x0f, 0x14, 0x0a, 0x09, 0x09, 0x09, 0x00,
0x00, 0x00, 0x00, 0x00, 0x17, 0x17, 0x13, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x09, 0x09, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x09, 0x13, 0x00,
0x00, 0x00, 0x00, 0x00, 0x09, 0x09, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x09, 0x13, 0x00,
0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x13, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x13, 0x09,
0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x00, 0x00, 0x00, 0x00, 0x09, 0x09, 0x13, 0x00,
0x00, 0x00, 0x00, 0x00, 0x09, 0x09, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x09, 0x13, 0x00,
0x00, 0x00, 0x00, 0x00, 0x09, 0x09, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x09, 0x13, 0x00,
0x00, 0x00, 0x00, 0x00, 0x09, 0x09, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x09, 0x13, 0x00,
0x00, 0x00, 0x00, 0x00, 0x09, 0x09, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x09, 0x13, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x0e, 0x00, 0x17, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
UINT8 bTimingED[0x100] =
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x0c, 0x0c, 0x0f, 0x14, 0x08, 0x0e, 0x08, 0x09, 0x0c, 0x0c, 0x0f, 0x14, 0x08, 0x0e, 0x08, 0x09,
0x0c, 0x0c, 0x0f, 0x14, 0x08, 0x08, 0x08, 0x09, 0x0c, 0x0c, 0x0f, 0x14, 0x08, 0x08, 0x08, 0x09,
0x0c, 0x0c, 0x0f, 0x14, 0x08, 0x08, 0x08, 0x12, 0x0c, 0x0c, 0x0f, 0x14, 0x08, 0x08, 0x08, 0x12,
0x0c, 0x0c, 0x0f, 0x14, 0x08, 0x08, 0x08, 0x00, 0x0c, 0x0c, 0x0f, 0x14, 0x08, 0x08, 0x08, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00,
0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
void EDHandler(UINT32 dwOpcode);
void DDHandler(UINT32 dwOpcode);
void FDHandler(UINT32 dwOpcode);
void CBHandler(UINT32 dwOpcode);
void PushPopOperations(UINT32 dwOpcode);
void AddRegpairOperations(UINT32 dwOpcode);
void CallHandler(UINT32 dwOpcode);
void MiscHandler(UINT32 dwOpcode);
void IMHandler(UINT32 dwOpcode);
void IRHandler(UINT32 dwOpcode);
void LdRegPairImmediate(UINT32 dwOpcode);
void LoadImmediate(UINT32 dwOpcode);
void LdRegpairPtrByte(UINT32 dwOpcode);
void MathOperation(UINT32 dwOpcode);
void RegIntoMemory(UINT32 dwOpcode);
void JpHandler(UINT32 dwOpcode);
void LdRegImmediate(UINT32 dwOpcode);
void IncRegister(UINT32 dwOpcode);
void DecRegister(UINT32 dwOpcode);
void IncDecRegpair(UINT32 dwOpcode);
void LdRegReg(UINT32 dwOpcode);
void MathOperationDirect(UINT32 dwOpcode);
void JrHandler(UINT32 dwOpcode);
void RetHandler(UINT32 dwOpcode);
void RestartHandler(UINT32 dwOpcode);
void ToRegFromHl(UINT32);
void RraRlaHandler(UINT32);
void LdByteRegpair(UINT32);
void IncDecHLPtr(UINT32 dwOpcode);
void InOutHandler(UINT32 dwOpcode);
void RLCRRCRLRRSLASRASRLHandler(UINT32 dwOpcode);
void BITHandler(UINT32 dwOpcode);
void RESSETHandler(UINT32 dwOpcode);
void PushPopOperationsIndexed(UINT32 dwOpcode);
void LDILDRLDIRLDDRHandler(UINT32);
void LdRegpair(UINT32 dwOpcode);
void ExtendedRegIntoMemory(UINT32 dwOpcode);
void NegHandler(UINT32 dwOpcode);
void ExtendedInHandler(UINT32 dwOpcode);
void ExtendedOutHandler(UINT32 dwOpcode);
void RetIRetNHandler(UINT32 dwOcode);
void AdcSbcRegpair(UINT32 dwOpcode);
void CPICPDCPIRCPDRHandler(UINT32 dwOpcode);
void RRDRLDHandler(UINT32 dwOpcode);
void UndocRegToIndex(UINT32 dwOpcode);
void UndocIndexToReg(UINT32 dwOpcode);
void MathOperationIndexed(UINT32 dwOpcode);
void IncDecIndexed(UINT32 dwOpcode);
void DDFDCBHandler(UINT32 dwOpcode);
void JPIXIYHandler(UINT32 dwOpcode);
void AddIndexHandler(UINT32 dwOpcode);
void SPToIndex(UINT32 dwOpcode);
void LdByteToIndex(UINT32 dwOpcode);
void LdRegIndexOffset(UINT32 dwOpcode);
void IncDecIndexReg(UINT32 dwOpcode);
void ExIndexed(UINT32 dwOpcode);
void UndocIncDecIndexReg(UINT32 dwOpcode);
void UndocLoadHalfIndexReg(UINT32 dwOpcode);
void UndocMathIndex(UINT32 dwOpcode);
void ddcbBitWise(UINT32 dwOpcode);
void LdIndexPtrReg(UINT32 dwOpcode);
void StoreIndexReg(UINT32 dwOpcode);
void LoadIndexReg(UINT32 dwOpcode);
void OTIROTDROUTIOUTDHandler(UINT32 dwOpcode);
void INIRINDRINIINDHandler(UINT32 dwOpcode);
struct sOp
{
UINT32 bOpCode;
void (*Emitter)(UINT32);
};
struct sOp StandardOps[] =
{
{0xd3, InOutHandler}, // V
{0xdb, InOutHandler}, // V
{0x0a, LdByteRegpair}, // V
{0x1a, LdByteRegpair}, // V
{0x17, RraRlaHandler}, // V
{0x1f, RraRlaHandler}, // V
{0x05, DecRegister}, // V
{0x0d, DecRegister}, // V
{0x15, DecRegister}, // V
{0x1d, DecRegister}, // V
{0x25, DecRegister}, // V
{0x2d, DecRegister}, // V
{0x3d, DecRegister}, // V
{0x04, IncRegister}, // V
{0x0c, IncRegister}, // V
{0x14, IncRegister}, // V
{0x1c, IncRegister}, // V
{0x24, IncRegister}, // V
{0x2c, IncRegister}, // V
{0x3c, IncRegister}, // V
{0x32, RegIntoMemory}, // V
{0x22, RegIntoMemory}, // V
{0xc3, JpHandler}, // V
{0xc2, JpHandler}, // V
{0xca, JpHandler}, // V
{0xd2, JpHandler}, // V
{0xda, JpHandler}, // V
{0xe2, JpHandler}, // V
{0xea, JpHandler}, // V
{0xf2, JpHandler}, // V
{0xfa, JpHandler}, // V
{0x06, LdRegImmediate}, // V
{0x0e, LdRegImmediate}, // V
{0x16, LdRegImmediate}, // V
{0x1e, LdRegImmediate}, // V
{0x26, LdRegImmediate}, // V
{0x2e, LdRegImmediate}, // V
{0x3e, LdRegImmediate}, // V
{0x0b, IncDecRegpair}, // V
{0x1b, IncDecRegpair}, // V
{0x2b, IncDecRegpair}, // V
{0x3b, IncDecRegpair}, // V
{0x03, IncDecRegpair}, // V
{0x13, IncDecRegpair}, // V
{0x23, IncDecRegpair}, // V
{0x33, IncDecRegpair}, // V
{0x34, IncDecHLPtr}, // V
{0x35, IncDecHLPtr}, // V
{0xcb, CBHandler},
{0xdd, DDHandler},
{0xed, EDHandler},
{0xfd, FDHandler},
{0x01, LdRegPairImmediate}, // V
{0x11, LdRegPairImmediate}, // V
{0x21, LdRegPairImmediate}, // V
{0x31, LdRegPairImmediate}, // V
{0xe3, MiscHandler}, // V
{0x2a, MiscHandler}, // V
{0xfb, MiscHandler}, // V
{0xf9, MiscHandler}, // V
{0xd9, MiscHandler}, // V
{0x76, MiscHandler}, // V
{0x3f, MiscHandler}, // V
{0x37, MiscHandler}, // V
{0x27, MiscHandler}, // V
{0x07, MiscHandler}, // V
{0x08, MiscHandler}, // V
{0x00, MiscHandler}, // V
{0xe9, MiscHandler}, // V
{0xeb, MiscHandler}, // V
{0xf3, MiscHandler}, // V
{0x3a, MiscHandler}, // V
{0x10, MiscHandler}, // V
{0x2f, MiscHandler}, // V
{0x0f, MiscHandler}, // V
{0x02, LdRegpairPtrByte}, // V
{0x12, LdRegpairPtrByte}, // V
{0x70, LdRegpairPtrByte}, // V
{0x71, LdRegpairPtrByte}, // V
{0x72, LdRegpairPtrByte}, // V
{0x73, LdRegpairPtrByte}, // V
{0x74, LdRegpairPtrByte}, // V
{0x75, LdRegpairPtrByte}, // V
{0x77, LdRegpairPtrByte}, // V
{0x36, LdRegpairPtrByte}, // V
{0x80, MathOperation}, // V
{0x81, MathOperation}, // V
{0x82, MathOperation}, // V
{0x83, MathOperation}, // V
{0x84, MathOperation}, // V
{0x85, MathOperation}, // V
{0x86, MathOperation}, // V
{0x87, MathOperation}, // V
{0x88, MathOperation}, // V
{0x89, MathOperation}, // V
{0x8a, MathOperation}, // V
{0x8b, MathOperation}, // V
{0x8c, MathOperation}, // V
{0x8d, MathOperation}, // V
{0x8e, MathOperation}, // V
{0x8f, MathOperation}, // V
{0x90, MathOperation}, // V
{0x91, MathOperation}, // V
{0x92, MathOperation}, // V
{0x93, MathOperation}, // V
{0x94, MathOperation}, // V
{0x95, MathOperation}, // V
{0x96, MathOperation}, // V
{0x97, MathOperation}, // V
{0x98, MathOperation}, // V
{0x99, MathOperation}, // V
{0x9a, MathOperation}, // V
{0x9b, MathOperation}, // V
{0x9c, MathOperation}, // V
{0x9d, MathOperation}, // V
{0x9e, MathOperation}, // V
{0x9f, MathOperation}, // V
{0xa0, MathOperation}, // V
{0xa1, MathOperation}, // V
{0xa2, MathOperation}, // V
{0xa3, MathOperation}, // V
{0xa4, MathOperation}, // V
{0xa5, MathOperation}, // V
{0xa6, MathOperation}, // V
{0xa7, MathOperation}, // V
{0xa8, MathOperation}, // V
{0xa9, MathOperation}, // V
{0xaa, MathOperation}, // V
{0xab, MathOperation}, // V
{0xac, MathOperation}, // V
{0xad, MathOperation}, // V
{0xae, MathOperation}, // V
{0xaf, MathOperation}, // V
{0xb0, MathOperation}, // V
{0xb1, MathOperation}, // V
{0xb2, MathOperation}, // V
{0xb3, MathOperation}, // V
{0xb4, MathOperation}, // V
{0xb5, MathOperation}, // V
{0xb6, MathOperation}, // V
{0xb7, MathOperation}, // V
{0xb8, MathOperation}, // V
{0xb9, MathOperation}, // V
{0xba, MathOperation}, // V
{0xbb, MathOperation}, // V
{0xbc, MathOperation}, // V
{0xbd, MathOperation}, // V
{0xbe, MathOperation}, // V
{0xbf, MathOperation}, // V
{0x40, LdRegReg}, // V
{0x41, LdRegReg}, // V
{0x42, LdRegReg}, // V
{0x43, LdRegReg}, // V
{0x44, LdRegReg}, // V
{0x45, LdRegReg}, // V
{0x47, LdRegReg}, // V
{0x48, LdRegReg}, // V
{0x49, LdRegReg}, // V
{0x4a, LdRegReg}, // V
{0x4b, LdRegReg}, // V
{0x4c, LdRegReg}, // V
{0x4d, LdRegReg}, // V
{0x4f, LdRegReg}, // V
{0x50, LdRegReg}, // V
{0x51, LdRegReg}, // V
{0x52, LdRegReg}, // V
{0x53, LdRegReg}, // V
{0x54, LdRegReg}, // V
{0x55, LdRegReg}, // V
{0x57, LdRegReg}, // V
{0x58, LdRegReg}, // V
{0x59, LdRegReg}, // V
{0x5a, LdRegReg}, // V
{0x5b, LdRegReg}, // V
{0x5c, LdRegReg}, // V
{0x5d, LdRegReg}, // V
{0x5f, LdRegReg}, // V
{0x60, LdRegReg}, // V
{0x61, LdRegReg}, // V
{0x62, LdRegReg}, // V
{0x63, LdRegReg}, // V
{0x64, LdRegReg}, // V
{0x65, LdRegReg}, // V
{0x67, LdRegReg}, // V
{0x68, LdRegReg}, // V
{0x69, LdRegReg}, // V
{0x6a, LdRegReg}, // V
{0x6b, LdRegReg}, // V
{0x6c, LdRegReg}, // V
{0x6d, LdRegReg}, // V
{0x6f, LdRegReg}, // V
{0x78, LdRegReg}, // V
{0x79, LdRegReg}, // V
{0x7a, LdRegReg}, // V
{0x7b, LdRegReg}, // V
{0x7c, LdRegReg}, // V
{0x7d, LdRegReg}, // V
{0x7f, LdRegReg}, // V
{0xc6, MathOperationDirect}, // V
{0xce, MathOperationDirect}, // V
{0xd6, MathOperationDirect}, // V
{0xde, MathOperationDirect}, // V
{0xe6, MathOperationDirect}, // V
{0xee, MathOperationDirect}, // V
{0xf6, MathOperationDirect}, // V
{0xfe, MathOperationDirect}, // V
{0x18, JrHandler}, // V
{0x20, JrHandler}, // V
{0x28, JrHandler}, // V
{0x30, JrHandler}, // V
{0x38, JrHandler},
{0xc4, CallHandler}, // V
{0xcc, CallHandler}, // V
{0xcd, CallHandler}, // V
{0xd4, CallHandler}, // V
{0xdc, CallHandler}, // V
{0xe4, CallHandler}, // V
{0xec, CallHandler}, // V
{0xf4, CallHandler}, // V
{0xfc, CallHandler}, // V
{0xc9, RetHandler}, // V
{0xc0, RetHandler}, // V
{0xc8, RetHandler}, // V
{0xd0, RetHandler}, // V
{0xd8, RetHandler}, // V
{0xe0, RetHandler}, // V
{0xe8, RetHandler}, // V
{0xf0, RetHandler}, // V
{0xf8, RetHandler}, // V
{0xc7, RestartHandler}, // V
{0xcf, RestartHandler}, // V
{0xd7, RestartHandler}, // V
{0xdf, RestartHandler}, // V
{0xe7, RestartHandler}, // V
{0xef, RestartHandler}, // V
{0xf7, RestartHandler}, // V
{0xff, RestartHandler}, // V
{0x46, ToRegFromHl}, // V
{0x4e, ToRegFromHl}, // V
{0x56, ToRegFromHl}, // V
{0x5e, ToRegFromHl}, // V
{0x66, ToRegFromHl}, // V
{0x6e, ToRegFromHl}, // V
{0x7e, ToRegFromHl},
{0x09, AddRegpairOperations}, // V
{0x19, AddRegpairOperations}, // V
{0x29, AddRegpairOperations}, // V
{0x39, AddRegpairOperations}, // V
{0xc5, PushPopOperations}, // V
{0xd5, PushPopOperations}, // V
{0xe5, PushPopOperations}, // V
{0xf5, PushPopOperations}, // V
{0xc1, PushPopOperations}, // V
{0xd1, PushPopOperations}, // V
{0xe1, PushPopOperations}, // V
{0xf1, PushPopOperations}, // V
// Terminator
{0xffffffff, NULL}
};
struct sOp CBOps[] =
{
{0x00, RLCRRCRLRRSLASRASRLHandler},
{0x01, RLCRRCRLRRSLASRASRLHandler},
{0x02, RLCRRCRLRRSLASRASRLHandler},
{0x03, RLCRRCRLRRSLASRASRLHandler},
{0x04, RLCRRCRLRRSLASRASRLHandler},
{0x05, RLCRRCRLRRSLASRASRLHandler},
{0x06, RLCRRCRLRRSLASRASRLHandler},
{0x07, RLCRRCRLRRSLASRASRLHandler},
{0x08, RLCRRCRLRRSLASRASRLHandler},
{0x09, RLCRRCRLRRSLASRASRLHandler},
{0x0a, RLCRRCRLRRSLASRASRLHandler},
{0x0b, RLCRRCRLRRSLASRASRLHandler},
{0x0c, RLCRRCRLRRSLASRASRLHandler},
{0x0d, RLCRRCRLRRSLASRASRLHandler},
{0x0e, RLCRRCRLRRSLASRASRLHandler},
{0x0f, RLCRRCRLRRSLASRASRLHandler},
{0x10, RLCRRCRLRRSLASRASRLHandler},
{0x11, RLCRRCRLRRSLASRASRLHandler},
{0x12, RLCRRCRLRRSLASRASRLHandler},
{0x13, RLCRRCRLRRSLASRASRLHandler},
{0x14, RLCRRCRLRRSLASRASRLHandler},
{0x15, RLCRRCRLRRSLASRASRLHandler},
{0x16, RLCRRCRLRRSLASRASRLHandler},
{0x17, RLCRRCRLRRSLASRASRLHandler},
{0x18, RLCRRCRLRRSLASRASRLHandler},
{0x19, RLCRRCRLRRSLASRASRLHandler},
{0x1a, RLCRRCRLRRSLASRASRLHandler},
{0x1b, RLCRRCRLRRSLASRASRLHandler},
{0x1c, RLCRRCRLRRSLASRASRLHandler},
{0x1d, RLCRRCRLRRSLASRASRLHandler},
{0x1e, RLCRRCRLRRSLASRASRLHandler},
{0x1f, RLCRRCRLRRSLASRASRLHandler},
{0x20, RLCRRCRLRRSLASRASRLHandler},
{0x21, RLCRRCRLRRSLASRASRLHandler},
{0x22, RLCRRCRLRRSLASRASRLHandler},
{0x23, RLCRRCRLRRSLASRASRLHandler},
{0x24, RLCRRCRLRRSLASRASRLHandler},
{0x25, RLCRRCRLRRSLASRASRLHandler},
{0x26, RLCRRCRLRRSLASRASRLHandler},
{0x27, RLCRRCRLRRSLASRASRLHandler},
{0x28, RLCRRCRLRRSLASRASRLHandler},
{0x29, RLCRRCRLRRSLASRASRLHandler},
{0x2a, RLCRRCRLRRSLASRASRLHandler},
{0x2b, RLCRRCRLRRSLASRASRLHandler},
{0x2c, RLCRRCRLRRSLASRASRLHandler},
{0x2d, RLCRRCRLRRSLASRASRLHandler},
{0x2e, RLCRRCRLRRSLASRASRLHandler},
{0x2f, RLCRRCRLRRSLASRASRLHandler},
{0x30, RLCRRCRLRRSLASRASRLHandler},
{0x31, RLCRRCRLRRSLASRASRLHandler},
{0x32, RLCRRCRLRRSLASRASRLHandler},
{0x33, RLCRRCRLRRSLASRASRLHandler},
{0x34, RLCRRCRLRRSLASRASRLHandler},
{0x35, RLCRRCRLRRSLASRASRLHandler},
{0x36, RLCRRCRLRRSLASRASRLHandler},
{0x37, RLCRRCRLRRSLASRASRLHandler},
{0x38, RLCRRCRLRRSLASRASRLHandler},
{0x39, RLCRRCRLRRSLASRASRLHandler},
{0x3a, RLCRRCRLRRSLASRASRLHandler},
{0x3b, RLCRRCRLRRSLASRASRLHandler},
{0x3c, RLCRRCRLRRSLASRASRLHandler},
{0x3d, RLCRRCRLRRSLASRASRLHandler},
{0x3e, RLCRRCRLRRSLASRASRLHandler},
{0x3f, RLCRRCRLRRSLASRASRLHandler},
{0x40, BITHandler},
{0x41, BITHandler},
{0x42, BITHandler},
{0x43, BITHandler},
{0x44, BITHandler},
{0x45, BITHandler},
{0x46, BITHandler},
{0x47, BITHandler},
{0x48, BITHandler},
{0x49, BITHandler},
{0x4a, BITHandler},
{0x4b, BITHandler},
{0x4c, BITHandler},
{0x4d, BITHandler},
{0x4e, BITHandler},
{0x4f, BITHandler},
{0x50, BITHandler},
{0x51, BITHandler},
{0x52, BITHandler},
{0x53, BITHandler},
{0x54, BITHandler},
{0x55, BITHandler},
{0x56, BITHandler},
{0x57, BITHandler},
{0x58, BITHandler},
{0x59, BITHandler},
{0x5a, BITHandler},
{0x5b, BITHandler},
{0x5c, BITHandler},
{0x5d, BITHandler},
{0x5e, BITHandler},
{0x5f, BITHandler},
{0x60, BITHandler},
{0x61, BITHandler},
{0x62, BITHandler},
{0x63, BITHandler},
{0x64, BITHandler},
{0x65, BITHandler},
{0x66, BITHandler},
{0x67, BITHandler},
{0x68, BITHandler},
{0x69, BITHandler},
{0x6a, BITHandler},
{0x6b, BITHandler},
{0x6c, BITHandler},
{0x6d, BITHandler},
{0x6e, BITHandler},
{0x6f, BITHandler},
{0x70, BITHandler},
{0x71, BITHandler},
{0x72, BITHandler},
{0x73, BITHandler},
{0x74, BITHandler},
{0x75, BITHandler},
{0x76, BITHandler},
{0x77, BITHandler},
{0x78, BITHandler},
{0x79, BITHandler},
{0x7a, BITHandler},
{0x7b, BITHandler},
{0x7c, BITHandler},
{0x7d, BITHandler},
{0x7e, BITHandler},
{0x7f, BITHandler},
// RES
{0x80, RESSETHandler},
{0x81, RESSETHandler},
{0x82, RESSETHandler},
{0x83, RESSETHandler},
{0x84, RESSETHandler},
{0x85, RESSETHandler},
{0x86, RESSETHandler},
{0x87, RESSETHandler},
{0x88, RESSETHandler},
{0x89, RESSETHandler},
{0x8a, RESSETHandler},
{0x8b, RESSETHandler},
{0x8c, RESSETHandler},
{0x8d, RESSETHandler},
{0x8e, RESSETHandler},
{0x8f, RESSETHandler},
{0x90, RESSETHandler},
{0x91, RESSETHandler},
{0x92, RESSETHandler},
{0x93, RESSETHandler},
{0x94, RESSETHandler},
{0x95, RESSETHandler},
{0x96, RESSETHandler},
{0x97, RESSETHandler},
{0x98, RESSETHandler},
{0x99, RESSETHandler},
{0x9a, RESSETHandler},
{0x9b, RESSETHandler},
{0x9c, RESSETHandler},
{0x9d, RESSETHandler},
{0x9e, RESSETHandler},
{0x9f, RESSETHandler},
{0xa0, RESSETHandler},
{0xa1, RESSETHandler},
{0xa2, RESSETHandler},
{0xa3, RESSETHandler},
{0xa4, RESSETHandler},
{0xa5, RESSETHandler},
{0xa6, RESSETHandler},
{0xa7, RESSETHandler},
{0xa8, RESSETHandler},
{0xa9, RESSETHandler},
{0xaa, RESSETHandler},
{0xab, RESSETHandler},
{0xac, RESSETHandler},
{0xad, RESSETHandler},
{0xae, RESSETHandler},
{0xaf, RESSETHandler},
{0xb0, RESSETHandler},
{0xb1, RESSETHandler},
{0xb2, RESSETHandler},
{0xb3, RESSETHandler},
{0xb4, RESSETHandler},
{0xb5, RESSETHandler},
{0xb6, RESSETHandler},
{0xb7, RESSETHandler},
{0xb8, RESSETHandler},
{0xb9, RESSETHandler},
{0xba, RESSETHandler},
{0xbb, RESSETHandler},
{0xbc, RESSETHandler},
{0xbd, RESSETHandler},
{0xbe, RESSETHandler},
{0xbf, RESSETHandler},
// SET
{0xc0, RESSETHandler},
{0xc1, RESSETHandler},
{0xc2, RESSETHandler},
{0xc3, RESSETHandler},
{0xc4, RESSETHandler},
{0xc5, RESSETHandler},
{0xc6, RESSETHandler},
{0xc7, RESSETHandler},
{0xc8, RESSETHandler},
{0xc9, RESSETHandler},
{0xca, RESSETHandler},
{0xcb, RESSETHandler},
{0xcc, RESSETHandler},
{0xcd, RESSETHandler},
{0xce, RESSETHandler},
{0xcf, RESSETHandler},
{0xd0, RESSETHandler},
{0xd1, RESSETHandler},
{0xd2, RESSETHandler},
{0xd3, RESSETHandler},
{0xd4, RESSETHandler},
{0xd5, RESSETHandler},
{0xd6, RESSETHandler},
{0xd7, RESSETHandler},
{0xd8, RESSETHandler},
{0xd9, RESSETHandler},
{0xda, RESSETHandler},
{0xdb, RESSETHandler},
{0xdc, RESSETHandler},
{0xdd, RESSETHandler},
{0xde, RESSETHandler},
{0xdf, RESSETHandler},
{0xe0, RESSETHandler},
{0xe1, RESSETHandler},
{0xe2, RESSETHandler},
{0xe3, RESSETHandler},
{0xe4, RESSETHandler},
{0xe5, RESSETHandler},
{0xe6, RESSETHandler},
{0xe7, RESSETHandler},
{0xe8, RESSETHandler},
{0xe9, RESSETHandler},
{0xea, RESSETHandler},
{0xeb, RESSETHandler},
{0xec, RESSETHandler},
{0xed, RESSETHandler},
{0xee, RESSETHandler},
{0xef, RESSETHandler},
{0xf0, RESSETHandler},
{0xf1, RESSETHandler},
{0xf2, RESSETHandler},
{0xf3, RESSETHandler},
{0xf4, RESSETHandler},
{0xf5, RESSETHandler},
{0xf6, RESSETHandler},
{0xf7, RESSETHandler},
{0xf8, RESSETHandler},
{0xf9, RESSETHandler},
{0xfa, RESSETHandler},
{0xfb, RESSETHandler},
{0xfc, RESSETHandler},
{0xfd, RESSETHandler},
{0xfe, RESSETHandler},
{0xff, RESSETHandler},
// Terminator
{0xffffffff, NULL}
};
struct sOp EDOps[] =
{
{0x67, RRDRLDHandler},
{0x6f, RRDRLDHandler},
{0x42, AdcSbcRegpair},
{0x4a, AdcSbcRegpair},
{0x52, AdcSbcRegpair},
{0x5a, AdcSbcRegpair},
{0x62, AdcSbcRegpair},
{0x6a, AdcSbcRegpair},
{0x72, AdcSbcRegpair},
{0x7a, AdcSbcRegpair},
{0x45, RetIRetNHandler},
{0x4d, RetIRetNHandler},
{0x44, NegHandler},
{0xa0, LDILDRLDIRLDDRHandler},
{0xa8, LDILDRLDIRLDDRHandler},
{0xb0, LDILDRLDIRLDDRHandler},
{0xb8, LDILDRLDIRLDDRHandler},
{0x57, IRHandler},
{0x5F, IRHandler},
{0x47, IRHandler},
{0x4F, IRHandler},
{0x46, IMHandler},
{0x56, IMHandler},
{0x5e, IMHandler},
{0x4b, LdRegpair},
{0x5b, LdRegpair},
{0x7b, LdRegpair},
{0x43, ExtendedRegIntoMemory},
{0x53, ExtendedRegIntoMemory},
{0x63, ExtendedRegIntoMemory},
{0x73, ExtendedRegIntoMemory},
{0x40, ExtendedInHandler},
{0x48, ExtendedInHandler},
{0x50, ExtendedInHandler},
{0x58, ExtendedInHandler},
{0x60, ExtendedInHandler},
{0x68, ExtendedInHandler},
{0x78, ExtendedInHandler},
{0x41, ExtendedOutHandler},
{0x49, ExtendedOutHandler},
{0x51, ExtendedOutHandler},
{0x59, ExtendedOutHandler},
{0x61, ExtendedOutHandler},
{0x69, ExtendedOutHandler},
{0x79, ExtendedOutHandler},
{0xa1, CPICPDCPIRCPDRHandler},
{0xa9, CPICPDCPIRCPDRHandler},
{0xb1, CPICPDCPIRCPDRHandler},
{0xb9, CPICPDCPIRCPDRHandler},
{0xbb, OTIROTDROUTIOUTDHandler}, // OTDR
{0xb3, OTIROTDROUTIOUTDHandler}, // OTIR
{0xab, OTIROTDROUTIOUTDHandler}, // OUTD
{0xa3, OTIROTDROUTIOUTDHandler}, // OUTI
{0xb2, INIRINDRINIINDHandler}, // INIR
{0xba, INIRINDRINIINDHandler}, // INDR
{0xa2, INIRINDRINIINDHandler}, // INI
{0xaa, INIRINDRINIINDHandler}, // IND
// Terminator
{0xffffffff, NULL}
};
struct sOp DDFDOps[] =
{
{0x35, IncDecIndexed},
{0x34, IncDecIndexed},
{0xcb, DDFDCBHandler},
{0x86, MathOperationIndexed},
{0x8e, MathOperationIndexed},
{0x96, MathOperationIndexed},
{0x9e, MathOperationIndexed},
{0xa6, MathOperationIndexed},
{0xae, MathOperationIndexed},
{0xb6, MathOperationIndexed},
{0xbe, MathOperationIndexed},
{0xe1, PushPopOperationsIndexed},
{0xe5, PushPopOperationsIndexed},
{0x21, LoadImmediate},
{0xe9, JPIXIYHandler},
{0x09, AddIndexHandler},
{0x19, AddIndexHandler},
{0x29, AddIndexHandler},
{0x39, AddIndexHandler},
{0xf9, SPToIndex},
{0x36, LdByteToIndex},
{0x46, LdRegIndexOffset},
{0x4e, LdRegIndexOffset},
{0x56, LdRegIndexOffset},
{0x5e, LdRegIndexOffset},
{0x66, LdRegIndexOffset},
{0x6e, LdRegIndexOffset},
{0x7e, LdRegIndexOffset},
{0x70, LdIndexPtrReg},
{0x71, LdIndexPtrReg},
{0x72, LdIndexPtrReg},
{0x73, LdIndexPtrReg},
{0x74, LdIndexPtrReg},
{0x75, LdIndexPtrReg},
{0x77, LdIndexPtrReg},
{0x23, IncDecIndexReg},
{0x2b, IncDecIndexReg},
{0x22, StoreIndexReg},
{0x2a, LoadIndexReg},
{0xe3, ExIndexed},
{0x44, UndocRegToIndex},
{0x45, UndocRegToIndex},
{0x4c, UndocRegToIndex},
{0x4d, UndocRegToIndex},
{0x54, UndocRegToIndex},
{0x55, UndocRegToIndex},
{0x5c, UndocRegToIndex},
{0x5d, UndocRegToIndex},
{0x7c, UndocRegToIndex},
{0x7d, UndocRegToIndex},
{0x60, UndocIndexToReg},
{0x61, UndocIndexToReg},
{0x62, UndocIndexToReg},
{0x63, UndocIndexToReg},
{0x64, UndocIndexToReg},
{0x65, UndocIndexToReg},
{0x67, UndocIndexToReg},
{0x68, UndocIndexToReg},
{0x69, UndocIndexToReg},
{0x6a, UndocIndexToReg},
{0x6b, UndocIndexToReg},
{0x6c, UndocIndexToReg},
{0x6d, UndocIndexToReg},
{0x6f, UndocIndexToReg},
{0x24, UndocIncDecIndexReg},
{0x25, UndocIncDecIndexReg},
{0x2c, UndocIncDecIndexReg},
{0x2d, UndocIncDecIndexReg},
{0x26, UndocLoadHalfIndexReg},
{0x2e, UndocLoadHalfIndexReg},
{0x84, UndocMathIndex},
{0x85, UndocMathIndex},
{0x8c, UndocMathIndex},
{0x8d, UndocMathIndex},
{0x94, UndocMathIndex},
{0x95, UndocMathIndex},
{0x9c, UndocMathIndex},
{0x9d, UndocMathIndex},
{0xa4, UndocMathIndex},
{0xa5, UndocMathIndex},
{0xac, UndocMathIndex},
{0xad, UndocMathIndex},
{0xb4, UndocMathIndex},
{0xb5, UndocMathIndex},
{0xbc, UndocMathIndex},
{0xbd, UndocMathIndex},
// Terminator
{0xffffffff, NULL}
};
struct sOp DDFDCBOps[] =
{
{0x06, ddcbBitWise},
{0x0e, ddcbBitWise},
{0x16, ddcbBitWise},
{0x1e, ddcbBitWise},
{0x26, ddcbBitWise},
{0x2e, ddcbBitWise},
{0x3e, ddcbBitWise},
{0x46, ddcbBitWise},
{0x4e, ddcbBitWise},
{0x56, ddcbBitWise},
{0x5e, ddcbBitWise},
{0x66, ddcbBitWise},
{0x6e, ddcbBitWise},
{0x76, ddcbBitWise},
{0x7e, ddcbBitWise},
{0x86, ddcbBitWise},
{0x8e, ddcbBitWise},
{0x96, ddcbBitWise},
{0x9e, ddcbBitWise},
{0xa6, ddcbBitWise},
{0xae, ddcbBitWise},
{0xb6, ddcbBitWise},
{0xbe, ddcbBitWise},
{0xc6, ddcbBitWise},
{0xce, ddcbBitWise},
{0xd6, ddcbBitWise},
{0xde, ddcbBitWise},
{0xe6, ddcbBitWise},
{0xee, ddcbBitWise},
{0xf6, ddcbBitWise},
{0xfe, ddcbBitWise},
// Terminator
{0xffffffff, NULL}
};
void InvalidInstructionC(UINT32 dwCount)
{
fprintf(fp, " InvalidInstruction(%ld);\n", dwCount);
}
UINT32 Timing(UINT8 bWho, UINT32 dwOpcode)
{
UINT32 dwTiming = 0;
assert(dwOpcode < 0x100);
if (TIMING_REGULAR == bWho) // Regular?
dwTiming = bTimingRegular[dwOpcode];
else
if (TIMING_CB == bWho)
dwTiming = bTimingCB[dwOpcode];
else
if (TIMING_DDFD == bWho)
dwTiming = bTimingDDFD[dwOpcode];
else
if (TIMING_ED == bWho)
dwTiming = bTimingED[dwOpcode];
else
if (TIMING_XXCB == bWho)
dwTiming = bTimingXXCB[dwOpcode];
else
if (TIMING_EXCEPT == bWho)
dwTiming = dwOpcode;
else
assert(0);
if (0 == dwTiming)
{
fprintf(stderr, "Opcode: %.2x:%.2x - Not zero!\n", bWho, dwOpcode);
fclose(fp);
exit(1);
}
return(dwTiming);
}
void IndexedOffset(UINT8 *Localmz80Index)
{
fprintf(fp, " mov dl, [esi] ; Fetch our offset\n");
fprintf(fp, " inc esi ; Move past the offset\n");
fprintf(fp, " or dl, dl ; Is this bad boy signed?\n");
fprintf(fp, " jns notSigned%ld ; Nope!\n", dwGlobalLabel);
fprintf(fp, " dec dh ; Make it FFable\n");
fprintf(fp, "notSigned%ld:\n", dwGlobalLabel);
fprintf(fp, " add dx, [_z80%s] ; Our offset!\n", Localmz80Index);
++dwGlobalLabel;
}
void CBHandler(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
fprintf(fp, ";\n");
fprintf(fp, "; Handler for all CBxx instructions\n");
fprintf(fp, ";\n");
sprintf(string, "RegInst%.2x", dwOpcode);
ProcBegin(0xffffffff);
fprintf(fp, " mov dl, [esi]\n");
fprintf(fp, " inc esi\n");
fprintf(fp, " jmp dword [z80PrefixCB+edx*4]\n\n");
fprintf(fp, "\n\n");
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " CBHandler();\n");
}
else
{
assert(0);
}
}
void EDHandler(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
fprintf(fp, ";\n");
fprintf(fp, "; Handler for all EDxx instructions\n");
fprintf(fp, ";\n");
sprintf(string, "RegInst%.2x", dwOpcode);
ProcBegin(0xffffffff);
fprintf(fp, " mov dl, [esi]\n");
fprintf(fp, " inc esi\n");
fprintf(fp, " jmp dword [z80PrefixED+edx*4]\n\n");
fprintf(fp, "\n\n");
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " EDHandler();\n");
}
else
{
assert(0);
}
}
void DDHandler(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
fprintf(fp, ";\n");
fprintf(fp, "; Handler for all DDxx instructions\n");
fprintf(fp, ";\n");
sprintf(string, "RegInst%.2x", dwOpcode);
ProcBegin(0xffffffff);
fprintf(fp, " mov dl, [esi]\n");
fprintf(fp, " inc esi\n");
fprintf(fp, " jmp dword [z80PrefixDD+edx*4]\n\n");
fprintf(fp, "\n\n");
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " DDHandler();\n");
}
else
{
assert(0);
}
}
void FDHandler(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
fprintf(fp, ";\n");
fprintf(fp, "; Handler for all FDxx instructions\n");
fprintf(fp, ";\n");
sprintf(string, "RegInst%.2x", dwOpcode);
ProcBegin(0xffffffff);
fprintf(fp, " mov dl, [esi]\n");
fprintf(fp, " inc esi\n");
fprintf(fp, " jmp dword [z80PrefixFD+edx*4]\n\n");
fprintf(fp, "\n\n");
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " FDHandler();\n");
}
else
{
assert(0);
}
}
StandardHeader()
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
fprintf(fp,"; For assembly by NASM only\n");
fprintf(fp,"bits 32\n\n");
fprintf(fp,"; Theory of operation\n\n");
fprintf(fp,"; EDI=General purpose\n");
fprintf(fp,"; ESI=Program counter + base address\n");
fprintf(fp,"; EBP=z80Base\n");
fprintf(fp,"; AX=AF\n");
fprintf(fp,"; BX=HL\n");
fprintf(fp,"; CX=BC\n");
fprintf(fp,"; DX=General purpose\n\n");
if (bUseStack)
fprintf(fp, "; Using stack calling conventions\n");
else
fprintf(fp, "; Using register calling conventions\n");
if (b16BitIo)
fprintf(fp, "; Extended input/output instructions treat (BC) as I/O address\n");
else
fprintf(fp, "; Extended input/output instructions treat (C) as I/O address\n\n");
fprintf(fp, "IFF1 equ 01h\n");
fprintf(fp, "IFF2 equ 02h\n");
fprintf(fp, "CPUREG_PC equ 00h\n");
fprintf(fp, "CPUREG_SP equ 01h\n");
fprintf(fp, "CPUREG_AF equ 02h\n");
fprintf(fp, "CPUREG_BC equ 03h\n");
fprintf(fp, "CPUREG_DE equ 04h\n");
fprintf(fp, "CPUREG_HL equ 05h\n");
fprintf(fp, "CPUREG_AFPRIME equ 06h\n");
fprintf(fp, "CPUREG_BCPRIME equ 07h\n");
fprintf(fp, "CPUREG_DEPRIME equ 08h\n");
fprintf(fp, "CPUREG_HLPRIME equ 09h\n");
fprintf(fp, "CPUREG_IX equ 0ah\n");
fprintf(fp, "CPUREG_IY equ 0bh\n");
fprintf(fp, "CPUREG_I equ 0ch\n");
fprintf(fp, "CPUREG_A equ 0dh\n");
fprintf(fp, "CPUREG_F equ 0eh\n");
fprintf(fp, "CPUREG_B equ 0fh\n");
fprintf(fp, "CPUREG_C equ 10h\n");
fprintf(fp, "CPUREG_D equ 11h\n");
fprintf(fp, "CPUREG_E equ 12h\n");
fprintf(fp, "CPUREG_H equ 13h\n");
fprintf(fp, "CPUREG_L equ 14h\n");
fprintf(fp, "CPUREG_IFF1 equ 15h\n");
fprintf(fp, "CPUREG_IFF2 equ 16h\n");
fprintf(fp, "CPUREG_CARRY equ 17h\n");
fprintf(fp, "CPUREG_NEGATIVE equ 18h\n");
fprintf(fp, "CPUREG_PARITY equ 19h\n");
fprintf(fp, "CPUREG_OVERFLOW equ 1ah\n");
fprintf(fp, "CPUREG_HALFCARRY equ 1bh\n");
fprintf(fp, "CPUREG_ZERO equ 1ch\n");
fprintf(fp, "CPUREG_SIGN equ 1dh\n");
fprintf(fp, "CPUREG_MAXINDEX equ 1eh\n\n");
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, "/* Multi-Z80 32 Bit emulator */\n");
fprintf(fp, "\n");
fprintf(fp, "/* Copyright 1996-2000 Neil Bradley, All rights reserved\n");
fprintf(fp, " *\n");
fprintf(fp, " * License agreement:\n");
fprintf(fp, " *\n");
fprintf(fp, " * (MZ80 Refers to both the assembly code emitted by makeZ80.c and makeZ80.c\n");
fprintf(fp, " * itself)\n");
fprintf(fp, " *\n");
fprintf(fp, " * MZ80 May be distributed in unmodified form to any medium.\n");
fprintf(fp, " *\n");
fprintf(fp, " * MZ80 May not be sold, or sold as a part of a commercial package without\n");
fprintf(fp, " * the express written permission of Neil Bradley ([email protected]). This\n");
fprintf(fp, " * includes shareware.\n");
fprintf(fp, " *\n");
fprintf(fp, " * Modified versions of MZ80 may not be publicly redistributed without author\n");
fprintf(fp, " * approval ([email protected]). This includes distributing via a publicly\n");
fprintf(fp, " * accessible LAN. You may make your own source modifications and distribute\n");
fprintf(fp, " * MZ80 in source or object form, but if you make modifications to MZ80\n");
fprintf(fp, " * then it should be noted in the top as a comment in makeZ80.c.\n");
fprintf(fp, " *\n");
fprintf(fp, " * MZ80 Licensing for commercial applications is available. Please email\n");
fprintf(fp, " * [email protected] for details.\n");
fprintf(fp, " *\n");
fprintf(fp, " * Synthcom Systems, Inc, and Neil Bradley will not be held responsible for\n");
fprintf(fp, " * any damage done by the use of MZ80. It is purely \"as-is\".\n");
fprintf(fp, " *\n");
fprintf(fp, " * If you use MZ80 in a freeware application, credit in the following text:\n");
fprintf(fp, " *\n");
fprintf(fp, " * \"Multi-Z80 CPU emulator by Neil Bradley ([email protected])\"\n");
fprintf(fp, " *\n");
fprintf(fp, " * must accompany the freeware application within the application itself or\n");
fprintf(fp, " * in the documentation.\n");
fprintf(fp, " *\n");
fprintf(fp, " * Legal stuff aside:\n");
fprintf(fp, " *\n");
fprintf(fp, " * If you find problems with MZ80, please email the author so they can get\n");
fprintf(fp, " * resolved. If you find a bug and fix it, please also email the author so\n");
fprintf(fp, " * that those bug fixes can be propogated to the installed base of MZ80\n");
fprintf(fp, " * users. If you find performance improvements or problems with MZ80, please\n");
fprintf(fp, " * email the author with your changes/suggestions and they will be rolled in\n");
fprintf(fp, " * with subsequent releases of MZ80.\n");
fprintf(fp, " *\n");
fprintf(fp, " * The whole idea of this emulator is to have the fastest available 32 bit\n");
fprintf(fp, " * Multi-Z80 emulator for the PC, giving maximum performance. \n");
fprintf(fp, " */\n\n");
fprintf(fp, "#include <stdio.h>\n");
fprintf(fp, "#include <stdlib.h>\n");
fprintf(fp, "#include <string.h>\n");
fprintf(fp, "#include \"mz80.h\"\n");
// HACK HACK
fprintf(fp, "UINT32 z80intAddr;\n");
fprintf(fp, "UINT32 z80pc;\n");
}
else
{
// Whoops. Unknown emission type.
assert(0);
}
fprintf(fp, "\n\n");
}
Alignment()
{
fprintf(fp, "\ntimes ($$-$) & 3 nop ; pad with NOPs to 4-byte boundary\n\n");
}
void ProcBegin(UINT32 dwOpcode)
{
Alignment();
fprintf(fp, "%s:\n", procname);
}
void SetSubFlagsSZHVC(UINT8 *pszLeft, UINT8 *pszRight)
{
fprintf(fp, " cpu.z80F = (cpu.z80F & ~(Z80_FLAG_CARRY | Z80_FLAG_NEGATIVE | Z80_FLAG_OVERFLOW_PARITY | \n");
fprintf(fp, " Z80_FLAG_HALF_CARRY | Z80_FLAG_ZERO | Z80_FLAG_SIGN)) |\n");
fprintf(fp, " pbSubSbcTable[((UINT32) %s << 8) | %s];\n", pszLeft, pszRight);
}
void SetSbcFlagsSZHVC(UINT8 *pszLeft, UINT8 *pszRight)
{
fprintf(fp, " cpu.z80F = (cpu.z80F & ~(Z80_FLAG_CARRY | Z80_FLAG_NEGATIVE | Z80_FLAG_OVERFLOW_PARITY | \n");
fprintf(fp, " Z80_FLAG_HALF_CARRY | Z80_FLAG_ZERO | Z80_FLAG_SIGN)) |\n");
fprintf(fp, " pbSubSbcTable[((UINT32) %s << 8) | %s | (((UINT32) cpu.z80F & Z80_FLAG_CARRY) << 16)];\n", pszLeft, pszRight);
}
void SetAddFlagsSZHVC(UINT8 *pszLeft, UINT8 *pszRight)
{
fprintf(fp, " cpu.z80F = (cpu.z80F & ~(Z80_FLAG_CARRY | Z80_FLAG_NEGATIVE | Z80_FLAG_OVERFLOW_PARITY | \n");
fprintf(fp, " Z80_FLAG_HALF_CARRY | Z80_FLAG_ZERO | Z80_FLAG_SIGN)) |\n");
fprintf(fp, " pbAddAdcTable[((UINT32) %s << 8) | %s];\n", pszLeft, pszRight);
}
void SetAdcFlagsSZHVC(UINT8 *pszLeft, UINT8 *pszRight)
{
fprintf(fp, " cpu.z80F = (cpu.z80F & ~(Z80_FLAG_CARRY | Z80_FLAG_NEGATIVE | Z80_FLAG_OVERFLOW_PARITY | \n");
fprintf(fp, " Z80_FLAG_HALF_CARRY | Z80_FLAG_ZERO | Z80_FLAG_SIGN)) |\n");
fprintf(fp, " pbAddAdcTable[((UINT32) %s << 8) | %s | (((UINT32) cpu.z80F & Z80_FLAG_CARRY) << 16)];\n", pszLeft, pszRight);
}
UINT32 dwOverflowCount = 0;
SetOverflow()
{
fprintf(fp, " seto dl\n");
fprintf(fp, " and ah, 0fbh ; Knock out parity/overflow\n");
fprintf(fp, " shl dl, 2\n");
fprintf(fp, " or ah, dl\n");
}
void FetchNextInstruction(UINT32 dwOpcode)
{
if (0xffffffff != dwOpcode)
{
fprintf(fp, " sub edi, byte %ld\n", Timing(bCurrentMode, dwOpcode));
if (bCurrentMode == TIMING_REGULAR)
fprintf(fp, " js near noMoreExec\n");
else
fprintf(fp, " js near noMoreExec\n");
}
fprintf(fp, " mov dl, byte [esi] ; Get our next instruction\n");
fprintf(fp, " inc esi ; Increment PC\n");
fprintf(fp, " jmp dword [z80regular+edx*4]\n\n");
}
void WriteValueToMemory(UINT8 *pszAddress, UINT8 *pszValue)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
fprintf(fp, " mov [cyclesRemaining], edi\n");
fprintf(fp, " mov [_z80af], ax ; Store AF\n");
// First off, load our byte to write into al after we've saved AF
if (strcmp(pszValue, "al") != 0)
fprintf(fp, " mov al, %s ; And our data to write\n", pszValue);
if (strcmp(pszValue, "[esi]") == 0) // Immediate value?
fprintf(fp, " inc esi ; Increment our program counter\n");
// Now get the address in DX - regardless of what it is
if (strcmp(pszAddress, "[_z80de]") == 0 ||
strcmp(pszAddress, "[_orgval]") == 0 ||
strcmp(pszAddress, "[_z80ix]") == 0 ||
strcmp(pszAddress, "[_z80iy]") == 0)
fprintf(fp, " mov dx, %s\n", pszAddress);
fprintf(fp, " mov edi, [_z80MemWrite] ; Point to the write array\n\n", cpubasename);
fprintf(fp, "checkLoop%ld:\n", dwGlobalLabel);
fprintf(fp, " cmp [edi], word 0ffffh ; End of our list?\n");
fprintf(fp, " je memoryWrite%ld ; Yes - go write it!\n", dwGlobalLabel);
if (strcmp(pszAddress, "[_z80de]") == 0 ||
strcmp(pszAddress, "[_orgval]") == 0 ||
strcmp(pszAddress, "[_z80ix]") == 0 ||
strcmp(pszAddress, "[_z80iy]") == 0)
fprintf(fp, " cmp dx, [edi] ; Are we smaller?\n", pszAddress);
else
fprintf(fp, " cmp %s, [edi] ; Are we smaller?\n", pszAddress);
fprintf(fp, " jb nextAddr%ld ; Yes... go to the next addr\n", dwGlobalLabel);
if (strcmp(pszAddress, "[_z80de]") == 0 ||
strcmp(pszAddress, "[_orgval]") == 0 ||
strcmp(pszAddress, "[_z80ix]") == 0 ||
strcmp(pszAddress, "[_z80iy]") == 0)
fprintf(fp, " cmp dx, [edi+4] ; Are we smaller?\n", pszAddress);
else
fprintf(fp, " cmp %s, [edi+4] ; Are we smaller?\n", pszAddress);
fprintf(fp, " jbe callRoutine%ld ; If not, go call it!\n\n", dwGlobalLabel);
fprintf(fp, "nextAddr%ld:\n", dwGlobalLabel);
fprintf(fp, " add edi, 10h ; Next structure, please\n");
fprintf(fp, " jmp short checkLoop%ld\n\n", dwGlobalLabel);
fprintf(fp, "callRoutine%ld:\n", dwGlobalLabel);
// Save off our registers!
if ((strcmp(pszAddress, "dx") != 0) && (strcmp(pszAddress, "[_z80de]") != 0) &&
(strcmp(pszAddress, "[_z80ix]") != 0) &&
(strcmp(pszAddress, "[_orgval]") != 0) &&
(strcmp(pszAddress, "[_z80iy]") != 0))
fprintf(fp, " mov dx, %s ; Get our address to target\n", pszAddress);
fprintf(fp, " call WriteMemoryByte ; Go write the data!\n");
fprintf(fp, " jmp short WriteMacroExit%ld\n", dwGlobalLabel);
fprintf(fp, "memoryWrite%ld:\n", dwGlobalLabel);
if (strcmp(pszValue, "[esi]") == 0)
fprintf(fp, " mov [ebp + e%s], al ; Store our direct value\n", pszAddress);
else
{
if (pszValue[0] == 'b' && pszValue[1] == 'y' && pszValue[2] == 't')
{
fprintf(fp, " mov edi, edx\n");
assert(strcmp(pszValue, "dl") != 0);
fprintf(fp, " mov dl, %s\n", pszValue);
if (strcmp(pszAddress, "dx") == 0)
fprintf(fp, " mov [ebp + edi], dl\n");
else
fprintf(fp, " mov [ebp + e%s], dl\n", pszAddress);
fprintf(fp, " mov edx, edi\n");
}
else
{
if (strcmp(pszAddress, "[_z80de]") != 0 &&
strcmp(pszAddress, "[_orgval]") != 0 &&
strcmp(pszAddress, "[_z80ix]") != 0 &&
strcmp(pszAddress, "[_z80iy]") != 0)
fprintf(fp, " mov [ebp + e%s], %s\n", pszAddress, pszValue);
else
fprintf(fp, " mov [ebp + edx], al\n");
}
}
fprintf(fp, " mov ax, [_z80af] ; Get our accumulator and flags\n");
fprintf(fp, "WriteMacroExit%ld:\n", dwGlobalLabel);
fprintf(fp, " mov edi, [cyclesRemaining]\n");
++dwGlobalLabel;
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " psMemWrite = cpu.z80MemWrite; /* Beginning of our handler */\n");
fprintf(fp, " while (psMemWrite->lowAddr != 0xffffffff)\n");
fprintf(fp, " {\n");
fprintf(fp, " if ((%s >= psMemWrite->lowAddr) && (%s <= psMemWrite->highAddr))\n", pszAddress, pszAddress);
fprintf(fp, " {\n");
fprintf(fp, " cpu.z80pc = (UINT32) pbPC - (UINT32) cpu.z80Base;\n");
fprintf(fp, " if (psMemWrite->memoryCall)\n");
fprintf(fp, " {\n");
fprintf(fp, " psMemWrite->memoryCall(%s, %s, psMemWrite);\n", pszAddress, pszValue);
fprintf(fp, " }\n");
fprintf(fp, " else\n");
fprintf(fp, " {\n");
fprintf(fp, " *((UINT8 *) psMemWrite->pUserArea + (%s - psMemWrite->lowAddr)) = %s;\n", pszAddress, pszValue);
fprintf(fp, " }\n");
fprintf(fp, " psMemWrite = NULL;\n");
fprintf(fp, " break;\n");
fprintf(fp, " }\n");
fprintf(fp, " ++psMemWrite;\n");
fprintf(fp, " }\n\n");
fprintf(fp, " if (psMemWrite)\n");
fprintf(fp, " {\n");
fprintf(fp, " cpu.z80Base[%s] = (UINT8) %s;\n", pszAddress, pszValue);
fprintf(fp, " }\n\n");
}
}
void WriteWordToMemory(UINT8 *pszAddress, UINT8 *pszTarget)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
fprintf(fp, " mov [cyclesRemaining], edi\n");
fprintf(fp, " mov edi, [_z80MemWrite] ; Point to the write array\n\n", cpubasename);
fprintf(fp, "checkLoop%ld:\n", dwGlobalLabel);
fprintf(fp, " cmp [edi], word 0ffffh ; End of the list?\n");
fprintf(fp, " je memoryWrite%ld\n", dwGlobalLabel);
fprintf(fp, " cmp %s, [edi] ; Are we smaller?\n", pszAddress);
fprintf(fp, " jb nextAddr%ld ; Yes, go to the next address\n", dwGlobalLabel);
fprintf(fp, " cmp %s, [edi+4] ; Are we bigger?\n", pszAddress);
fprintf(fp, " jbe callRoutine%ld\n\n", dwGlobalLabel);
fprintf(fp, "nextAddr%ld:\n", dwGlobalLabel);
fprintf(fp, " add edi, 10h ; Next structure!\n");
fprintf(fp, " jmp short checkLoop%ld\n\n", dwGlobalLabel);
fprintf(fp, "callRoutine%ld:\n", dwGlobalLabel);
fprintf(fp, " push ax ; Save this for later\n");
// Write the LSB
fprintf(fp, " push dx\n");
if (strcmp(pszTarget, "ax") != 0)
{
fprintf(fp, " mov ax, %s\n", pszTarget);
}
else
{
fprintf(fp, " xchg ah, al\n");
}
fprintf(fp, " call WriteMemoryByte\n");
fprintf(fp, " pop dx\n");
fprintf(fp, " pop ax\n");
fprintf(fp, " inc dx\n\n");
fprintf(fp, " push ax\n");
fprintf(fp, " push dx\n");
if (strcmp(pszTarget, "ax") != 0)
{
fprintf(fp, " mov ax, %s\n", pszTarget);
fprintf(fp, " xchg ah, al\n");
}
fprintf(fp, " call WriteMemoryByte\n");
fprintf(fp, " pop dx\n");
fprintf(fp, " pop ax ; Restore us!\n");
fprintf(fp, " jmp writeExit%ld\n\n", dwGlobalLabel);
fprintf(fp, "memoryWrite%ld:\n", dwGlobalLabel);
if (strlen(pszTarget) != 2)
{
fprintf(fp, " mov di, %s\n", pszTarget);
fprintf(fp, " mov [ebp + e%s], di ; Store our word\n", pszAddress);
}
else
{
if (strcmp(pszTarget, "ax") != 0)
{
fprintf(fp, " mov [ebp + e%s], %s ; Store our word\n", pszAddress, pszTarget);
}
else
{
fprintf(fp, " xchg ah, al ; Swap for later\n");
fprintf(fp, " mov [ebp + e%s], %s ; Store our word\n", pszAddress, pszTarget);
fprintf(fp, " xchg ah, al ; Restore\n");
}
}
fprintf(fp, "writeExit%ld:\n", dwGlobalLabel);
fprintf(fp, " mov edi, [cyclesRemaining]\n");
dwGlobalLabel++;
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " psMemWrite = cpu.z80MemWrite; /* Beginning of our handler */\n");
fprintf(fp, " while (psMemWrite->lowAddr != 0xffffffff)\n");
fprintf(fp, " {\n");
fprintf(fp, " if ((%s >= psMemWrite->lowAddr) && (%s <= psMemWrite->highAddr))\n", pszAddress, pszAddress);
fprintf(fp, " {\n");
fprintf(fp, " cpu.z80pc = (UINT32) pbPC - (UINT32) cpu.z80Base;\n");
fprintf(fp, " if (psMemWrite->memoryCall)\n");
fprintf(fp, " {\n");
fprintf(fp, " psMemWrite->memoryCall(%s, (%s & 0xff), psMemWrite);\n", pszAddress, pszTarget);
fprintf(fp, " psMemWrite->memoryCall(%s + 1, (%s >> 8), psMemWrite);\n", pszAddress, pszTarget);
fprintf(fp, " }\n");
fprintf(fp, " else\n");
fprintf(fp, " {\n");
fprintf(fp, " *((UINT8 *) psMemWrite->pUserArea + (%s - psMemWrite->lowAddr)) = %s;\n", pszAddress, pszTarget);
fprintf(fp, " *((UINT8 *) psMemWrite->pUserArea + (%s - psMemWrite->lowAddr) + 1) = %s >> 8;\n", pszAddress, pszTarget);
fprintf(fp, " }\n");
fprintf(fp, " psMemWrite = NULL;\n");
fprintf(fp, " break;\n");
fprintf(fp, " }\n");
fprintf(fp, " ++psMemWrite;\n");
fprintf(fp, " }\n\n");
fprintf(fp, " if (psMemWrite)\n");
fprintf(fp, " {\n");
fprintf(fp, " cpu.z80Base[%s] = (UINT8) %s;\n", pszAddress, pszTarget);
fprintf(fp, " cpu.z80Base[%s + 1] = (UINT8) ((UINT32) %s >> 8);\n", pszAddress, pszTarget);
fprintf(fp, " }\n\n");
}
else
{
assert(0);
}
}
void WriteValueToIo(UINT8 *pszIoAddress, UINT8 *pszValue)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
fprintf(fp, " mov [cyclesRemaining], edi\n");
fprintf(fp, " mov [_z80af], ax ; Store AF\n");
if (strcmp(pszValue, "al") != 0)
fprintf(fp, " mov al, %s ; And our data to write\n", pszValue);
if (strcmp(pszValue, "[esi]") == 0) // Immediate value?
fprintf(fp, " inc esi ; Increment our program counter\n");
fprintf(fp, " mov edi, [_z80IoWrite] ; Point to the I/O write array\n\n", cpubasename);
fprintf(fp, "checkLoop%ld:\n", dwGlobalLabel);
fprintf(fp, " cmp [edi], word 0ffffh ; End of our list?\n");
fprintf(fp, " je WriteMacroExit%ld ; Yes - ignore it!\n", dwGlobalLabel);
fprintf(fp, " cmp %s, [edi] ; Are we smaller?\n", pszIoAddress);
fprintf(fp, " jb nextAddr%ld ; Yes... go to the next addr\n", dwGlobalLabel);
fprintf(fp, " cmp %s, [edi+2] ; Are we bigger?\n", pszIoAddress);
fprintf(fp, " jbe callRoutine%ld ; If not, go call it!\n\n", dwGlobalLabel);
fprintf(fp, "nextAddr%ld:\n", dwGlobalLabel);
fprintf(fp, " add edi, 0ch ; Next structure, please\n");
fprintf(fp, " jmp short checkLoop%ld\n\n", dwGlobalLabel);
fprintf(fp, "callRoutine%ld:\n", dwGlobalLabel);
// Save off our registers!
if (strcmp(pszIoAddress, "dx") != 0)
fprintf(fp, " mov dx, %s ; Get our address to target\n", pszIoAddress);
fprintf(fp, " call WriteIOByte ; Go write the data!\n");
fprintf(fp, "WriteMacroExit%ld:\n", dwGlobalLabel);
fprintf(fp, " mov edi, [cyclesRemaining]\n");
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " psIoWrite = cpu.z80IoWrite; /* Beginning of our handler */\n");
fprintf(fp, " while (psIoWrite->lowIoAddr != 0xffff)\n");
fprintf(fp, " {\n");
fprintf(fp, " if ((%s >= psIoWrite->lowIoAddr) && (%s <= psIoWrite->highIoAddr))\n", pszIoAddress, pszIoAddress);
fprintf(fp, " {\n");
fprintf(fp, " cpu.z80pc = (UINT32) pbPC - (UINT32) cpu.z80Base;\n");
fprintf(fp, " psIoWrite->IOCall(%s, %s, psIoWrite);\n", pszIoAddress, pszValue);
fprintf(fp, " psIoWrite = NULL;\n");
fprintf(fp, " break;\n");
fprintf(fp, " }\n");
fprintf(fp, " ++psIoWrite;\n");
fprintf(fp, " }\n\n");
}
else
{
assert(0);
}
++dwGlobalLabel;
}
void ReadValueFromMemory(UINT8 *pszAddress, UINT8 *pszTarget)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
fprintf(fp, " mov [cyclesRemaining], edi\n");
fprintf(fp, " mov edi, [_z80MemRead] ; Point to the read array\n\n", cpubasename);
fprintf(fp, "checkLoop%ld:\n", dwGlobalLabel);
fprintf(fp, " cmp [edi], word 0ffffh ; End of the list?\n");
fprintf(fp, " je memoryRead%ld\n", dwGlobalLabel);
fprintf(fp, " cmp e%s, [edi] ; Are we smaller?\n", pszAddress);
fprintf(fp, " jb nextAddr%ld ; Yes, go to the next address\n", dwGlobalLabel);
fprintf(fp, " cmp e%s, [edi+4] ; Are we bigger?\n", pszAddress);
fprintf(fp, " jbe callRoutine%ld\n\n", dwGlobalLabel);
fprintf(fp, "nextAddr%ld:\n", dwGlobalLabel);
fprintf(fp, " add edi, 10h ; Next structure!\n");
fprintf(fp, " jmp short checkLoop%ld\n\n", dwGlobalLabel);
fprintf(fp, "callRoutine%ld:\n", dwGlobalLabel);
if (strcmp(pszAddress, "dx") != 0)
fprintf(fp, " mov dx, %s ; Get our address\n", pszAddress);
fprintf(fp, " call ReadMemoryByte ; Standard read routine\n");
// Yes, these are intentionally reversed!
if (strcmp(pszTarget, "al") == 0)
fprintf(fp, " mov [_z80af], al ; Save our new accumulator\n");
else
if (strcmp(pszTarget, "ah") == 0)
fprintf(fp, " mov [_z80af + 1], al ; Save our new flags\n");
else
fprintf(fp, " mov %s, al ; Put our returned value here\n", pszTarget);
// And are properly restored HERE:
fprintf(fp, " mov ax, [_z80af] ; Get our AF back\n");
// Restore registers here...
fprintf(fp, " jmp short readExit%ld\n\n", dwGlobalLabel);
fprintf(fp, "memoryRead%ld:\n", dwGlobalLabel);
if (pszTarget[0] == 'b' && pszTarget[1] == 'y' && pszTarget[2] == 't')
{
fprintf(fp, " mov di, dx\n");
fprintf(fp, " mov dl, [ebp + e%s]\n", pszAddress);
fprintf(fp, " mov %s, dl\n", pszTarget);
fprintf(fp, " mov dx, di\n");
}
else
fprintf(fp, " mov %s, [ebp + e%s] ; Get our data\n\n", pszTarget, pszAddress);
fprintf(fp, "readExit%ld:\n", dwGlobalLabel);
fprintf(fp, " mov edi, [cyclesRemaining]\n");
dwGlobalLabel++;
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " psMemRead = cpu.z80MemRead; /* Beginning of our handler */\n");
fprintf(fp, " while (psMemRead->lowAddr != 0xffffffff)\n");
fprintf(fp, " {\n");
fprintf(fp, " if ((%s >= psMemRead->lowAddr) && (%s <= psMemRead->highAddr))\n", pszAddress, pszAddress);
fprintf(fp, " {\n");
fprintf(fp, " cpu.z80pc = (UINT32) pbPC - (UINT32) cpu.z80Base;\n");
fprintf(fp, " if (psMemRead->memoryCall)\n");
fprintf(fp, " {\n");
fprintf(fp, " %s = psMemRead->memoryCall(%s, psMemRead);\n", pszTarget, pszAddress);
fprintf(fp, " }\n");
fprintf(fp, " else\n");
fprintf(fp, " {\n");
fprintf(fp, " %s = *((UINT8 *) psMemRead->pUserArea + (%s - psMemRead->lowAddr));\n", pszTarget, pszAddress);
fprintf(fp, " }\n");
fprintf(fp, " psMemRead = NULL;\n");
fprintf(fp, " break;\n");
fprintf(fp, " }\n");
fprintf(fp, " ++psMemRead;\n");
fprintf(fp, " }\n\n");
fprintf(fp, " if (psMemRead)\n");
fprintf(fp, " {\n");
fprintf(fp, " %s = cpu.z80Base[%s];\n", pszTarget, pszAddress);
fprintf(fp, " }\n\n");
}
else
{
assert(0);
}
}
void ReadWordFromMemory(UINT8 *pszAddress, UINT8 *pszTarget)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
fprintf(fp, " mov [cyclesRemaining], edi\n");
fprintf(fp, " mov edi, [_z80MemRead] ; Point to the read array\n\n", cpubasename);
fprintf(fp, "checkLoop%ld:\n", dwGlobalLabel);
fprintf(fp, " cmp [edi], word 0ffffh ; End of the list?\n");
fprintf(fp, " je memoryRead%ld\n", dwGlobalLabel);
fprintf(fp, " cmp %s, [edi] ; Are we smaller?\n", pszAddress);
fprintf(fp, " jb nextAddr%ld ; Yes, go to the next address\n", dwGlobalLabel);
fprintf(fp, " cmp %s, [edi+4] ; Are we bigger?\n", pszAddress);
fprintf(fp, " jbe callRoutine%ld\n\n", dwGlobalLabel);
fprintf(fp, "nextAddr%ld:\n", dwGlobalLabel);
fprintf(fp, " add edi, 10h ; Next structure!\n");
fprintf(fp, " jmp short checkLoop%ld\n\n", dwGlobalLabel);
fprintf(fp, "callRoutine%ld:\n", dwGlobalLabel);
if (strcmp(pszAddress, "dx") != 0)
fprintf(fp, " mov dx, %s ; Get our address\n", pszAddress);
if (strcmp(pszTarget, "ax") != 0)
fprintf(fp, " push ax ; Save this for later\n");
fprintf(fp, " push dx ; Save address\n");
fprintf(fp, " call ReadMemoryByte ; Standard read routine\n");
fprintf(fp, " pop dx ; Restore our address\n");
fprintf(fp, " inc dx ; Next byte, please\n");
fprintf(fp, " push ax ; Save returned byte\n");
fprintf(fp, " call ReadMemoryByte ; Standard read routine\n");
fprintf(fp, " xchg ah, al ; Swap for endian's sake\n");
fprintf(fp, " pop dx ; Restore LSB\n");
fprintf(fp, " mov dh, ah ; Our word is now in DX\n");
// DX Now has our data and our address is toast
if (strcmp(pszTarget, "ax") != 0)
{
fprintf(fp, " pop ax ; Restore this\n");
if (strcmp(pszTarget, "dx") != 0)
{
fprintf(fp, " mov %s, dx ; Store our word\n", pszTarget);
}
}
else
fprintf(fp, " mov ax, dx\n");
if (strcmp(pszTarget, "ax") == 0)
{
fprintf(fp, " xchg ah, al\n");
}
fprintf(fp, " jmp readExit%ld\n\n", dwGlobalLabel);
fprintf(fp, "memoryRead%ld:\n", dwGlobalLabel);
if (strlen(pszTarget) == 2)
{
fprintf(fp, " mov %s, [ebp + e%s]\n", pszTarget, pszAddress);
if (strcmp(pszTarget, "ax") == 0)
{
fprintf(fp, " xchg ah, al\n");
}
}
else
{
fprintf(fp, " mov dx, [ebp + e%s]\n", pszAddress);
fprintf(fp, " mov %s, dx\n", pszTarget);
}
fprintf(fp, "readExit%ld:\n", dwGlobalLabel);
fprintf(fp, " mov edi, [cyclesRemaining]\n");
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " psMemRead = cpu.z80MemRead; /* Beginning of our handler */\n");
fprintf(fp, " while (psMemRead->lowAddr != 0xffffffff)\n");
fprintf(fp, " {\n");
fprintf(fp, " if ((%s >= psMemRead->lowAddr) && (%s <= psMemRead->highAddr))\n", pszAddress, pszAddress);
fprintf(fp, " {\n");
fprintf(fp, " cpu.z80pc = (UINT32) pbPC - (UINT32) cpu.z80Base;\n");
fprintf(fp, " if (psMemRead->memoryCall)\n");
fprintf(fp, " {\n");
fprintf(fp, " %s = psMemRead->memoryCall(%s, psMemRead);\n", pszTarget, pszAddress);
fprintf(fp, " %s |= (UINT32) ((UINT32) psMemRead->memoryCall(%s + 1, psMemRead) << 8);\n", pszTarget, pszAddress);
fprintf(fp, " }\n");
fprintf(fp, " else\n");
fprintf(fp, " {\n");
fprintf(fp, " %s = *((UINT8 *) psMemRead->pUserArea + (%s - psMemRead->lowAddr));\n", pszTarget, pszAddress);
fprintf(fp, " %s |= (UINT32) ((UINT32) *((UINT8 *) psMemRead->pUserArea + (%s - psMemRead->lowAddr + 1)) << 8);\n", pszTarget, pszAddress);
fprintf(fp, " }\n");
fprintf(fp, " psMemRead = NULL;\n");
fprintf(fp, " break;\n");
fprintf(fp, " }\n");
fprintf(fp, " ++psMemRead;\n");
fprintf(fp, " }\n\n");
fprintf(fp, " if (psMemRead)\n");
fprintf(fp, " {\n");
fprintf(fp, " %s = cpu.z80Base[%s];\n", pszTarget, pszAddress);
fprintf(fp, " %s |= (UINT32) ((UINT32) cpu.z80Base[%s + 1] << 8);\n", pszTarget, pszAddress);
fprintf(fp, " }\n\n");
}
else
{
assert(0);
}
dwGlobalLabel++;
}
void ReadValueFromIo(UINT8 *pszIoAddress, UINT8 *pszTarget)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
fprintf(fp, " mov [cyclesRemaining], edi\n");
fprintf(fp, " mov edi, [_z80IoRead] ; Point to the read array\n\n", cpubasename);
fprintf(fp, "checkLoop%ld:\n", dwGlobalLabel);
fprintf(fp, " cmp [edi], word 0ffffh ; End of the list?\n");
fprintf(fp, " je ioRead%ld\n", dwGlobalLabel);
fprintf(fp, " cmp %s, [edi] ; Are we smaller?\n", pszIoAddress);
fprintf(fp, " jb nextAddr%ld ; Yes, go to the next address\n", dwGlobalLabel);
fprintf(fp, " cmp %s, [edi+2] ; Are we bigger?\n", pszIoAddress);
fprintf(fp, " jbe callRoutine%ld\n\n", dwGlobalLabel);
fprintf(fp, "nextAddr%ld:\n", dwGlobalLabel);
fprintf(fp, " add edi, 0ch ; Next structure!\n");
fprintf(fp, " jmp short checkLoop%ld\n\n", dwGlobalLabel);
fprintf(fp, "callRoutine%ld:\n", dwGlobalLabel);
if (strcmp(pszIoAddress, "dx") != 0)
fprintf(fp, " mov dx, %s ; Get our address\n", pszIoAddress);
fprintf(fp, " call ReadIOByte ; Standard read routine\n");
// Yes, these are intentionally reversed!
if (strcmp(pszTarget, "al") == 0)
fprintf(fp, " mov [_z80af], al ; Save our new accumulator\n");
else
if (strcmp(pszTarget, "ah") == 0)
fprintf(fp, " mov [_z80af + 1], ah ; Save our new flags\n");
else
if (strcmp(pszTarget, "dl") == 0)
fprintf(fp, " mov [_z80de], al ; Put it in E\n");
else
if (strcmp(pszTarget, "dh") == 0)
fprintf(fp, " mov [_z80de + 1], al ; Put it in D\n");
else
if (strcmp(pszTarget, "*dl") == 0)
fprintf(fp, " mov dl, al ; Put it in DL for later consumption\n");
else
fprintf(fp, " mov %s, al ; Put our returned value here\n", pszTarget);
// And are properly restored HERE:
fprintf(fp, " mov ax, [_z80af] ; Get our AF back\n");
// Restore registers here...
fprintf(fp, " jmp short readExit%ld\n\n", dwGlobalLabel);
fprintf(fp, "ioRead%ld:\n", dwGlobalLabel);
if (strcmp(pszTarget, "*dl") == 0)
fprintf(fp, " mov dl, 0ffh ; An unreferenced read\n");
else
fprintf(fp, " mov %s, 0ffh ; An unreferenced read\n", pszTarget);
fprintf(fp, "readExit%ld:\n", dwGlobalLabel);
fprintf(fp, " mov edi, [cyclesRemaining]\n");
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " psIoRead = cpu.z80IoRead; /* Beginning of our handler */\n");
fprintf(fp, " while (psIoRead->lowIoAddr != 0xffff)\n");
fprintf(fp, " {\n");
fprintf(fp, " if ((%s >= psIoRead->lowIoAddr) && (%s <= psIoRead->highIoAddr))\n", pszIoAddress, pszIoAddress);
fprintf(fp, " {\n");
fprintf(fp, " cpu.z80pc = (UINT32) pbPC - (UINT32) cpu.z80Base;\n");
fprintf(fp, " %s = psIoRead->IOCall(%s, psIoRead);\n", pszTarget, pszIoAddress);
fprintf(fp, " psIoRead = NULL;\n");
fprintf(fp, " break;\n");
fprintf(fp, " }\n");
fprintf(fp, " ++psIoRead;\n");
fprintf(fp, " }\n\n");
fprintf(fp, " if (psIoRead)\n");
fprintf(fp, " {\n");
fprintf(fp, " %s = 0xff; /* Unclaimed I/O read */\n", pszTarget);
fprintf(fp, " }\n\n");
}
else
{
assert(0);
}
dwGlobalLabel++;
}
// Basic instruction set area
void MiscHandler(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
if (dwOpcode == 0xe3)
{
if (bThroughCallHandler)
{
fprintf(fp, " call PopWord\n");
fprintf(fp, " xchg bx, [_wordval]\n");
fprintf(fp, " call PushWord\n");
}
else
{
fprintf(fp, " mov dx, word [_z80sp]\n");
fprintf(fp, " xchg bx, [ebp+edx]\n");
fprintf(fp, " xor edx, edx\n");
}
}
if (dwOpcode == 0x2a)
{
fprintf(fp, " mov dx, [esi] ; Get address to load\n");
fprintf(fp, " add esi, 2 ; Skip over it so we don't execute it\n");
ReadWordFromMemory("dx", "bx");
fprintf(fp, " xor edx, edx\n");
}
if (dwOpcode == 0xfb)
{
fprintf(fp, " or dword [_z80iff], IFF1 ; Indicate interrupts are enabled now\n");
fprintf(fp, " sub edi, 4 ; Takes 4 cycles!\n");
fprintf(fp, " mov [dwEITiming], edi ; Snapshot our current timing\n");
fprintf(fp, " mov [bEIExit], byte 1 ; Indicate we're exiting because of an EI\n");
fprintf(fp, " xor edi, edi ; Force next instruction to exit\n");
fprintf(fp, " mov dl, byte [esi] ; Get our next instruction\n");
fprintf(fp, " inc esi ; Next PC\n");
fprintf(fp, " jmp dword [z80regular+edx*4]\n\n");
}
if (dwOpcode == 0xf9)
fprintf(fp, " mov word [_z80sp], bx\n");
if (dwOpcode == 0xd9)
{
fprintf(fp, " mov [cyclesRemaining], edi\n");
fprintf(fp, " mov di, [_z80de]\n");
fprintf(fp, " xchg cx, [_z80bcprime]\n");
fprintf(fp, " xchg di, [_z80deprime]\n");
fprintf(fp, " xchg bx, [_z80hlprime]\n");
fprintf(fp, " mov [_z80de], di\n");
fprintf(fp, " mov edi, [cyclesRemaining]\n");
}
if (dwOpcode == 0x76)
{
fprintf(fp, " mov dword [_z80halted], 1 ; We've halted the chip!\n");
if (FALSE == bNoTiming)
{
fprintf(fp, " xor edi, edi\n");
fprintf(fp, " mov [cyclesRemaining], edi\n");
}
fprintf(fp, " jmp noMoreExec\n");
return;
}
if (dwOpcode == 0x3f)
{
fprintf(fp, " mov dl, ah\n");
fprintf(fp, " and dl, 01h\n");
fprintf(fp, " shl dl, 4\n");
fprintf(fp, " xor ah, 01h\n");
fprintf(fp, " and ah, 0edh\n");
fprintf(fp, " or ah, dl\n");
}
if (dwOpcode == 0x37)
{
fprintf(fp, " or ah, 1\n");
fprintf(fp, " and ah,0edh\n");
}
if (dwOpcode == 0x27)
{
fprintf(fp, " mov dh, ah\n");
fprintf(fp, " and dh, 02ah\n");
fprintf(fp, " test ah, 02h ; Were we doing a subtraction?\n");
fprintf(fp, " jnz handleNeg ; Nope!\n");
fprintf(fp, " sahf\n");
fprintf(fp, " daa\n");
fprintf(fp, " lahf\n");
fprintf(fp, " jmp short endDaa\n");
fprintf(fp, "handleNeg:\n");
fprintf(fp, " sahf\n");
fprintf(fp, " das\n");
fprintf(fp, " lahf\n");
fprintf(fp, "endDaa:\n");
fprintf(fp, " and ah, 0d5h\n");
fprintf(fp, " or ah, dh\n");
fprintf(fp, " xor edx, edx\n");
}
if (dwOpcode == 0x08)
{
fprintf(fp, " xchg ah, al\n");
fprintf(fp, " xchg ax, [_z80afprime]\n");
fprintf(fp, " xchg ah, al\n");
}
if (dwOpcode == 0x07)
{
fprintf(fp, " sahf\n");
fprintf(fp, " rol al, 1\n");
fprintf(fp, " lahf\n");
fprintf(fp, " and ah, 0edh\n");
}
if (dwOpcode == 0x0f)
{
fprintf(fp, " sahf\n");
fprintf(fp, " ror al, 1\n");
fprintf(fp, " lahf\n");
fprintf(fp, " and ah, 0edh\n");
}
if (dwOpcode == 0xe9)
{
fprintf(fp, " mov si, bx\n");
fprintf(fp, " and esi, 0ffffh\n");
fprintf(fp, " add esi, ebp\n");
}
if (dwOpcode == 0xeb)
fprintf(fp, " xchg [_z80de], bx ; Exchange DE & HL\n");
if (dwOpcode == 0x2f)
{
fprintf(fp, " not al\n");
fprintf(fp, " or ah, 012h ; N And H are now on!\n");
}
if (dwOpcode == 0x10) // DJNZ
{
fprintf(fp, " mov dl, [esi] ; Get our relative offset\n");
fprintf(fp, " inc esi ; Next instruction, please!\n");
fprintf(fp, " dec ch ; Decrement B\n");
fprintf(fp, " jz noJump ; Don't take the jump if it's done!\n");
fprintf(fp, "; Otherwise, take the jump\n");
fprintf(fp, " sub edi, 5\n");
fprintf(fp, " xchg eax, edx\n");
fprintf(fp, " cbw\n");
fprintf(fp, " xchg eax, edx\n");
fprintf(fp, " sub esi, ebp\n");
fprintf(fp, " add si, dx\n");
fprintf(fp, " add esi, ebp\n");
fprintf(fp, "noJump:\n");
fprintf(fp, " xor edx, edx\n");
}
if (dwOpcode == 0x3a) // LD A,(xxxx)
{
fprintf(fp, " mov dx, [esi] ; Get our address\n");
fprintf(fp, " add esi, 2 ; Skip past the address\n");
ReadValueFromMemory("dx", "al");
fprintf(fp, " xor edx, edx ; Make sure we don't hose things\n");
}
if (dwOpcode == 0xf3) // DI
{
fprintf(fp, " and dword [_z80iff], (~IFF1) ; Not in an interrupt\n");
}
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
if (dwOpcode == 0x76) // HALT!
{
fprintf(fp, " cpu.z80halted = 1;\n");
fprintf(fp, " dwElapsedTicks += sdwCyclesRemaining;\n");
fprintf(fp, " sdwCyclesRemaining = 0;\n");
}
else
if (dwOpcode == 0x2f) // CPL
{
fprintf(fp, " cpu.z80A ^= 0xff;\n");
fprintf(fp, " cpu.z80F |= (Z80_FLAG_NEGATIVE | Z80_FLAG_HALF_CARRY);\n");
}
else
if (dwOpcode == 0xd9) // EXX
{
fprintf(fp, " dwTemp = cpu.z80DE;\n");
fprintf(fp, " cpu.z80DE = cpu.z80deprime;\n");
fprintf(fp, " cpu.z80deprime = dwTemp;\n");
fprintf(fp, " dwTemp = cpu.z80BC;\n");
fprintf(fp, " cpu.z80BC = cpu.z80bcprime;\n");
fprintf(fp, " cpu.z80bcprime = dwTemp;\n");
fprintf(fp, " dwTemp = cpu.z80HL;\n");
fprintf(fp, " cpu.z80HL = cpu.z80hlprime;\n");
fprintf(fp, " cpu.z80hlprime = dwTemp;\n");
}
else
if (dwOpcode == 0xf9) // LD SP, HL
{
fprintf(fp, " cpu.z80sp = cpu.z80HL;\n");
}
else
if (dwOpcode == 0x27) // DAA
{
fprintf(fp, " dwAddr = (((cpu.z80F & Z80_FLAG_CARRY) | \n");
fprintf(fp, " ((cpu.z80F & Z80_FLAG_HALF_CARRY) >> 3) | \n");
fprintf(fp, " ((cpu.z80F & Z80_FLAG_NEGATIVE) << 1)) << 8) | cpu.z80A;\n");
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_CARRY | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_HALF_CARRY | Z80_FLAG_ZERO | Z80_FLAG_SIGN);\n");
fprintf(fp, " cpu.z80F |= (wDAATable[dwAddr] >> 8);\n");
fprintf(fp, " cpu.z80A = wDAATable[dwAddr] & 0xff;\n");
}
else
if (dwOpcode == 0x2a)
{
fprintf(fp, " dwAddr = *pbPC++;\n");
fprintf(fp, " dwAddr |= ((UINT32) *pbPC++ << 8);\n");
ReadWordFromMemory("dwAddr", "cpu.z80HL");
}
else
if (dwOpcode == 0xe3) // EX (SP), HL
{
ReadWordFromMemory("cpu.z80sp", "dwAddr");
WriteWordToMemory("cpu.z80sp", "cpu.z80HL");
fprintf(fp, " cpu.z80HL = dwAddr;\n");
}
else
if (dwOpcode == 0xe9) // JP (HL)
{
fprintf(fp, " pbPC = cpu.z80Base + cpu.z80HL;\n");
}
else
if (0x08 == dwOpcode) // EX AF, AF'
{
fprintf(fp, " dwAddr = (UINT32) cpu.z80AF;\n");
fprintf(fp, " cpu.z80AF = cpu.z80afprime;\n");
fprintf(fp, " cpu.z80afprime = dwAddr;\n");
}
else
if (0xeb == dwOpcode) // EX DE, HL
{
fprintf(fp, " dwAddr = cpu.z80DE;\n");
fprintf(fp, " cpu.z80DE = cpu.z80HL;\n");
fprintf(fp, " cpu.z80HL = dwAddr;\n");
}
else
if (0x10 == dwOpcode) // DJNZ
{
fprintf(fp, " sdwAddr = (INT8) *pbPC++; /* Get LSB first */\n");
fprintf(fp, " if (--cpu.z80B)\n");
fprintf(fp, " {\n");
fprintf(fp, " dwElapsedTicks += 5; /* 5 More for jump taken */\n");
fprintf(fp, " cpu.z80pc = (UINT32) pbPC - (UINT32) cpu.z80Base;\n");
fprintf(fp, " sdwAddr = (sdwAddr + (INT32) cpu.z80pc) & 0xffff;\n");
fprintf(fp, " pbPC = cpu.z80Base + sdwAddr; /* Normalize the address */\n");
fprintf(fp, " }\n");
}
else
if (0x37 == dwOpcode) // SCF
{
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_HALF_CARRY | Z80_FLAG_NEGATIVE);\n");
fprintf(fp, " cpu.z80F |= Z80_FLAG_CARRY;\n");
}
else
if (0x3f == dwOpcode) // CCF
{
fprintf(fp, " bTemp = (cpu.z80F & Z80_FLAG_CARRY) << 4;\n");
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_HALF_CARRY | Z80_FLAG_NEGATIVE);\n");
fprintf(fp, " cpu.z80F ^= Z80_FLAG_CARRY;\n");
}
else
if (0x07 == dwOpcode) // RLCA
{
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_CARRY | Z80_FLAG_NEGATIVE | Z80_FLAG_HALF_CARRY);\n");
fprintf(fp, " cpu.z80F |= (cpu.z80A >> 7);\n");
fprintf(fp, " cpu.z80A = (cpu.z80A << 1) | (cpu.z80A >> 7);\n");
}
else
if (0x0f == dwOpcode) // RRCA
{
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_CARRY | Z80_FLAG_NEGATIVE | Z80_FLAG_HALF_CARRY);\n");
fprintf(fp, " cpu.z80F |= (cpu.z80A & Z80_FLAG_CARRY);\n");
fprintf(fp, " cpu.z80A = (cpu.z80A >> 1) | (cpu.z80A << 7);\n");
}
else
if (0x3a == dwOpcode) // LD A, (xxxxh)
{
fprintf(fp, " dwTemp = *pbPC++;\n");
fprintf(fp, " dwTemp |= (((UINT32) *pbPC++) << 8);\n");
ReadValueFromMemory("dwTemp", "cpu.z80A");
}
else
if (0xf3 == dwOpcode) // DI
{
fprintf(fp, " cpu.z80iff &= (~IFF1);\n");
}
else
if (0xfb == dwOpcode) // EI
{
fprintf(fp, " cpu.z80iff |= IFF1;\n");
}
else
if (0x00 == dwOpcode) // NOP
{
fprintf(fp, " /* Intentionally not doing anything - NOP! */\n");
}
else
{
InvalidInstructionC(1);
}
}
else
{
assert(0);
}
}
void LdRegPairImmediate(UINT32 dwOpcode)
{
UINT8 bOp = 0;
bOp = (dwOpcode >> 4) & 0x3;
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
if (bOp == 0)
fprintf(fp, " mov cx, [esi] ; Get our immediate value of BC\n");
else
if (bOp == 2)
fprintf(fp, " mov bx, [esi] ; Get our immediate value of HL\n");
else
if (bOp == 1)
{
fprintf(fp, " mov dx, [esi] ; Get our immediate value of DE\n");
fprintf(fp, " mov word [_z80de], dx ; Store DE\n");
fprintf(fp, " xor edx, edx\n");
}
else
if (bOp == 3)
{
fprintf(fp, " mov dx, [esi] ; Get our immediate value of SP\n");
fprintf(fp, " mov word [_z80sp], dx ; Store it!\n");
fprintf(fp, " xor edx, edx\n");
}
fprintf(fp, " add esi, 2\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " %s = *pbPC++; /* LSB First */\n", pbRegPairC[bOp]);
fprintf(fp, " %s |= (((UINT32) *pbPC++ << 8)); /* Now the MSB */\n", pbRegPairC[bOp]);
}
else
{
assert(0);
}
}
void LdRegpairPtrByte(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
if (dwOpcode == 0x36) // Immediate into (HL)
WriteValueToMemory("bx", "[esi]");
if (dwOpcode == 0x12)
WriteValueToMemory("[_z80de]", "al"); // (DE), A
if (dwOpcode == 0x2) // (BC), A
WriteValueToMemory("cx", "al");
if (dwOpcode >= 0x70 && dwOpcode < 0x78)
WriteValueToMemory("bx", pbMathReg[dwOpcode & 0x07]);
fprintf(fp, " xor edx, edx\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
if (dwOpcode == 0x36)
WriteValueToMemory("cpu.z80HL", "*pbPC++");
if (dwOpcode == 0x12)
WriteValueToMemory("cpu.z80DE", "cpu.z80A");
if (dwOpcode == 0x02)
WriteValueToMemory("cpu.z80BC", "cpu.z80A");
if (dwOpcode >= 0x70 && dwOpcode < 0x78)
WriteValueToMemory("cpu.z80HL", pbMathRegC[dwOpcode & 0x07]);
}
else
{
assert(0);
}
}
void MathOperation(UINT32 dwOrgOpcode)
{
UINT8 bRegister;
UINT32 dwOpcode;
UINT8 tempstr[150];
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOrgOpcode);
dwOpcode = dwOrgOpcode;
bRegister = dwOpcode & 0x07;
dwOpcode &= 0xf8;
if (dwOpcode == 0x80)
strcpy(tempstr, "add");
if (dwOpcode == 0x88)
strcpy(tempstr, "adc");
if (dwOpcode == 0x90)
strcpy(tempstr, "sub");
if (dwOpcode == 0x98)
strcpy(tempstr, "sbb");
if (dwOpcode == 0xa0)
strcpy(tempstr, "and");
if (dwOpcode == 0xa8)
strcpy(tempstr, "xor");
if (dwOpcode == 0xb0)
strcpy(tempstr, "or");
if (dwOpcode == 0xb8)
strcpy(tempstr, "cmp");
// Let's see if we have to deal with (HL) or #xxh
if (bRegister == 0x6)
{
// We have to deal with (HL)
ReadValueFromMemory("bx", "dl");
}
if (bRegister != 0x06 && bRegister < 0xff)
{
fprintf(fp, " sahf\n");
fprintf(fp, " %s al, %s\n", tempstr, pbMathReg[bRegister]);
fprintf(fp, " lahf\n");
}
else // If it's (HL)....
{
fprintf(fp, " sahf\n");
fprintf(fp, " %s al, dl\n", tempstr);
fprintf(fp, " lahf\n");
}
if (dwOpcode != 0xa8 && dwOpcode != 0xa0 && dwOpcode != 0xb0)
SetOverflow();
if (dwOpcode == 0xa8)
fprintf(fp, " and ah, 0ech ; Only these flags matter!\n");
if (dwOpcode == 0xa0)
{
fprintf(fp, " and ah, 0ech ; Only these flags matter!\n");
fprintf(fp, " or ah, 010h ; Half carry gets set\n");
}
if (dwOpcode == 0xb0)
fprintf(fp, " and ah, 0ech ; No H, N, or C\n");
if (dwOpcode == 0xb8)
fprintf(fp, " or ah, 02h ; Set N for compare!\n");
if (dwOpcode == 0x80 || dwOpcode == 0x88)
fprintf(fp, " and ah, 0fdh ; No N!\n");
if (dwOpcode == 0x90 || dwOpcode == 0x98)
fprintf(fp, " or ah, 02h ; N Gets set!\n");
if (bRegister == 0x6)
fprintf(fp, " xor edx, edx ; Zero this...\n");
FetchNextInstruction(dwOrgOpcode);
}
else
if (MZ80_C == bWhat)
{
dwOpcode = dwOrgOpcode;
bRegister = dwOpcode & 0x07;
dwOpcode &= 0xf8;
if (6 == bRegister) // Deal with (HL)
{
ReadValueFromMemory("cpu.z80HL", "bTemp");
}
if (dwOpcode == 0xa0)
{
fprintf(fp, " cpu.z80A &= %s;\n", pbMathRegC[bRegister]);
}
else
if (dwOpcode == 0xa8)
{
fprintf(fp, " cpu.z80A ^= %s;\n", pbMathRegC[bRegister]);
}
else
if (dwOpcode == 0xb0)
{
fprintf(fp, " cpu.z80A |= %s;\n", pbMathRegC[bRegister]);
}
else
if (dwOpcode == 0xb8)
{
// Don't do anything. We just do flags!
}
else
if (dwOpcode == 0x88) // ADC
{
fprintf(fp, " bTemp2 = cpu.z80A + %s + (cpu.z80F & Z80_FLAG_CARRY);\n", pbMathRegC[bRegister]);
}
else
if (dwOpcode == 0x90) // SUB
{
fprintf(fp, " bTemp2 = cpu.z80A - %s;\n", pbMathRegC[bRegister]);
}
else
if (dwOpcode == 0x80) // ADD
{
fprintf(fp, " bTemp2 = cpu.z80A + %s;\n", pbMathRegC[bRegister]);
}
else
if (dwOpcode == 0x98) // SBC
{
fprintf(fp, " bTemp2 = cpu.z80A - %s - (cpu.z80F & Z80_FLAG_CARRY);\n", pbMathRegC[bRegister]);
}
else
{
InvalidInstructionC(1);
}
// Now do flag fixup
if (0xb0 == dwOpcode || 0xa8 == dwOpcode)
{
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_CARRY | Z80_FLAG_NEGATIVE | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_HALF_CARRY | Z80_FLAG_ZERO | Z80_FLAG_SIGN);\n");
fprintf(fp, " cpu.z80F |= bPostORFlags[cpu.z80A];\n\n");
}
if (0xa0 == dwOpcode)
{
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_CARRY | Z80_FLAG_NEGATIVE | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_HALF_CARRY | Z80_FLAG_ZERO | Z80_FLAG_SIGN);\n");
fprintf(fp, " cpu.z80F |= bPostANDFlags[cpu.z80A];\n\n");
}
if (0xb8 == dwOpcode || 0x90 == dwOpcode)
{
SetSubFlagsSZHVC("cpu.z80A", pbMathRegC[bRegister]);
if (0x90 == dwOpcode)
{
fprintf(fp, " cpu.z80A = bTemp2;\n");
}
}
if (0x80 == dwOpcode) // Add fixup
{
SetAddFlagsSZHVC("cpu.z80A", pbMathRegC[bRegister]);
fprintf(fp, " cpu.z80A = bTemp2;\n");
}
if (0x88 == dwOpcode) // Adc fixup
{
SetAdcFlagsSZHVC("cpu.z80A", pbMathRegC[bRegister]);
fprintf(fp, " cpu.z80A = bTemp2;\n");
}
if (0x98 == dwOpcode) // Sbc fixup
{
SetSbcFlagsSZHVC("cpu.z80A", pbMathRegC[bRegister]);
fprintf(fp, " cpu.z80A = bTemp2;\n");
}
}
else
{
assert(0);
}
}
void RegIntoMemory(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
fprintf(fp, " mov dx, [esi] ; Get our address to write to\n");
fprintf(fp, " add esi, 2 ; Next address, please...\n");
if (0x32 == dwOpcode) // LD (xxxx), A
WriteValueToMemory("dx", "al");
if (0x22 == dwOpcode) // LD (xxxx), HL
{
WriteWordToMemory("dx", "bx");
}
fprintf(fp, " xor edx, edx ; Zero our upper byte\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " dwTemp = *pbPC++;\n");
fprintf(fp, " dwTemp |= ((UINT32) *pbPC++ << 8);\n");
if (0x32 == dwOpcode)
WriteValueToMemory("dwTemp", "cpu.z80A");
if (0x22 == dwOpcode)
WriteWordToMemory("dwTemp", "cpu.z80HL");
return;
}
else
{
assert(0);
}
}
void JpHandler(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
if (0xc3 == dwOpcode) // If it's a straight jump...
{
fprintf(fp, " mov si, [esi] ; Get our new address\n");
fprintf(fp, " and esi, 0ffffh ; Only the lower 16 bits\n");
fprintf(fp, " add esi, ebp ; Our new address!\n");
}
else // It's a conditional handler...
{
fprintf(fp, " sahf ; Restore our flags\n");
fprintf(fp, " j%s takeJump%ld ; We're going to take a jump\n", pbFlags[(dwOpcode >> 3) & 0x07], dwGlobalLabel);
fprintf(fp, " add esi, 2 ; Skip past the address\n");
fprintf(fp, " jmp short nextInst%ld ; Go execute the next instruction\n", dwGlobalLabel);
fprintf(fp, "takeJump%ld:\n", dwGlobalLabel);
fprintf(fp, " mov si, [esi] ; Get our new offset\n");
fprintf(fp, " and esi, 0ffffh ; Only the lower WORD is valid\n");
fprintf(fp, " add esi, ebp ; Our new address!\n");
fprintf(fp, "nextInst%ld:\n", dwGlobalLabel);
++dwGlobalLabel;
}
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " dwAddr = *pbPC++; /* Get LSB first */\n");
fprintf(fp, " dwAddr |= ((UINT32) *pbPC++ << 8); /* Get MSB last */\n");
if (0xc3 != dwOpcode)
{
fprintf(fp, " if %s\n", pbFlagsC[(dwOpcode >> 3) & 0x07]);
fprintf(fp, " {\n");
fprintf(fp, " pbPC = cpu.z80Base + dwAddr; /* Normalize the address */\n");
fprintf(fp, " }\n");
}
else // Regular jump here
{
fprintf(fp, " pbPC = cpu.z80Base + dwAddr; /* Normalize the address */\n");
}
}
else
{
assert(0);
}
}
void LdRegImmediate(UINT32 dwOpcode)
{
UINT8 bOp;
bOp = (dwOpcode >> 3) & 0x7;
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
if (bOp != 2 && bOp != 3)
fprintf(fp, " mov %s, [esi] ; Get our immediate value\n", pbMathReg[bOp]);
else
{
fprintf(fp, " mov dl, [esi] ; Get our immediate value\n");
fprintf(fp, " mov %s, dl ; Store our new value\n", pbMathReg[bOp]);
}
fprintf(fp, " inc esi\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " %s = *pbPC++; /* Get immediate byte into register */\n", pbMathRegC[bOp]);
}
else
{
assert(0);
}
}
void IncRegister(UINT32 dwOpcode)
{
UINT32 dwOpcode1 = 0;
dwOpcode1 = (dwOpcode >> 3) & 0x07;
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
fprintf(fp, " sahf\n");
fprintf(fp, " inc %s\n", pbMathReg[dwOpcode1]);
fprintf(fp, " lahf\n");
SetOverflow();
fprintf(fp, " and ah, 0fdh ; Knock out N!\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_SIGN | Z80_FLAG_ZERO | Z80_FLAG_HALF_CARRY | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_NEGATIVE);\n");
fprintf(fp ," cpu.z80F |= bPostIncFlags[%s++];\n", pbMathRegC[dwOpcode1]);
}
else
{
assert(0);
}
}
void DecRegister(UINT32 dwOpcode)
{
UINT32 dwOpcode1 = 0;
dwOpcode1 = (dwOpcode >> 3) & 0x07;
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
fprintf(fp, " sahf\n");
fprintf(fp, " dec %s\n", pbMathReg[dwOpcode1]);
fprintf(fp, " lahf\n");
SetOverflow();
fprintf(fp, " or ah, 02h ; Set negative!\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_SIGN | Z80_FLAG_ZERO | Z80_FLAG_HALF_CARRY | Z80_FLAG_OVERFLOW_PARITY);\n");
fprintf(fp ," cpu.z80F |= bPostDecFlags[%s--];\n", pbMathRegC[dwOpcode1]);
}
else
{
assert(0);
}
}
void IncDecRegpair(UINT32 dwOpcode)
{
UINT32 dwOpcode1 = 0;
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
if ((dwOpcode & 0x0f) == 3) // Increment?
fprintf(fp, " inc %s\n", pbRegPairs[(dwOpcode >> 4) & 0x03]);
else
fprintf(fp, " dec %s\n", pbRegPairs[(dwOpcode >> 4) & 0x03]);
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
if ((dwOpcode & 0x0f) == 3) // Increment
fprintf(fp, " %s++;\n", pbRegPairC[(dwOpcode >> 4) & 0x03]);
else
fprintf(fp, " %s--;\n", pbRegPairC[(dwOpcode >> 4) & 0x03]);
fprintf(fp, " %s &= 0xffff;\n", pbRegPairC[(dwOpcode >> 4) & 0x03]);
}
else
{
assert(0);
}
}
void LdRegReg(UINT32 dwOpcode)
{
UINT8 bDestination;
UINT8 bSource;
bDestination = (dwOpcode >> 3) & 0x07;
bSource = (dwOpcode) & 0x07;
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
if (bSource != bDestination)
{
if (bSource == 2 && bDestination == 3)
{
fprintf(fp, " mov dl, byte [_z80de + 1]\n");
fprintf(fp, " mov [_z80de], dl\n");
}
else
if (bSource == 3 && bDestination == 2)
{
fprintf(fp, " mov dl, byte [_z80de]\n");
fprintf(fp, " mov [_z80de + 1], dl\n");
}
else
fprintf(fp, " mov %s, %s\n", pbMathReg[bDestination], pbMathReg[bSource]);
}
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
if (bDestination != bSource)
{
fprintf(fp, " %s = %s;\n",
pbMathRegC[bDestination],
pbMathRegC[bSource]);
}
}
else
{
assert(0);
}
}
void MathOperationDirect(UINT32 dwOpcode)
{
UINT8 tempstr[4];
if (MZ80_ASSEMBLY_X86 == bWhat)
{
if (dwOpcode == 0xc6)
strcpy(tempstr, "add");
if (dwOpcode == 0xce)
strcpy(tempstr, "adc");
if (dwOpcode == 0xd6)
strcpy(tempstr, "sub");
if (dwOpcode == 0xde)
strcpy(tempstr, "sbb");
if (dwOpcode == 0xe6)
strcpy(tempstr, "and");
if (dwOpcode == 0xee)
strcpy(tempstr, "xor");
if (dwOpcode == 0xf6)
strcpy(tempstr, "or");
if (dwOpcode == 0xfe)
strcpy(tempstr, "cmp");
ProcBegin(dwOpcode);
// Let's see if we have to deal with (HL) or #xxh
fprintf(fp, " sahf\n");
fprintf(fp, " %s al, [esi]\n", tempstr);
fprintf(fp, " lahf\n");
if (dwOpcode != 0xee && dwOpcode != 0xe6 && dwOpcode != 0xf6)
{
SetOverflow();
}
if (dwOpcode == 0xe6)
{
fprintf(fp, " and ah, 0ech ; Only parity, half carry, sign, zero\n");
fprintf(fp, " or ah, 10h ; Half carry\n");
}
if (dwOpcode == 0xc6 || dwOpcode == 0xce)
fprintf(fp, " and ah, 0fdh ; Knock out N!\n");
if (dwOpcode == 0xd6 || dwOpcode == 0xde || dwOpcode == 0xfe)
fprintf(fp, " or ah, 02h ; Set negative!\n");
if (dwOpcode == 0xf6 || dwOpcode == 0xee)
fprintf(fp, " and ah, 0ech ; No H, N, or C\n");
fprintf(fp, " inc esi\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
if (0xfe == dwOpcode) // Cp
{
SetSubFlagsSZHVC("cpu.z80A", "*pbPC++");
}
else
if (0xe6 == dwOpcode) // And
{
fprintf(fp, " cpu.z80A &= *pbPC++;\n");
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_CARRY | Z80_FLAG_NEGATIVE | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_HALF_CARRY | Z80_FLAG_ZERO | Z80_FLAG_SIGN);\n");
fprintf(fp, " cpu.z80F |= bPostANDFlags[cpu.z80A];\n\n");
}
else
if (0xf6 == dwOpcode) // Or
{
fprintf(fp, " cpu.z80A |= *pbPC++;\n");
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_CARRY | Z80_FLAG_NEGATIVE | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_HALF_CARRY | Z80_FLAG_ZERO | Z80_FLAG_SIGN);\n");
fprintf(fp, " cpu.z80F |= bPostORFlags[cpu.z80A];\n\n");
}
else
if (0xc6 == dwOpcode) // Add
{
fprintf(fp, " bTemp = *pbPC++;\n");
SetAddFlagsSZHVC("cpu.z80A", "bTemp");
fprintf(fp, " cpu.z80A += bTemp;\n");
}
else
if (0xce == dwOpcode) // Adc
{
fprintf(fp, " bTemp = *pbPC++ + (cpu.z80F & Z80_FLAG_CARRY);\n");
SetAdcFlagsSZHVC("cpu.z80A", "bTemp");
fprintf(fp, " cpu.z80A += bTemp;\n");
}
else
if (0xd6 == dwOpcode) // Sub
{
fprintf(fp, " bTemp = *pbPC++;\n");
SetSubFlagsSZHVC("cpu.z80A", "bTemp");
fprintf(fp, " cpu.z80A -= bTemp;\n");
}
else
if (0xde == dwOpcode) // Sbc
{
fprintf(fp, " bTemp = *pbPC++ + (cpu.z80F & Z80_FLAG_CARRY);\n");
SetSbcFlagsSZHVC("cpu.z80A", "bTemp");
fprintf(fp, " cpu.z80A = cpu.z80A - bTemp;\n");
}
else
if (0xee == dwOpcode) // Xor
{
fprintf(fp, " cpu.z80A ^= *pbPC++;\n");
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_CARRY | Z80_FLAG_NEGATIVE | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_HALF_CARRY | Z80_FLAG_ZERO | Z80_FLAG_SIGN);\n");
fprintf(fp, " cpu.z80F |= bPostORFlags[cpu.z80A];\n\n");
}
else
InvalidInstructionC(1);
}
else
{
assert(0);
}
}
// JR cc, addr
void JrHandler(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
fprintf(fp, " sub esi, ebp\n");
fprintf(fp, " and esi, 0ffffh\n");
fprintf(fp, " add esi, ebp\n");
fprintf(fp, " mov dl, [esi] ; Get our relative offset\n");
fprintf(fp, " inc esi ; Next instruction, please!\n");
if (dwOpcode != 0x18)
{
fprintf(fp, " sahf\n");
fprintf(fp, " j%s takeJump%ld\n", pbFlags[(dwOpcode >> 3) & 0x3], dwGlobalLabel);
fprintf(fp, " jmp short noJumpMan%ld\n", dwGlobalLabel);
fprintf(fp, "takeJump%ld:\n", dwGlobalLabel);
if (FALSE == bNoTiming)
{
fprintf(fp, " sub edi, 5\n");
}
}
else // It's a JR
{
fprintf(fp, " cmp dl, 0feh ; Jump to self?\n");
fprintf(fp, " je yesJrMan ; Yup! Bail out!\n");
}
fprintf(fp, " xchg eax, edx\n");
fprintf(fp, " cbw\n");
fprintf(fp, " xchg eax, edx\n");
fprintf(fp, " sub esi, ebp\n");
fprintf(fp, " add si, dx\n");
fprintf(fp, " and esi, 0ffffh ; Only the lower 16 bits\n");
fprintf(fp, " add esi, ebp\n");
fprintf(fp, " xor dh, dh\n");
fprintf(fp, "noJumpMan%ld:\n", dwGlobalLabel++);
FetchNextInstruction(dwOpcode);
if (0x18 == dwOpcode)
{
fprintf(fp,"yesJrMan:\n");
fprintf(fp, " xor edx, edx ; Zero me for later\n");
fprintf(fp, " mov edi, edx\n");
fprintf(fp, " mov [cyclesRemaining], edx\n");
fprintf(fp, " sub esi, 2 ; Back to the instruction again\n");
fprintf(fp, " jmp noMoreExec\n\n");
}
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " sdwAddr = (INT8) *pbPC++; /* Get LSB first */\n");
fprintf(fp, " cpu.z80pc = (UINT32) pbPC - (UINT32) cpu.z80Base;\n");
fprintf(fp, " sdwAddr = (sdwAddr + (INT32) cpu.z80pc) & 0xffff;\n");
if (0x18 != dwOpcode)
{
fprintf(fp, " if %s\n", pbFlagsC[(dwOpcode >> 3) & 0x03]);
}
fprintf(fp, " {\n");
fprintf(fp, " sdwCyclesRemaining -= 5;\n");
fprintf(fp, " pbPC = cpu.z80Base + sdwAddr; /* Normalize the address */\n");
fprintf(fp, " }\n");
}
else
{
assert(0);
}
}
void CallHandler(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
if (dwOpcode != 0xcd)
{
fprintf(fp, " sahf ; Restore our flags\n");
fprintf(fp, " j%s takeJump%ld ; We're going call in this case\n", pbFlags[(dwOpcode >> 3) & 0x07], dwGlobalLabel);
fprintf(fp, " add esi, 2 ; Skip past the address\n");
fprintf(fp, " jmp short noCallTaken%ld ; Go execute the next instruction\n", dwGlobalLabel);
fprintf(fp, "takeJump%ld:\n", dwGlobalLabel);
fprintf(fp, " sub edi, 7\n");
}
if (bThroughCallHandler)
{
fprintf(fp, " mov dx, [esi] ; Get our call to address\n");
fprintf(fp, " mov [_z80pc], dx ; Store our new program counter\n");
fprintf(fp, " add esi, 2 ; Skip to our new address to be pushed\n");
fprintf(fp, " sub esi, ebp ; Value to push onto the \"stack\"\n");
fprintf(fp, " mov [_wordval], si ; Store our return address on the stack\n");
fprintf(fp, " mov si, dx ; Our new address\n");
fprintf(fp, " add esi, ebp ; And our base address\n");
fprintf(fp, " call PushWord ; Go push our orgval to the stack\n");
}
else
{
fprintf(fp, " mov dx, [esi] ; Get our call to address\n");
fprintf(fp, " mov [_z80pc], dx ; Store our new program counter\n");
fprintf(fp, " add esi, 2 ; Skip to our new address to be pushed\n");
fprintf(fp, " sub esi, ebp ; Value to push onto the \"stack\"\n");
fprintf(fp, " mov dx, word [_z80sp] ; Get the current stack pointer\n");
fprintf(fp, " sub dx, 2 ; Back up two bytes\n");
fprintf(fp, " mov [ebp+edx], si ; PUSH It!\n");
fprintf(fp, " mov word [_z80sp], dx ; Store our new stack pointer\n");
fprintf(fp, " mov si, [_z80pc] ; Get our new program counter\n");
fprintf(fp, " add esi, ebp ; Naturalize it!\n");
}
if (dwOpcode != 0xcd)
fprintf(fp, "noCallTaken%ld:\n", dwGlobalLabel++);
fprintf(fp, " xor edx, edx\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " dwAddr = *pbPC++; /* Get LSB first */\n");
fprintf(fp, " dwAddr |= ((UINT32) *pbPC++ << 8); /* Get MSB last */\n");
if (0xcd != dwOpcode)
{
fprintf(fp, " if %s\n", pbFlagsC[(dwOpcode >> 3) & 0x07]);
fprintf(fp, " {\n");
fprintf(fp, " cpu.z80pc = (UINT32) pbPC - (UINT32) cpu.z80Base;\n");
fprintf(fp, " pbSP = (cpu.z80Base + cpu.z80sp - 1); /* Normalize the stack pointer */\n");
fprintf(fp, " *pbSP-- = cpu.z80pc >> 8; /* MSB */\n");
fprintf(fp, " *pbSP = (UINT8) cpu.z80pc; /* LSB */\n");
fprintf(fp, " cpu.z80sp -= 2; /* Back our stack up */\n");
fprintf(fp, " pbPC = cpu.z80Base + dwAddr; /* Normalize the address */\n");
fprintf(fp, " }\n");
}
else // Just a regular call
{
fprintf(fp, " cpu.z80pc = (UINT32) pbPC - (UINT32) cpu.z80Base;\n");
fprintf(fp, " pbSP = (cpu.z80Base + cpu.z80sp - 1); /* Normalize the stack pointer */\n");
fprintf(fp, " *pbSP-- = cpu.z80pc >> 8; /* LSB */\n");
fprintf(fp, " *pbSP = (UINT8) cpu.z80pc; /* MSB */\n");
fprintf(fp, " cpu.z80sp -= 2; /* Back our stack up */\n");
fprintf(fp, " pbPC = cpu.z80Base + dwAddr; /* Normalize the address */\n");
}
}
else
{
assert(0);
}
}
void RetHandler(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
if (dwOpcode != 0xc9)
{
fprintf(fp, " sahf\n");
fprintf(fp, " j%s takeReturn%ld\n", pbFlags[(dwOpcode >> 3) & 0x07], dwGlobalLabel);
fprintf(fp, " jmp short retNotTaken%ld\n", dwGlobalLabel);
fprintf(fp, "takeReturn%ld:\n", dwGlobalLabel);
if (FALSE == bNoTiming)
{
fprintf(fp, " sub edi, byte 6\n");
}
}
if (bThroughCallHandler)
{
fprintf(fp, " call PopWord\n");
fprintf(fp, " xor esi, esi\n");
fprintf(fp, " mov si, dx\n");
fprintf(fp, " add esi, ebp\n");
fprintf(fp, " xor edx, edx\n");
}
else
{
fprintf(fp, " mov dx, word [_z80sp] ; Get our current stack pointer\n");
fprintf(fp, " mov si, [edx+ebp] ; Get our return address\n");
fprintf(fp, " and esi, 0ffffh ; Only within 64K!\n");
fprintf(fp, " add esi, ebp ; Add in our base address\n");
fprintf(fp, " add word [_z80sp], 02h ; Remove our two bytes from the stack\n");
fprintf(fp, " xor edx, edx\n");
}
if (dwOpcode != 0xc9)
fprintf(fp, "retNotTaken%ld:\n", dwGlobalLabel++);
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
if (dwOpcode != 0xc9)
{
fprintf(fp, " if %s\n", pbFlagsC[(dwOpcode >> 3) & 0x07]);
fprintf(fp, " {\n");
fprintf(fp, " dwElapsedTicks += 6;\n");
}
fprintf(fp, " pbSP = cpu.z80Base + cpu.z80sp; /* Normalize our stack PTR */\n");
fprintf(fp, " dwAddr = *pbSP++; /* Pop LSB */\n");
fprintf(fp, " dwAddr |= ((UINT32) *pbSP << 8); /* Pop MSB */\n");
fprintf(fp, " cpu.z80sp += 2; /* Pop the word off */\n");
fprintf(fp, " pbPC = (cpu.z80Base + dwAddr); /* Point PC to our return address */\n");
if (dwOpcode != 0xc9)
{
fprintf(fp, " }\n");
}
}
else
{
assert(0);
}
}
void RestartHandler(UINT32 dwOpcode)
{
UINT32 dwOpcode1 = 0;
dwOpcode1 = dwOpcode & 0x38;
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
if (bThroughCallHandler)
{
fprintf(fp, " sub esi, ebp\n");
fprintf(fp, " mov [_wordval], si ; Store our return address\n");
fprintf(fp, " call PushWord\n");
fprintf(fp, " xor esi, esi\n");
fprintf(fp, " mov si, %.4lxh\n", dwOpcode1);
fprintf(fp, " add esi, ebp\n");
}
else
{
fprintf(fp, " mov dx, word [_z80sp] ; Get our stack pointer\n");
fprintf(fp, " sub dx, 2 ; Make room for the new value!\n");
fprintf(fp, " mov word [_z80sp], dx ; Store our new stack pointer\n");
fprintf(fp, " sub esi, ebp ; Get our real PC\n");
fprintf(fp, " mov [ebp+edx], si ; Our return address\n");
fprintf(fp, " mov si, 0%.2xh ; Our new call address\n", dwOpcode1);
fprintf(fp, " add esi, ebp ; Back to the base!\n");
}
fprintf(fp, " xor edx, edx\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " cpu.z80pc = (UINT32) pbPC - (UINT32) cpu.z80Base;\n");
fprintf(fp, " pbSP = (cpu.z80Base + cpu.z80sp - 1); /* Normalize the stack pointer */\n");
fprintf(fp, " *pbSP-- = cpu.z80pc >> 8; /* LSB */\n");
fprintf(fp, " *pbSP = (UINT8) cpu.z80pc; /* MSB */\n");
fprintf(fp, " cpu.z80sp -= 2; /* Back our stack up */\n");
fprintf(fp, " pbPC = cpu.z80Base + 0x%.2x; /* Normalize the address */\n", dwOpcode1);
}
else
{
assert(0);
}
}
void ToRegFromHl(UINT32 dwOpcode)
{
UINT8 bReg;
bReg = (dwOpcode >> 3) & 0x07;
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
if (bReg != 2 && bReg != 3)
ReadValueFromMemory("bx", pbMathReg[bReg]);
else
{
ReadValueFromMemory("bx", pbLocalReg[bReg]);
fprintf(fp, " mov %s, %s\n", pbMathReg[bReg], pbLocalReg[bReg]);
}
fprintf(fp, " xor edx, edx\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
ReadValueFromMemory("cpu.z80HL", pbLocalRegC[bReg]);
}
else
{
assert(0);
}
}
void AddRegpairOperations(UINT32 dwOpcode)
{
UINT8 bRegpair;
bRegpair = (dwOpcode >> 4) & 0x3;
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
fprintf(fp, " mov dh, ah ; Get our flags\n");
fprintf(fp, " and dh, 0ech ; Preserve the top three and bits 2 & 3\n");
fprintf(fp, " mov [_orgval], bx ; Store our original value\n");
fprintf(fp, " add bx, %s\n", pbRegPairs[bRegpair]);
fprintf(fp, " lahf\n");
fprintf(fp, " mov [cyclesRemaining], edi\n");
fprintf(fp, " mov di, [_orgval] ; Get original\n");
fprintf(fp, " xor di, bx ; XOR It with our computed value\n");
fprintf(fp, " xor di, %s\n", pbRegPairs[bRegpair]);
fprintf(fp, " and di, 1000h ; Just our half carry\n");
fprintf(fp, " or dx, di ; Or in our flags\n");
fprintf(fp, " and ah, 01h ; Just carry\n");
fprintf(fp, " or ah, dh\n");
fprintf(fp, " mov edi, [cyclesRemaining]\n");
fprintf(fp, " xor edx, edx\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_CARRY | Z80_FLAG_NEGATIVE | Z80_FLAG_HALF_CARRY);\n");
fprintf(fp, " dwTemp = cpu.z80HL + %s;\n", pbRegPairsC[bRegpair]);
fprintf(fp, " cpu.z80F |= ((dwTemp >> 16) & Z80_FLAG_CARRY) | (((cpu.z80HL ^ dwTemp ^ %s) >> 8) & Z80_FLAG_HALF_CARRY);\n", pbRegPairsC[bRegpair]);
fprintf(fp, " cpu.z80HL = dwTemp & 0xffff;\n");
return;
}
else
{
assert(0);
}
}
void PushPopOperations(UINT32 dwOpcode)
{
UINT8 bRegPair;
bRegPair = ((dwOpcode >> 4) & 0x3) << 1;
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
if ((dwOpcode & 0xcf) == 0xc5) // Push
{
fprintf(fp, " sub word [_z80sp], 2\n");
fprintf(fp, " mov dx, [_z80sp]\n");
WriteWordToMemory("dx", pbPopRegPairs[bRegPair >> 1]);
}
else // Pop
{
fprintf(fp, " mov dx, [_z80sp]\n");
ReadWordFromMemory("dx", pbPopRegPairs[bRegPair >> 1]);
fprintf(fp, " add word [_z80sp], 2\n");
}
fprintf(fp, " xor edx, edx\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
if ((dwOpcode & 0xcf) == 0xc5) // Push?
{
fprintf(fp, " cpu.z80sp -= 2;\n");
fprintf(fp, " pbSP = (cpu.z80Base + cpu.z80sp); /* Normalize the stack pointer */\n");
WriteWordToMemory("cpu.z80sp", pbPopRegPairC[bRegPair >> 1]);
return;
}
else
{
ReadWordFromMemory("cpu.z80sp", pbPopRegPairC[bRegPair >> 1]);
fprintf(fp, " cpu.z80sp += 2;\n");
fprintf(fp, " pbSP = (cpu.z80Base + cpu.z80sp); /* Normalize the stack pointer */\n");
return;
}
InvalidInstructionC(1);
}
else
{
assert(0);
}
}
void RraRlaHandler(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
fprintf(fp, " sahf\n");
if (dwOpcode == 0x1f)
fprintf(fp, " rcr al, 1\n");
else
fprintf(fp, " rcl al, 1\n");
fprintf(fp, " lahf\n");
fprintf(fp, " and ah, 0edh\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
if (0x1f == dwOpcode) // RRA
{
fprintf(fp, " bTemp = (cpu.z80F & Z80_FLAG_CARRY) << 7;\n");
fprintf(fp, " cpu.z80F = (cpu.z80F & ~(Z80_FLAG_CARRY | Z80_FLAG_NEGATIVE | Z80_FLAG_HALF_CARRY)) | (cpu.z80A & Z80_FLAG_CARRY);\n");
fprintf(fp, " cpu.z80A = ((cpu.z80A >> 1) | bTemp);\n");
}
else // RLA
{
fprintf(fp, " bTemp = cpu.z80A >> 7;\n");
fprintf(fp, " cpu.z80A = (cpu.z80A << 1) | (cpu.z80F & Z80_FLAG_CARRY);\n");
fprintf(fp, " cpu.z80F = (cpu.z80F & ~(Z80_FLAG_CARRY | Z80_FLAG_NEGATIVE | Z80_FLAG_HALF_CARRY)) | bTemp;\n");
}
}
else
{
assert(0);
}
}
void LdByteRegpair(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
if (dwOpcode == 0x0a)
ReadValueFromMemory("cx", "al");
if (dwOpcode == 0x1a)
{
fprintf(fp, " mov dx, [_z80de]\n");
ReadValueFromMemory("dx", "al");
}
fprintf(fp, " xor edx, edx\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
if (dwOpcode == 0x0a)
ReadValueFromMemory("cpu.z80BC", "cpu.z80A");
if (dwOpcode == 0x1a)
ReadValueFromMemory("cpu.z80DE", "cpu.z80A");
}
else
{
assert(0);
}
}
void IncDecHLPtr(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
ReadValueFromMemory("bx", "dl");
fprintf(fp, " sahf\n");
if (dwOpcode == 0x34)
fprintf(fp, " inc dl\n");
else
fprintf(fp, " dec dl\n");
fprintf(fp, " lahf\n");
fprintf(fp, " o16 pushf\n");
fprintf(fp, " shl edx, 16\n");
fprintf(fp, " and ah, 0fbh ; Knock out parity/overflow\n");
fprintf(fp, " pop dx\n");
fprintf(fp, " and dh, 08h ; Just the overflow\n");
fprintf(fp, " shr dh, 1 ; Shift it into position\n");
fprintf(fp, " or ah, dh ; OR It in with the real flags\n");
fprintf(fp, " shr edx, 16\n");
if (dwOpcode == 0x34)
fprintf(fp, " and ah, 0fdh ; Knock out N!\n");
else
fprintf(fp, " or ah, 02h ; Make it N!\n");
WriteValueToMemory("bx", "dl");
fprintf(fp, " xor edx, edx\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
ReadValueFromMemory("cpu.z80HL", "bTemp");
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_SIGN | Z80_FLAG_ZERO | Z80_FLAG_HALF_CARRY | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_NEGATIVE);\n");
if (0x34 == dwOpcode)
fprintf(fp ," cpu.z80F |= bPostIncFlags[bTemp];\n");
else
fprintf(fp ," cpu.z80F |= bPostDecFlags[bTemp];\n");
if (0x34 == dwOpcode)
fprintf(fp, " bTemp++;\n");
else
fprintf(fp, " bTemp--;\n");
WriteValueToMemory("cpu.z80HL", "bTemp");
return;
}
else
{
assert(0);
}
}
void InOutHandler(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
fprintf(fp, " mov dl, [esi] ; Get our address to 'out' to\n");
fprintf(fp, " inc esi ; Next address\n");
if (b16BitIo)
{
fprintf(fp, " mov dh, al ; Upper 8 bits are the A register for 16 bit addressing\n");
}
if (0xd3 == dwOpcode)
WriteValueToIo("dx", "al");
else
ReadValueFromIo("dx", "al");
fprintf(fp, " xor edx, edx\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
fprintf(fp ," dwTemp = *pbPC++;\n");
if (0xd3 == dwOpcode)
WriteValueToIo("dwTemp", "cpu.z80A");
else
ReadValueFromIo("dwTemp", "cpu.z80A");
// Not supposed to set flags for immediate instruction!
return;
}
else
{
assert(0);
}
}
// CB Area
void RESSETHandler(UINT32 dwOpcode)
{
UINT8 op = 0;
op = dwOpcode & 0x07;
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
if ((2 == op) || (3 == op))
fprintf(fp, " mov dx, [_z80de] ; Move DE into something half usable\n");
if ((dwOpcode & 0x07) == 6) // (HL)?
ReadValueFromMemory("bx", "dl");
if ((dwOpcode & 0xc0) == 0x80)
fprintf(fp, " and %s, 0%.2xh ; Reset a bit\n",
pbLocalReg[op],
0xff - (1 << ((dwOpcode >> 3) & 0x7)));
if ((dwOpcode & 0xc0) == 0xc0)
fprintf(fp, " or %s, 0%.2xh ; Set a bit\n",
pbLocalReg[op],
(1 << ((dwOpcode >> 3) & 0x7)));
if ((2 == op) || (3 == op))
{
fprintf(fp, " mov [_z80de], dx ; Once modified, put it back\n");
fprintf(fp, " xor edx, edx\n");
}
if ((dwOpcode & 0x07) == 6) // (HL)?
{
WriteValueToMemory("bx", "dl");
fprintf(fp, " xor edx, edx\n");
}
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
if (6 == op) // (HL)?
ReadValueFromMemory("cpu.z80HL", "bTemp");
if ((dwOpcode & 0xc0) == 0x80) // RES
fprintf(fp, " %s &= 0x%.2x;\n", pbMathRegC[op], (UINT8) ~((UINT8) 1 << ((dwOpcode >> 3) & 0x07)));
else // SET
fprintf(fp, " %s |= 0x%.2x;\n", pbMathRegC[op], 1 << ((dwOpcode >> 3) & 0x07));
if (6 == op) // (HL)?
WriteValueToMemory("cpu.z80HL", "bTemp");
}
else
assert(0);
}
void BITHandler(UINT32 dwOpcode)
{
UINT8 op = 0;
UINT8 bBitVal = 0;
op = dwOpcode & 0x07;
bBitVal = 1 << ((dwOpcode >> 3) & 0x07);
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
if ((dwOpcode & 0x07) == 6) // (HL)?
ReadValueFromMemory("bx", "dl");
fprintf(fp, " mov byte [_z80af], ah ; Store F\n");
fprintf(fp, " sahf\n");
if ((dwOpcode & 0x07) == 6)
fprintf(fp, " test dl, 0%.2xh ; Do a bitwise check\n", 1 << ((dwOpcode >> 3) & 0x7));
else
fprintf(fp, " test %s, 0%.2xh ; Do a bitwise check\n", pbMathReg[op], 1 << ((dwOpcode >> 3) & 0x7));
fprintf(fp, " lahf\n");
fprintf(fp, " and ah, 0c0h ; Only care about Z and S\n");
fprintf(fp, " or ah, 10h ; Set half carry to 1\n");
fprintf(fp, " and byte [_z80af], 029h ; Only zero/non-zero!\n");
fprintf(fp, " or ah, byte [_z80af] ; Put it in with the real flags\n");
if (6 == (dwOpcode & 0x07)) // (HL)?
fprintf(fp, " xor edx, edx\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
if (6 == op) // (HL)?
ReadValueFromMemory("cpu.z80HL", "bTemp");
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_HALF_CARRY | Z80_FLAG_NEGATIVE | Z80_FLAG_ZERO);\n");
fprintf(fp, " cpu.z80F |= (Z80_FLAG_HALF_CARRY);\n");
fprintf(fp, " if (!(%s & 0x%.2lx))\n", pbMathRegC[op], bBitVal);
fprintf(fp, " {\n");
fprintf(fp, " cpu.z80F |= Z80_FLAG_ZERO;\n");
fprintf(fp, " }\n");
}
else
assert(0);
}
void RLCRRCRLRRSLASRASRLHandler(UINT32 dwOpcode)
{
UINT8 op = 0;
op = dwOpcode & 0x07;
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
if ((2 == op) || (3 == op))
fprintf(fp, " mov dx, [_z80de] ; Move DE into something half usable\n");
if ((dwOpcode & 0x07) == 6) // (HL)?
ReadValueFromMemory("bx", "dl");
fprintf(fp, " sahf\n");
if ((dwOpcode & 0xf8) == 0)
fprintf(fp, " rol %s, 1\n", pbLocalReg[op]);
else
if ((dwOpcode & 0xf8) == 0x08)
fprintf(fp, " ror %s, 1\n", pbLocalReg[op]);
else
if ((dwOpcode & 0xf8) == 0x10)
fprintf(fp, " rcl %s, 1\n", pbLocalReg[op]);
else
if ((dwOpcode & 0xf8) == 0x18)
fprintf(fp, " rcr %s, 1\n", pbLocalReg[op]);
else
if ((dwOpcode & 0xf8) == 0x20 || (dwOpcode & 0xf8) == 0x30)
fprintf(fp, " shl %s, 1\n", pbLocalReg[op]);
else
if ((dwOpcode & 0xf8) == 0x28)
fprintf(fp, " sar %s, 1\n", pbLocalReg[op]);
else
if ((dwOpcode & 0xf8) == 0x38)
fprintf(fp, " shr %s, 1\n", pbLocalReg[op]);
else
assert(0);
fprintf(fp, " lahf\n");
if ((dwOpcode & 0xf8) >= 0x20)
{
if ((dwOpcode & 0xf8) == 0x30)
fprintf(fp, " or %s, 1 ; Slide in a 1 bit (SLIA)\n", pbLocalReg[op]);
fprintf(fp, " and ah, 0edh ; Clear H and N\n");
}
else
{
fprintf(fp, " and ah, 029h ; Clear H and N\n");
fprintf(fp, " mov byte [_z80af], ah\n");
fprintf(fp, " or %s, %s\n", pbLocalReg[op], pbLocalReg[op]);
fprintf(fp, " lahf\n");
fprintf(fp, " and ah, 0c4h ; Sign, zero, and parity\n");
fprintf(fp, " or ah, byte [_z80af]\n");
}
if ((2 == op) || (3 == op))
{
fprintf(fp, " mov [_z80de], dx ; Once modified, put it back\n");
fprintf(fp, " xor edx, edx\n");
}
if ((dwOpcode & 0x07) == 6) // (HL)?
{
WriteValueToMemory("bx", "dl");
fprintf(fp, " xor edx, edx\n");
}
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
if (6 == op) // (HL)?
ReadValueFromMemory("cpu.z80HL", "bTemp");
dwOpcode &= 0xf8; // Just the instruction
if (0 == dwOpcode) // RLC
{
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_ZERO | Z80_FLAG_SIGN | Z80_FLAG_HALF_CARRY | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_NEGATIVE | Z80_FLAG_CARRY);\n");
fprintf(fp, " bTemp2 = (%s >> 7);\n", pbMathRegC[op]);
fprintf(fp, " %s = (%s << 1) | bTemp2;\n", pbMathRegC[op], pbMathRegC[op]);
fprintf(fp, " cpu.z80F |= bTemp2 | bPostORFlags[%s];\n", pbMathRegC[op]);
}
else
if (0x08 == dwOpcode) // RRC
{
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_ZERO | Z80_FLAG_SIGN | Z80_FLAG_HALF_CARRY | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_NEGATIVE | Z80_FLAG_CARRY);\n");
fprintf(fp, " cpu.z80F |= (%s & Z80_FLAG_CARRY);\n", pbMathRegC[op]);
fprintf(fp, " %s = (%s >> 1) | (%s << 7);\n", pbMathRegC[op], pbMathRegC[op], pbMathRegC[op]);
fprintf(fp, " cpu.z80F |= bPostORFlags[%s];\n", pbMathRegC[op]);
}
else
if (0x10 == dwOpcode) // RL
{
fprintf(fp, " bTemp2 = cpu.z80F & Z80_FLAG_CARRY;\n");
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_ZERO | Z80_FLAG_SIGN | Z80_FLAG_HALF_CARRY | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_NEGATIVE | Z80_FLAG_CARRY);\n");
fprintf(fp, " cpu.z80F |= (%s >> 7);\n", pbMathRegC[op]);
fprintf(fp, " %s = (%s << 1) | bTemp2;\n", pbMathRegC[op], pbMathRegC[op]);
fprintf(fp, " cpu.z80F |= bPostORFlags[%s];\n", pbMathRegC[op]);
}
else
if (0x18 == dwOpcode) // RR
{
fprintf(fp, " bTemp2 = (cpu.z80F & Z80_FLAG_CARRY) << 7;\n");
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_ZERO | Z80_FLAG_SIGN | Z80_FLAG_HALF_CARRY | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_NEGATIVE | Z80_FLAG_CARRY);\n");
fprintf(fp, " cpu.z80F |= (%s & Z80_FLAG_CARRY);\n", pbMathRegC[op]);
fprintf(fp, " %s = (%s >> 1) | bTemp2;\n", pbMathRegC[op], pbMathRegC[op]);
fprintf(fp, " cpu.z80F |= bPostORFlags[%s];\n", pbMathRegC[op]);
}
else
if ((0x20 == dwOpcode) || (0x30 == dwOpcode)) // SLA/SRL
{
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_ZERO | Z80_FLAG_SIGN | Z80_FLAG_HALF_CARRY | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_NEGATIVE | Z80_FLAG_CARRY);\n");
fprintf(fp, " cpu.z80F |= (%s >> 7);\n", pbMathRegC[op]);
fprintf(fp, " %s = (%s << 1);\n", pbMathRegC[op], pbMathRegC[op]);
fprintf(fp, " cpu.z80F |= bPostORFlags[%s];\n", pbMathRegC[op]);
}
else
if (0x28 == dwOpcode) // SRA
{
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_ZERO | Z80_FLAG_SIGN | Z80_FLAG_HALF_CARRY | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_NEGATIVE | Z80_FLAG_CARRY);\n");
fprintf(fp, " cpu.z80F |= (%s & Z80_FLAG_CARRY);\n", pbMathRegC[op]);
fprintf(fp, " %s = (%s >> 1) | (%s & 0x80);\n", pbMathRegC[op], pbMathRegC[op], pbMathRegC[op]);
fprintf(fp, " cpu.z80F |= bPostORFlags[%s];\n", pbMathRegC[op]);
}
else
if (0x38 == dwOpcode) // SRL
{
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_ZERO | Z80_FLAG_SIGN | Z80_FLAG_HALF_CARRY | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_NEGATIVE | Z80_FLAG_CARRY);\n");
fprintf(fp, " cpu.z80F |= (%s & Z80_FLAG_CARRY);\n", pbMathRegC[op]);
fprintf(fp, " %s = (%s >> 1);\n", pbMathRegC[op], pbMathRegC[op], pbMathRegC[op]);
fprintf(fp, " cpu.z80F |= bPostORFlags[%s];\n", pbMathRegC[op]);
}
else
{
InvalidInstructionC(2);
}
if (6 == op) // (HL)?
WriteValueToMemory("cpu.z80HL", "bTemp");
}
else
assert(0);
}
// ED Area
void RRDRLDHandler(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
ReadValueFromMemory("bx", "dl"); // Get (HL)
fprintf(fp, " mov dh, dl ; Put a copy in DH\n");
if (0x6f == dwOpcode) // RLD
{
fprintf(fp, " shr dh, 4 ; Get our upper nibble in position\n");
fprintf(fp, " shl dl, 4 ; Get our lower nibble into the higher position\n");
fprintf(fp, " shl ecx, 16 ; Save this for later\n");
fprintf(fp, " mov cl, al\n");
fprintf(fp, " and cl, 0fh\n ; Only the lower nibble\n");
fprintf(fp, " or dl, cl ; OR In A->(HL) transfer\n");
fprintf(fp, " and al, 0f0h ; Only the upper 4 bits remain\n");
fprintf(fp, " or al, dh ; OR It in to our accumulator\n");
fprintf(fp, " shr ecx, 16 ; Restore this\n");
}
else // RRD
if (0x67 == dwOpcode)
{
fprintf(fp, " shr dl, 4 ; Upper nibble to lower nibble\n");
fprintf(fp, " shl ecx, 16 ; Save this\n");
fprintf(fp, " mov cl, al\n");
fprintf(fp, " shl cl, 4\n");
fprintf(fp, " or dl, cl ; OR In what was in A\n");
fprintf(fp, " and al, 0f0h ; Knock out lower part\n");
fprintf(fp, " and dh, 0fh ; Only the lower nibble\n");
fprintf(fp, " or al, dh ; OR In our nibble\n");
fprintf(fp, " shr ecx, 16 ; Restore this\n");
}
else // Whoops!
assert(0);
// This routine assumes that the new value to be placed at (HL) is in DL
fprintf(fp, " and ah, 29h ; Retain carry & two undefined bits\n");
fprintf(fp, " mov dh, ah ; Store our flags away for later\n");
fprintf(fp, " or al, al ; Get our flags\n");
fprintf(fp, " lahf\n");
fprintf(fp, " and ah,0c4h ; Only partiy, zero, and sign\n");
fprintf(fp, " or ah, dh ; OR In our old flags\n");
// Now go write the value back
WriteValueToMemory("bx", "dl");
fprintf(fp, " xor edx, edx ; Zero out this for later\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
if (0x67 == dwOpcode) // RRD
{
ReadValueFromMemory("cpu.z80HL", "bTemp");
fprintf(fp, " bTemp2 = (cpu.z80A & 0x0f) << 4;\n");
fprintf(fp, " cpu.z80A = (cpu.z80A & 0xf0) | (bTemp & 0x0f);\n");
fprintf(fp, " bTemp = (bTemp >> 4) | bTemp2;\n");
WriteValueToMemory("cpu.z80HL", "bTemp");
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_SIGN | Z80_FLAG_ZERO | Z80_FLAG_HALF_CARRY | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_NEGATIVE);\n");
fprintf(fp, " cpu.z80F |= bPostORFlags[cpu.z80A];\n");
}
else
if (0x6f == dwOpcode) // RLD
{
ReadValueFromMemory("cpu.z80HL", "bTemp");
fprintf(fp, " bTemp2 = (cpu.z80A & 0x0f);\n");
fprintf(fp, " cpu.z80A = (cpu.z80A & 0xf0) | (bTemp >> 4);\n");
fprintf(fp, " bTemp = (bTemp << 4) | bTemp2;\n");
WriteValueToMemory("cpu.z80HL", "bTemp");
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_SIGN | Z80_FLAG_ZERO | Z80_FLAG_HALF_CARRY | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_NEGATIVE);\n");
fprintf(fp, " cpu.z80F |= bPostORFlags[cpu.z80A];\n");
}
else
InvalidInstructionC(2);
}
else
assert(0);
}
void CPICPDCPIRCPDRHandler(UINT32 dwOpcode)
{
UINT32 dwRepeatOb = 0;
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
if (dwOpcode == 0xb1 || dwOpcode == 0xb9)
{
fprintf(fp, "cpRepeat%ld:\n", dwGlobalLabel);
dwRepeatOb = dwGlobalLabel;
++dwGlobalLabel;
}
// Now go get the data from the source
ReadValueFromMemory("bx", "dl");
// Target data is in DL
fprintf(fp, " mov byte [_z80af], ah\n");
fprintf(fp, " sahf\n");
fprintf(fp, " cmp al, dl ; Do our comparison\n");
fprintf(fp, " lahf\n");
fprintf(fp, " and ah, 0fah ; No P/V or carry!\n");
fprintf(fp, " dec cx ; Dec BC\n");
fprintf(fp, " jz notBcZero%ld\n", dwGlobalLabel);
fprintf(fp, " or ah, 04h ; P/V set when BC not zero\n");
fprintf(fp, "notBcZero%ld:\n", dwGlobalLabel);
fprintf(fp, " or ah, 02h ; N Gets set when we do compares\n");
fprintf(fp, " mov dl, byte [_z80af]\n");
fprintf(fp, " and dl, 01h\n");
fprintf(fp, " or ah, dl ; Preserve carry!\n");
if (dwOpcode == 0xa1 || dwOpcode == 0xb1)
fprintf(fp, " inc bx ; Increment!\n");
if (dwOpcode == 0xa9 || dwOpcode == 0xb9)
fprintf(fp, " dec bx ; Decrement!\n");
// Let's see if we repeat...
if (dwOpcode == 0xb1 || dwOpcode == 0xb9)
{
fprintf(fp, " sahf\n");
fprintf(fp, " jz BCDone%ld\n", dwRepeatOb);
fprintf(fp, " jnp BCDone%ld\n", dwRepeatOb);
if (FALSE == bNoTiming)
{
fprintf(fp, " sub edi, dword 21\n");
fprintf(fp, " js BCDoneExit%ld\n", dwRepeatOb);
}
fprintf(fp, " jmp cpRepeat%ld\n", dwRepeatOb);
fprintf(fp, "BCDoneExit%ld:\n", dwRepeatOb);
fprintf(fp, " sub esi, 2 ; Back up to the instruction again\n");
fprintf(fp, " jmp noMoreExec\n\n");
fprintf(fp, "BCDone%ld:\n", dwRepeatOb);
}
fprintf(fp, " xor edx, edx\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
if (0xb1 == dwOpcode || 0xb9 == dwOpcode) // Repeat instruction?
{
fprintf(fp, " while ((sdwCyclesRemaining >= 0) && (cpu.z80BC))\n");
}
fprintf(fp, " {\n");
ReadValueFromMemory("cpu.z80HL", "bTemp");
if (0xb1 == dwOpcode || 0xa1 == dwOpcode)
{
fprintf(fp, " cpu.z80HL++;\n");
fprintf(fp, " cpu.z80HL &= 0xffff;\n");
}
else
{
fprintf(fp, " cpu.z80HL--;\n");
fprintf(fp, " cpu.z80HL &= 0xffff;\n");
}
fprintf(fp, " cpu.z80BC--;\n");
fprintf(fp, " cpu.z80BC &= 0xffff;\n");
if (0xb1 == dwOpcode || 0xb9 == dwOpcode) // Repeat?
{
fprintf(fp, " sdwCyclesRemaining -= 16;\n");
fprintf(fp, " if (cpu.z80A == bTemp)\n");
fprintf(fp, " {\n");
fprintf(fp, " break;\n");
fprintf(fp, " }\n");
}
fprintf(fp, " }\n");
// Now figure out what's going on
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_ZERO | Z80_FLAG_SIGN | Z80_FLAG_HALF_CARRY | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_NEGATIVE);\n");
fprintf(fp, " cpu.z80F |= (pbSubSbcTable[((UINT32) cpu.z80A << 8) | bTemp] & (Z80_FLAG_SIGN | Z80_FLAG_NEGATIVE | Z80_FLAG_ZERO));\n");
fprintf(fp, " if (cpu.z80BC)\n");
fprintf(fp, " {\n");
fprintf(fp, " cpu.z80F |= Z80_FLAG_OVERFLOW_PARITY;\n");
fprintf(fp, " }\n");
}
else
assert(0);
}
void INIRINDRINIINDHandler(UINT32 dwOpcode)
{
UINT32 dwTempLabel = 0;
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
dwTempLabel = dwGlobalLabel;
dwGlobalLabel++;
if (0xba == dwOpcode || 0xb2 == dwOpcode)
fprintf(fp, "loopIt%ld:\n", dwTempLabel);
// Fetch what's at (C) and put it in (HL)
fprintf(fp, " push cx ; Save BC\n");
if (b16BitIo == FALSE)
fprintf(fp, " xor ch, ch ; We want 8 bit ports\n");
ReadValueFromIo("cx", "*dl"); // Put our value in DL
fprintf(fp, " pop cx ; Restore BC\n");
WriteValueToMemory("bx", "dl");
if (0xa2 == dwOpcode || 0xb2 == dwOpcode)
fprintf(fp, " inc bx ; Increment HL\n");
else
if (0xaa == dwOpcode || 0xba == dwOpcode)
fprintf(fp, " dec bx ; Decrement HL\n");
// Now we decrement B
fprintf(fp, " dec ch ; Decrement B (of C)\n");
// Emit this instruction if we repeat
if (0xba == dwOpcode || 0xb2 == dwOpcode)
{
fprintf(fp, " jz near finalExit%ld\n", dwTempLabel);
// Otherwise, we need to loop again
if (FALSE == bNoTiming)
{
fprintf(fp, " sub edi, dword 21\n");
fprintf(fp, " js loopExit%ld\n", dwTempLabel);
}
fprintf(fp, " jmp loopIt%ld\n\n", dwTempLabel);
fprintf(fp, "loopExit%ld:\n", dwTempLabel);
fprintf(fp, " sub esi, 2\n");
fprintf(fp, " jmp noMoreExec\n\n");
}
// Now let's fix up the flags
fprintf(fp, "finalExit%ld:\n", dwTempLabel);
fprintf(fp, " jnz clearFlag%ld\n", dwTempLabel);
fprintf(fp, " or ah, 040h ; Set the Zero flag!\n");
fprintf(fp, " jmp short continue%ld\n", dwTempLabel);
fprintf(fp, "clearFlag%ld:\n", dwTempLabel);
fprintf(fp, " and ah, 0bfh ; Clear the zero flag\n");
fprintf(fp, "continue%ld:\n", dwTempLabel);
fprintf(fp, " or ah, 02h ; Set negative!\n");
fprintf(fp, " xor edx, edx\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
if (0xb2 == dwOpcode || 0xba == dwOpcode) // Repeat instruction?
{
fprintf(fp, " while ((sdwCyclesRemaining > 0) && (cpu.z80B))\n");
}
fprintf(fp, " {\n");
ReadValueFromIo("cpu.z80B", "bTemp");
WriteValueToMemory("cpu.z80HL", "bTemp");
if (0xb2 == dwOpcode || 0xa2 == dwOpcode)
{
fprintf(fp, " cpu.z80HL++;\n");
fprintf(fp, " cpu.z80HL &= 0xffff;\n");
}
else
{
fprintf(fp, " cpu.z80HL--;\n");
fprintf(fp, " cpu.z80HL &= 0xffff;\n");
}
fprintf(fp, " sdwCyclesRemaining -= 16;\n");
fprintf(fp, " cpu.z80B--;\n");
fprintf(fp, " }\n");
// Now figure out what's going on
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_ZERO | Z80_FLAG_SIGN | Z80_FLAG_HALF_CARRY | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_NEGATIVE);\n");
fprintf(fp, " cpu.z80F |= (bPostORFlags[bTemp] & (Z80_FLAG_SIGN | Z80_FLAG_NEGATIVE | Z80_FLAG_HALF_CARRY | Z80_FLAG_OVERFLOW_PARITY));\n");
fprintf(fp, " if (cpu.z80B)\n");
fprintf(fp, " {\n");
fprintf(fp, " cpu.z80F |= Z80_FLAG_OVERFLOW_PARITY;\n");
fprintf(fp, " pbPC -= 2;\n");
fprintf(fp, " }\n");
}
else
assert(0);
}
void OTIROTDROUTIOUTDHandler(UINT32 dwOpcode)
{
UINT32 dwTempLabel = 0;
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
dwTempLabel = dwGlobalLabel;
dwGlobalLabel++;
if (0xbb == dwOpcode || 0xb3 == dwOpcode)
fprintf(fp, "loopIt%ld:\n", dwTempLabel);
// Fetch what's at (HL) and put it in DL
ReadValueFromMemory("bx", "dl");
fprintf(fp, " push cx ; Save BC\n");
if (b16BitIo == FALSE)
fprintf(fp, " xor ch, ch ; No 16 bit for this instruction!\n");
WriteValueToIo("cx", "dl");
fprintf(fp, " pop cx ; Restore BC now that it has been \"OUT\"ed\n");
if (0xa3 == dwOpcode || 0xb3 == dwOpcode)
fprintf(fp, " inc bx ; Increment HL\n");
else
if (0xab == dwOpcode || 0xbb == dwOpcode)
fprintf(fp, " dec bx ; Decrement HL\n");
// Now we decrement B
fprintf(fp, " dec ch ; Decrement B (of C)\n");
// Emit this instruction if we repeat
if (0xbb == dwOpcode || 0xb3 == dwOpcode)
{
fprintf(fp, " jz near finalExit%ld\n", dwTempLabel);
// Otherwise, we need to loop again
if (FALSE == bNoTiming)
{
fprintf(fp, " sub edi, dword 21\n");
fprintf(fp, " js loopExit%ld\n", dwTempLabel);
}
fprintf(fp, " jmp loopIt%ld\n\n", dwTempLabel);
fprintf(fp, "loopExit%ld:\n", dwTempLabel);
fprintf(fp, " sub esi, 2\n");
fprintf(fp, " jmp noMoreExec\n\n");
}
// Now let's fix up the flags
fprintf(fp, "finalExit%ld:\n", dwTempLabel);
fprintf(fp, " jnz clearFlag%ld\n", dwTempLabel);
fprintf(fp, " or ah, 040h ; Set the Zero flag!\n");
fprintf(fp, " jmp short continue%ld\n", dwTempLabel);
fprintf(fp, "clearFlag%ld:\n", dwTempLabel);
fprintf(fp, " and ah, 0bfh ; Clear the zero flag\n");
fprintf(fp, "continue%ld:\n", dwTempLabel);
fprintf(fp, " or ah, 02h ; Set negative!\n");
fprintf(fp, " xor edx, edx\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
if (0xb3 == dwOpcode || 0xbb == dwOpcode) // Repeat instruction?
{
fprintf(fp, " while ((sdwCyclesRemaining > 0) && (cpu.z80B))\n");
}
fprintf(fp, " {\n");
ReadValueFromMemory("cpu.z80HL", "bTemp");
WriteValueToIo("cpu.z80BC", "bTemp");
if (0xb3 == dwOpcode || 0xa3 == dwOpcode)
{
fprintf(fp, " cpu.z80HL++;\n");
fprintf(fp, " cpu.z80HL &= 0xffff;\n");
}
else
{
fprintf(fp, " cpu.z80HL--;\n");
fprintf(fp, " cpu.z80HL &= 0xffff;\n");
}
fprintf(fp, " sdwCyclesRemaining -= 16;\n");
fprintf(fp, " cpu.z80B--;\n");
fprintf(fp, " }\n");
// Now figure out what's going on
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_ZERO | Z80_FLAG_SIGN | Z80_FLAG_HALF_CARRY | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_NEGATIVE);\n");
fprintf(fp, " cpu.z80F |= (bPostORFlags[bTemp] & (Z80_FLAG_SIGN | Z80_FLAG_NEGATIVE | Z80_FLAG_HALF_CARRY | Z80_FLAG_OVERFLOW_PARITY));\n");
fprintf(fp, " if (cpu.z80B)\n");
fprintf(fp, " {\n");
fprintf(fp, " cpu.z80F |= Z80_FLAG_OVERFLOW_PARITY;\n");
fprintf(fp, " }\n");
}
else
assert(0);
}
void AdcSbcRegpair(UINT32 dwOpcode)
{
UINT8 bOp = 0;
bOp = (dwOpcode >> 4) & 0x03;
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
fprintf(fp, " mov dx, %s ; Get our original register\n", pbRegPairs[bOp]);
fprintf(fp, " mov [_orgval], dx ; Store this for later half carry computation\n");
fprintf(fp, " mov [_orgval2], bx ; Store this, too\n");
fprintf(fp, " sahf ; Restore our flags\n");
if ((dwOpcode & 0xcf) == 0x4a)
fprintf(fp, " adc bx, dx ; Do the operation!\n");
else
fprintf(fp, " sbb bx, dx ; Do the operation!\n");
fprintf(fp, " lahf ; Get our new flags\n");
if ((dwOpcode & 0xcf) != 0x4a)
{
SetOverflow();
fprintf(fp, " and ah, 0edh ; Knock out negative & half carry flags\n");
fprintf(fp, " or ah, 02h ; Negative!\n");
fprintf(fp, " mov [_z80hl], bx\n");
fprintf(fp, " xor bx, [_orgval]\n");
fprintf(fp, " xor bx, [_orgval2]\n");
fprintf(fp, " and bh, 10h ; Half carry?\n");
fprintf(fp, " or ah, bh ; OR It in if so\n");
fprintf(fp, " mov bx, [_z80hl]\n");
}
else
{
SetOverflow();
fprintf(fp, " and ah, 0edh ; Knock out negative & half carry flags\n");
fprintf(fp, " mov [_z80hl], bx\n");
fprintf(fp, " xor bx, [_orgval]\n");
fprintf(fp, " xor bx, [_orgval2]\n");
fprintf(fp, " and bh, 10h ; Half carry?\n");
fprintf(fp, " or ah, bh ; OR It in if so\n");
fprintf(fp, " mov bx, [_z80hl]\n");
}
fprintf(fp, " xor edx, edx ; Make sure we don't hose things\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
if ((dwOpcode & 0xcf) == 0x4a) // ADC
{
fprintf(fp, " dwTemp = cpu.z80HL + %s + (cpu.z80F & Z80_FLAG_CARRY);\n", pbRegPairsC[bOp]);
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_CARRY | Z80_FLAG_NEGATIVE | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_HALF_CARRY | Z80_FLAG_ZERO | Z80_FLAG_SIGN);\n");
fprintf(fp, " cpu.z80F |= ((dwTemp >> 8) & Z80_FLAG_SIGN);\n");
fprintf(fp, " if (0 == (dwTemp & 0xffff))\n");
fprintf(fp, " {\n");
fprintf(fp, " cpu.z80F |= Z80_FLAG_ZERO;\n");
fprintf(fp, " }\n");
fprintf(fp, " cpu.z80F |= (((cpu.z80HL ^ dwTemp ^ %s) >> 8) & Z80_FLAG_HALF_CARRY);\n", pbRegPairsC[bOp]);
fprintf(fp, " cpu.z80F |= ((((%s ^ cpu.z80HL ^ 0x8000) & (%s ^ dwTemp)) >> 13) & Z80_FLAG_OVERFLOW_PARITY);\n", pbRegPairsC[bOp], pbRegPairsC[bOp]);
fprintf(fp, " cpu.z80F |= ((dwTemp >> 16) & Z80_FLAG_CARRY);\n");
fprintf(fp, " cpu.z80HL = dwTemp & 0xffff;\n");
return;
}
else // SBC
{
fprintf(fp, " dwTemp = cpu.z80HL - %s - (cpu.z80F & Z80_FLAG_CARRY);\n", pbRegPairsC[bOp]);
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_CARRY | Z80_FLAG_NEGATIVE | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_HALF_CARRY | Z80_FLAG_ZERO | Z80_FLAG_SIGN);\n");
fprintf(fp, " cpu.z80F |= ((dwTemp >> 8) & Z80_FLAG_SIGN);\n");
fprintf(fp, " if (0 == (dwTemp & 0xffff))\n");
fprintf(fp, " {\n");
fprintf(fp, " cpu.z80F |= Z80_FLAG_ZERO;\n");
fprintf(fp, " }\n");
fprintf(fp, " cpu.z80F |= (((cpu.z80HL ^ dwTemp ^ %s) >> 8) & Z80_FLAG_HALF_CARRY);\n", pbRegPairsC[bOp]);
fprintf(fp, " cpu.z80F |= ((((%s ^ cpu.z80HL) & (%s ^ dwTemp)) >> 13) & Z80_FLAG_OVERFLOW_PARITY);\n", pbRegPairsC[bOp], pbRegPairsC[bOp]);
fprintf(fp, " cpu.z80F |= ((dwTemp >> 16) & Z80_FLAG_CARRY);\n");
fprintf(fp, " cpu.z80HL = dwTemp & 0xffff;\n");
return;
}
}
else
assert(0);
}
void RetIRetNHandler(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
if (bThroughCallHandler)
{
fprintf(fp, " call PopWord\n");
fprintf(fp, " xor esi, esi\n");
fprintf(fp, " mov si, dx\n");
fprintf(fp, " add esi, ebp\n");
}
else
{
fprintf(fp, " mov dx, word [_z80sp] ; Get our current stack pointer\n");
fprintf(fp, " mov si, [edx+ebp] ; Get our return address\n");
fprintf(fp, " and esi, 0ffffh ; Only within 64K!\n");
fprintf(fp, " add esi, ebp ; Add in our base address\n");
fprintf(fp, " add word [_z80sp], 02h ; Remove our two bytes from the stack\n");
}
if (dwOpcode == 0x45)
{
fprintf(fp, " xor edx, edx\n");
fprintf(fp, " mov dl, [_z80iff] ; Get interrupt flags\n");
fprintf(fp, " shr dl, 1 ; Move IFF2->IFF1\n");
fprintf(fp, " and [_z80iff], dword (~IFF1) ; Get rid of IFF 1\n");
fprintf(fp, " and dl, IFF1 ; Just want the IFF 1 value now\n");
fprintf(fp, " or dword [_z80iff], edx\n");
}
fprintf(fp, " xor edx, edx ; Make sure we don't hose things\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
if (0x4d == dwOpcode) // RETI
{
fprintf(fp, " pbSP = cpu.z80Base + cpu.z80sp; /* Normalize our stack PTR */\n");
fprintf(fp, " dwAddr = *pbSP++; /* Pop LSB */\n");
fprintf(fp, " dwAddr |= ((UINT32) *pbSP << 8); /* Pop MSB */\n");
fprintf(fp, " cpu.z80sp += 2; /* Pop the word off */\n");
fprintf(fp, " pbPC = (cpu.z80Base + dwAddr); /* Point PC to our return address */\n");
}
else
if (0x45 == dwOpcode) // RETN
{
fprintf(fp, " pbSP = cpu.z80Base + cpu.z80sp; /* Normalize our stack PTR */\n");
fprintf(fp, " dwAddr = *pbSP++; /* Pop LSB */\n");
fprintf(fp, " dwAddr |= ((UINT32) *pbSP << 8); /* Pop MSB */\n");
fprintf(fp, " cpu.z80sp += 2; /* Pop the word off */\n");
fprintf(fp, " pbPC = (cpu.z80Base + dwAddr); /* Point PC to our return address */\n");
fprintf(fp, " cpu.z80iff &= ~(IFF1); /* Keep IFF2 around */\n");
fprintf(fp, " cpu.z80iff |= ((cpu.z80iff >> 1) & IFF1); /* IFF2->IFF1 */\n");
}
else
{
InvalidInstructionC(2);
}
}
else
assert(0);
}
void ExtendedOutHandler(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
if (b16BitIo == FALSE)
fprintf(fp, " mov dl, cl ; Address in DX... (C)\n");
else
fprintf(fp, " mov dx, cx ; Address in DX... (BC)\n");
WriteValueToIo("dx", pbMathReg[(dwOpcode >> 3) & 0x07]);
fprintf(fp, " xor edx, edx\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
if (b16BitIo == FALSE)
fprintf(fp, " dwAddr = cpu.z80C;\n");
else
fprintf(fp, " dwAddr = cpu.z80BC;\n");
WriteValueToIo("dwAddr", pbMathRegC[(dwOpcode >> 3) & 0x07]);
}
else
assert(0);
}
void ExtendedInHandler(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
if (b16BitIo == FALSE)
fprintf(fp, " mov dl, cl ; Address in DX... (C)\n");
else
fprintf(fp, " mov dx, cx ; Address in DX... (BC)\n");
ReadValueFromIo("dx", pbMathReg[(dwOpcode >> 3) & 0x07]);
fprintf(fp, ";\n; Remember, this variant of the IN instruction modifies the flags\n;\n\n");
fprintf(fp, " sahf ; Restore our flags\n");
fprintf(fp, " mov dh, ah ; Save flags for later\n");
if (0x50 == dwOpcode || 0x58 == dwOpcode)
{
fprintf(fp, " mov dl, %s\n", pbMathReg[(dwOpcode >> 3) & 0x07]);
fprintf(fp, " or dl, dl\n");
}
else
fprintf(fp, " or %s, %s;\n", pbMathReg[(dwOpcode >> 3) & 0x07], pbMathReg[(dwOpcode >> 3) & 0x07]);
fprintf(fp, " lahf\n");
fprintf(fp, " and dh, 029h ; Only keep carry and two unused flags\n");
fprintf(fp, " and ah, 0d4h\n");
fprintf(fp, " or ah, dh\n");
fprintf(fp, " xor edx, edx\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
if (b16BitIo == FALSE)
fprintf(fp, " dwAddr = cpu.z80C;\n");
else
fprintf(fp, " dwAddr = cpu.z80BC;\n");
ReadValueFromIo("dwAddr", pbMathRegC[(dwOpcode >> 3) & 0x07]);
// Set flags!
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_SIGN | Z80_FLAG_ZERO | Z80_FLAG_HALF_CARRY | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_NEGATIVE);\n");
fprintf(fp, " cpu.z80F |= bPostORFlags[%s];\n", pbMathRegC[(dwOpcode >> 3) & 0x07]);
}
else
assert(0);
}
void NegHandler(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
fprintf(fp, " sahf\n");
fprintf(fp, " sub dh, al\n");
fprintf(fp, " lahf\n");
fprintf(fp, " mov al, dh\n");
SetOverflow();
fprintf(fp, " or ah, 02h\n");
fprintf(fp, " xor edx, edx\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
SetSubFlagsSZHVC("0", "cpu.z80A");
fprintf(fp, " cpu.z80A = 0 - cpu.z80A;\n");
}
else
assert(0);
}
void ExtendedRegIntoMemory(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
fprintf(fp, " mov dx, [esi] ; Get our address to write to\n");
fprintf(fp, " add esi, 2 ; Next address, please...\n");
if (dwOpcode == 0x43)
WriteValueToMemory("dx", "cl");
if (dwOpcode == 0x53)
WriteValueToMemory("dx", "byte [_z80de]");
if (dwOpcode == 0x63)
WriteValueToMemory("dx", "bl");
if (dwOpcode == 0x73)
WriteValueToMemory("dx", "byte [_z80sp]");
fprintf(fp, " inc dx\n");
if (dwOpcode == 0x43)
WriteValueToMemory("dx", "ch");
if (dwOpcode == 0x53)
WriteValueToMemory("dx", "byte [_z80de + 1]");
if (dwOpcode == 0x63)
WriteValueToMemory("dx", "bh");
if (dwOpcode == 0x73)
WriteValueToMemory("dx", "byte [_z80sp + 1]");
fprintf(fp, " xor edx, edx ; Zero our upper word\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " dwTemp = *pbPC++;\n");
fprintf(fp, " dwTemp |= ((UINT32) *pbPC++ << 8);\n");
if (0x43 == dwOpcode) // LD (xxxxh), BC
WriteWordToMemory("dwTemp", "cpu.z80BC");
if (0x53 == dwOpcode) // LD (xxxxh), DE
WriteWordToMemory("dwTemp", "cpu.z80DE");
if (0x63 == dwOpcode) // LD (xxxxh), HL
WriteWordToMemory("dwTemp", "cpu.z80HL");
if (0x73 == dwOpcode) // LD (xxxxh), SP
WriteWordToMemory("dwTemp", "cpu.z80sp");
}
else
assert(0);
}
void LdRegpair(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
fprintf(fp, " mov dx, [esi] ; Get address to load\n");
fprintf(fp, " add esi, 2 ; Skip over it so we don't execute it\n");
if (dwOpcode == 0x4b)
ReadValueFromMemory("dx", "cl");
if (dwOpcode == 0x5b)
ReadValueFromMemory("dx", "byte [_z80de]");
if (dwOpcode == 0x7b)
ReadValueFromMemory("dx", "byte [_z80sp]");
fprintf(fp, " inc dx\n");
if (dwOpcode == 0x4b)
ReadValueFromMemory("dx", "ch");
if (dwOpcode == 0x5b)
ReadValueFromMemory("dx", "byte [_z80de + 1]");
if (dwOpcode == 0x7b)
ReadValueFromMemory("dx", "byte [_z80sp + 1]");
fprintf(fp, " xor edx, edx\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " dwTemp = *pbPC++;\n");
fprintf(fp, " dwTemp |= ((UINT32) *pbPC++ << 8);\n");
if (0x4b == dwOpcode)
ReadWordFromMemory("dwTemp", "cpu.z80BC");
if (0x5b == dwOpcode)
ReadWordFromMemory("dwTemp", "cpu.z80DE");
if (0x7b == dwOpcode)
ReadWordFromMemory("dwTemp", "cpu.z80sp");
}
else
assert(0);
}
void LDILDRLDIRLDDRHandler(UINT32 dwOpcode)
{
UINT32 dwOrgGlobal = 0;
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
if (dwOpcode == 0xb0 || dwOpcode == 0xb8)
{
dwOrgGlobal = dwGlobalLabel;
fprintf(fp, "ldRepeat%ld:\n", dwGlobalLabel);
}
ReadValueFromMemory("bx", "dl");
// Here we write the byte back to the target
WriteValueToMemory("[_z80de]", "dl");
// Now we decide what to do
if ((dwOpcode & 0x0f) == 0)
{
fprintf(fp, " inc bx ; Increment HL\n");
fprintf(fp, " inc word [_z80de] ; Increment DE\n");
}
else
{
fprintf(fp, " dec bx ; Decrement HL\n");
fprintf(fp, " dec word [_z80de] ; Decrement DE\n");
}
fprintf(fp, " dec cx ; Decrement BC\n");
if (dwOpcode == 0xb0 || dwOpcode == 0xb8)
{
if (FALSE == bNoTiming)
{
fprintf(fp, " jz noMore%ld\n", dwGlobalLabel);
fprintf(fp, " sub edi, dword 16 ; 16 T-States per iteration\n");
fprintf(fp, " js noMore%ld\n", dwGlobalLabel);
}
else
{
fprintf(fp, " jz noMore%ld\n", dwGlobalLabel);
}
fprintf(fp, " jmp ldRepeat%ld ; Loop until we're done!\n", dwOrgGlobal);
fprintf(fp, "noMore%ld:\n", dwGlobalLabel);
}
fprintf(fp, " and ah, 0e9h ; Knock out H & N and P/V\n");
fprintf(fp, " or cx, cx ; Flag BC\n");
fprintf(fp, " jz atZero%ld ; We're done!\n", dwGlobalLabel);
if (dwOpcode == 0xb0 || dwOpcode == 0xb8)
{
// It's a repeat, so let's readjust ESI, shall we?
fprintf(fp, " or ah, 04h ; Non-zero - we're still going!\n");
fprintf(fp, " sub esi, 2 ; Adjust back to the beginning of the instruction\n");
fprintf(fp, " jmp noMoreExec\n\n");
}
else
if (dwOpcode == 0xa0 || dwOpcode == 0xa8)
{
fprintf(fp, " or ah, 04h ; Non-zero - we're still going!\n");
}
fprintf(fp, "atZero%ld:\n", dwGlobalLabel);
++dwGlobalLabel;
fprintf(fp, " xor edx, edx ; Make sure we don't hose things\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
// This is the actual move
if (0xb0 == dwOpcode || 0xb8 == dwOpcode) // Repeat instruction?
{
fprintf(fp, " while ((sdwCyclesRemaining > 0) && (cpu.z80BC))\n");
fprintf(fp, " {\n");
}
ReadValueFromMemory("cpu.z80HL", "bTemp");
WriteValueToMemory("cpu.z80DE", "bTemp");
if ((dwOpcode & 0x0f) == 0)
{
fprintf(fp, " ++cpu.z80HL;\n");
fprintf(fp, " ++cpu.z80DE;\n");
}
else
{
fprintf(fp, " --cpu.z80HL;\n");
fprintf(fp, " --cpu.z80DE;\n");
}
fprintf(fp, " --cpu.z80BC;\n");
fprintf(fp, " cpu.z80HL &= 0xffff;\n");
fprintf(fp, " cpu.z80DE &= 0xffff;\n");
fprintf(fp, " cpu.z80BC &= 0xffff;\n");
if (0xb0 == dwOpcode || 0xb8 == dwOpcode) // Repeat instruction?
{
fprintf(fp, " sdwCyclesRemaining -= 21;\n");
fprintf(fp, " }\n");
}
// Time for a flag fixup!
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_HALF_CARRY | Z80_FLAG_NEGATIVE | Z80_FLAG_OVERFLOW_PARITY);\n");
fprintf(fp, " if (cpu.z80BC)\n");
fprintf(fp, " {\n");
if (0xb0 == dwOpcode || 0xb8 == dwOpcode)
{
fprintf(fp, " pbPC -= 2; /* Back up so we hit this instruction again */\n");
}
fprintf(fp, " cpu.z80F |= Z80_FLAG_OVERFLOW_PARITY;\n");
fprintf(fp, " }\n");
if (0xb0 == dwOpcode || 0xb8 == dwOpcode) // Repeat instruction?
{
fprintf(fp, " sdwCyclesRemaining -= 16;\n");
}
}
else
assert(0);
}
void IMHandler(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
if (dwOpcode == 0x46)
fprintf(fp, " mov dword [_z80interruptMode], 0 ; IM 0\n");
if (dwOpcode == 0x56)
{
fprintf(fp, " mov dword [_z80interruptMode], 1 ; Interrupt mode 1\n");
fprintf(fp, " mov word [_z80intAddr], 038h ; Interrupt mode 1 cmd!\n");
}
if (dwOpcode == 0x5e)
fprintf(fp, " mov dword [_z80interruptMode], 2 ; IM 2\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
if (0x46 == dwOpcode) // IM 0
fprintf(fp, " cpu.z80interruptMode = 0;\n");
if (0x56 == dwOpcode) // IM 1
{
fprintf(fp, " cpu.z80interruptMode = 1;\n");
fprintf(fp, " cpu.z80intAddr = 0x38;\n");
}
if (0x5e == dwOpcode) // IM 2
fprintf(fp, " cpu.z80interruptMode = 2;\n");
}
else
assert(0);
}
void IRHandler(UINT32 dwOpcode)
{
char *src, *dst;
if (MZ80_ASSEMBLY_X86 == bWhat)
{
switch(dwOpcode)
{
case 0x57:
dst = "al"; src="[_z80i]"; break;
case 0x5F:
dst = "al"; src="[_z80r]"; break;
case 0x47:
dst = "[_z80i]"; src="al"; break;
case 0x4F:
dst = "[_z80r]"; src="al"; break;
}
ProcBegin(dwOpcode);
fprintf(fp, " mov %s, %s\n",dst,src);
if (dwOpcode == 0x5f)
{
fprintf(fp, " and ah, 029h ; No N, H, Z, or S!\n");
fprintf(fp, " or al,al ; Get appropriate flags\n");
fprintf(fp, " o16 pushf\n");
fprintf(fp, " pop dx\n");
fprintf(fp, " and dl, 0c0h\n");
fprintf(fp, " or ah, dl ; OR In our S & Z flags\n");
fprintf(fp, " mov dl, [_z80iff]\n");
fprintf(fp, " and dl, IFF2\n");
fprintf(fp, " shl dl, 1\n");
fprintf(fp, " or ah, dl\n");
// Randomize R
fprintf(fp, " mov edx, [dwLastRSample]\n");
fprintf(fp, " sub edx, edi\n");
fprintf(fp, " add edx, [_z80rCounter]\n");
fprintf(fp, " shr edx, 2\n");
fprintf(fp, " and edx, 07fh\n");
fprintf(fp, " and byte [_z80r], 80h\n");
fprintf(fp, " or byte [_z80r], dl\n");
fprintf(fp, " xor edx, edx\n");
fprintf(fp, " mov [dwLastRSample], edi\n");
}
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
if (0x5f == dwOpcode) // LD A, R
{
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_HALF_CARRY | Z80_FLAG_SIGN | Z80_FLAG_ZERO | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_NEGATIVE);\n");
fprintf(fp, " cpu.z80F |= bPostORFlags[cpu.z80r];\n");
fprintf(fp, " cpu.z80F = (cpu.z80F & ~(Z80_FLAG_OVERFLOW_PARITY)) | ((cpu.z80iff & IFF2) << 1);\n");
fprintf(fp, " cpu.z80A = cpu.z80r;\n");
// Now randomize a little
fprintf(fp, " bTemp = (cpu.z80r + (cpu.z80B + sdwCyclesRemaining + 1 + cpu.z80H)) ^ cpu.z80A;\n");
fprintf(fp, " cpu.z80r = (cpu.z80r & 0x80) | (bTemp & 0x7f);\n");
}
else
if (0x47 == dwOpcode) // LD I, A
{
fprintf(fp, " cpu.z80i = cpu.z80A;\n");
}
else
if (0x57 == dwOpcode) // LD A, I
{
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_HALF_CARRY | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_NEGATIVE | Z80_FLAG_ZERO | Z80_FLAG_SIGN);\n");
fprintf(fp, " cpu.z80F |= ((cpu.z80iff & IFF2) << 1);\n");
fprintf(fp, " cpu.z80A = cpu.z80i;\n");
fprintf(fp, " cpu.z80F |= bPostORFlags[cpu.z80A];\n");
}
else
if (0x4f == dwOpcode) // LD R, A
{
fprintf(fp, " cpu.z80r = cpu.z80A;\n");
}
else
{
InvalidInstructionC(2);
}
}
else
assert(0);
}
// DD/FD Area
void DDFDCBHandler(UINT32 dwOpcode)
{
UINT32 dwData = 0;
if (MZ80_ASSEMBLY_X86 == bWhat)
{
fprintf(fp, "%sInst%.2x:\n", majorOp, dwOpcode);
fprintf(fp, " mov dx, [esi] ; Get our instruction (and offset)\n");
fprintf(fp, " add esi, 2 ; Increment our PC\n");
fprintf(fp, " mov byte [_orgval], dl ; Store our value\n");
fprintf(fp, " or dl, dl\n");
fprintf(fp, " js notNeg%ld\n", dwGlobalLabel);
fprintf(fp, " mov byte [_orgval + 1], 00h;\n");
fprintf(fp, " jmp short jumpHandler%ld\n", dwGlobalLabel);
fprintf(fp, "notNeg%ld:\n", dwGlobalLabel);
fprintf(fp, " mov byte [_orgval + 1], 0ffh; It's negative\n");
fprintf(fp, "jumpHandler%ld:\n", dwGlobalLabel++);
fprintf(fp, " shl ebx, 16 ; Save BX away\n");
fprintf(fp, " mov bx, [_z80%s]\n", mz80Index);
fprintf(fp, " add [_orgval], bx\n");
fprintf(fp, " shr ebx, 16 ; Restore BX\n");
fprintf(fp, " mov dl, dh ; Get our instruction\n");
fprintf(fp, " xor dh, dh ; Zero this\n");
fprintf(fp, " jmp dword [z80ddfdcbInstructions+edx*4]\n\n");
}
else
if (MZ80_C == bWhat)
{
if (strcmp("cpu.z80IX", mz80Index) == 0)
dwData = 0;
else
dwData = 1;
fprintf(fp, " DDFDCBHandler(%d);\n", dwData);
}
else
assert(0);
}
void LoadIndexReg(UINT32 dwOpcode)
{
UINT8 string[150];
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
sprintf(string, "[_z80%s]", mz80Index);
fprintf(fp, " mov dx, [esi] ; Get our address to store\n");
fprintf(fp, " add esi, 2\n");
ReadWordFromMemory("dx", string);
fprintf(fp, " xor edx, edx\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " dwAddr = *pbPC++;\n");
fprintf(fp, " dwAddr |= ((UINT32) *pbPC++ << 8);\n");
ReadWordFromMemory("dwAddr", mz80Index);
}
else
assert(0);
}
void StoreIndexReg(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
fprintf(fp, " mov dx, [esi] ; Get our address to store\n");
fprintf(fp, " add esi, 2\n");
fprintf(fp, " mov [_orgval], dx\n");
fprintf(fp, " mov dl, [_z80%s]\n", mz80Index);
WriteValueToMemory("[_orgval]", "dl");
fprintf(fp, " inc word [_orgval]\n");
fprintf(fp, " mov dl, [_z80%s + 1]\n", mz80Index);
WriteValueToMemory("[_orgval]", "dl");
fprintf(fp, " xor edx, edx\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " dwAddr = *pbPC++;\n");
fprintf(fp, " dwAddr |= ((UINT32) *pbPC++ << 8);\n");
WriteWordToMemory("dwAddr", mz80Index);
}
else
assert(0);
}
void LdIndexPtrReg(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
IndexedOffset(mz80Index);
// DX Contains the address
WriteValueToMemory("dx", pbMathReg[dwOpcode & 0x07]);
fprintf(fp, " xor edx, edx\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " sdwAddr = (INT8) *pbPC++; // Get the offset\n");
fprintf(fp, " sdwAddr = ((INT32) %s + sdwAddr) & 0xffff;\n", mz80Index);
WriteValueToMemory("sdwAddr", pbMathRegC[dwOpcode & 0x07]);
}
else
assert(0);
}
void UndocMathIndex(UINT32 dwOpcode)
{
UINT32 dwOpcode1 = 0;
UINT8 *pbIndexReg = NULL;
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
if (dwOpcode & 1)
fprintf(fp, " mov dl, byte [_z80%s]\n", mz80Index);
else
fprintf(fp, " mov dl, byte [_z80%s + 1]\n", mz80Index);
// Info is in DL - let's do the math operation
fprintf(fp, " sahf ; Store our flags in x86 flag reg\n");
dwOpcode1 = (dwOpcode & 0xf8); // Only the operation
if (dwOpcode1 == 0x80)
fprintf(fp, " add al, dl\n");
else
if (dwOpcode1 == 0x88)
fprintf(fp, " adc al, dl\n");
else
if (dwOpcode1 == 0x90)
fprintf(fp, " sub al, dl\n");
else
if (dwOpcode1 == 0x98)
fprintf(fp, " sbb al, dl\n");
else
if (dwOpcode1 == 0xa0)
fprintf(fp, " and al, dl\n");
else
if (dwOpcode1 == 0xa8)
fprintf(fp, " xor al, dl\n");
else
if (dwOpcode1 == 0xb0)
fprintf(fp, " or al, dl\n");
else
if (dwOpcode1 == 0xb8)
fprintf(fp, " cmp al, dl\n");
else
assert(0);
fprintf(fp, " lahf ; Get flags back into AH\n");
if (dwOpcode1 != 0xa8 && dwOpcode1 != 0xa0 && dwOpcode1 != 0xb0)
{
SetOverflow();
}
if (dwOpcode1 == 0xa8)
fprintf(fp, " and ah, 0ech ; Only these flags matter!\n");
if (dwOpcode1 == 0xa0)
{
fprintf(fp, " and ah, 0ech ; Only these flags matter!\n");
fprintf(fp, " or ah, 010h ; Half carry gets set\n");
}
if (dwOpcode1 == 0xb0)
fprintf(fp, " and ah, 0ech ; No H, N, or C\n");
if (dwOpcode1 == 0xb8)
fprintf(fp, " or ah, 02h ; Negative gets set on a compare\n");
if (dwOpcode1 == 0x80 || dwOpcode1 == 0x88)
fprintf(fp, " and ah, 0fdh ; No N!\n");
if (dwOpcode1 == 0x90 || dwOpcode1 == 0x98)
fprintf(fp, " or ah, 02h ; N Gets set!\n");
if (dwOpcode1 == 0xb0)
fprintf(fp, " and ah, 0ech ; No H, N, or C\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
if (dwOpcode & 1)
pbIndexReg = mz80IndexHalfLow;
else
pbIndexReg = mz80IndexHalfHigh;
dwOpcode1 = (dwOpcode & 0xf8); // Only the operation
if (0x80 == dwOpcode1) // ADD
{
fprintf(fp, " bTemp2 = cpu.z80A + %s;\n", pbIndexReg);
SetAddFlagsSZHVC("cpu.z80A", pbIndexReg);
}
else
if (0x88 == dwOpcode1) // ADC
{
fprintf(fp, " bTemp2 = cpu.z80A + %s + (cpu.z80F & Z80_FLAG_CARRY);\n", pbIndexReg);
SetAdcFlagsSZHVC("cpu.z80A", pbIndexReg);
}
else
if (0x90 == dwOpcode1) // SUB
{
fprintf(fp, " bTemp2 = cpu.z80A - %s;\n", pbIndexReg);
SetSubFlagsSZHVC("cpu.z80A", pbIndexReg);
}
else
if (0x98 == dwOpcode1) // SBC
{
fprintf(fp, " bTemp2 = cpu.z80A - %s - (cpu.z80F & Z80_FLAG_CARRY);\n", pbIndexReg);
SetSbcFlagsSZHVC("cpu.z80A", pbIndexReg);
}
else
if (0xa0 == dwOpcode1) // AND
{
fprintf(fp, " cpu.z80A &= %s;\n", pbIndexReg);
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_CARRY | Z80_FLAG_NEGATIVE | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_HALF_CARRY | Z80_FLAG_ZERO | Z80_FLAG_SIGN);\n");
fprintf(fp, " cpu.z80F |= bPostANDFlags[cpu.z80A];\n\n");
}
else
if (0xa8 == dwOpcode1) // XOR
{
fprintf(fp, " cpu.z80A ^= %s;\n", pbIndexReg);
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_CARRY | Z80_FLAG_NEGATIVE | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_HALF_CARRY | Z80_FLAG_ZERO | Z80_FLAG_SIGN);\n");
fprintf(fp, " cpu.z80F |= bPostORFlags[cpu.z80A];\n\n");
}
else
if (0xb0 == dwOpcode1) // OR
{
fprintf(fp, " cpu.z80A |= %s;\n", pbIndexReg);
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_CARRY | Z80_FLAG_NEGATIVE | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_HALF_CARRY | Z80_FLAG_ZERO | Z80_FLAG_SIGN);\n");
fprintf(fp, " cpu.z80F |= bPostORFlags[cpu.z80A];\n\n");
}
else
if (0xb8 == dwOpcode1) // CP - Don't do anything! Just flags!
{
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_CARRY | Z80_FLAG_NEGATIVE | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_HALF_CARRY | Z80_FLAG_ZERO | Z80_FLAG_SIGN);\n");
fprintf(fp, " cpu.z80F |= bPostORFlags[cpu.z80A];\n\n");
}
else
{
assert(0);
}
InvalidInstructionC(2);
}
else
assert(0);
}
void UndocLoadHalfIndexReg(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
fprintf(fp, " mov dl, [esi] ; Get immediate byte to load\n");
fprintf(fp, " inc esi ; Next byte\n");
if (dwOpcode == 0x26)
fprintf(fp, " mov byte [_z80%s + 1], dl\n", mz80Index);
if (dwOpcode == 0x2e)
fprintf(fp, " mov byte [_z80%s], dl\n", mz80Index);
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
if (dwOpcode & 0x08)
fprintf(fp, " %s = *pbPC++;\n", mz80IndexHalfLow);
else
fprintf(fp, " %s = *pbPC++;\n", mz80IndexHalfHigh);
}
else
assert(0);
}
void UndocIncDecIndexReg(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
fprintf(fp, " sahf\n");
if (dwOpcode == 0x24)
fprintf(fp, " inc byte [_z80%s + 1]\n", mz80Index);
if (dwOpcode == 0x25)
fprintf(fp, " dec byte [_z80%s + 1]\n", mz80Index);
if (dwOpcode == 0x2c)
fprintf(fp, " inc byte [_z80%s]\n", mz80Index);
if (dwOpcode == 0x2d)
fprintf(fp, " dec byte [_z80%s]\n", mz80Index);
fprintf(fp, " lahf\n");
SetOverflow();
if ((0x24 == dwOpcode) || (0x2c == dwOpcode))
fprintf(fp, " and ah, 0fdh ; Knock out N!\n");
else
fprintf(fp, " or ah, 02h ; Set negative!\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_SIGN | Z80_FLAG_ZERO | Z80_FLAG_HALF_CARRY | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_NEGATIVE);\n");
if (0x24 == dwOpcode || 0x2c == dwOpcode)
{
if (dwOpcode & 0x08)
fprintf(fp, " cpu.z80F |= bPostIncFlags[%s++];\n", mz80IndexHalfLow);
else
fprintf(fp, " cpu.z80F |= bPostIncFlags[%s++];\n", mz80IndexHalfHigh);
}
else
{
if (dwOpcode & 0x08)
fprintf(fp, " cpu.z80F |= bPostDecFlags[%s--];\n", mz80IndexHalfLow);
else
fprintf(fp, " cpu.z80F |= bPostDecFlags[%s--];\n", mz80IndexHalfHigh);
}
}
else
assert(0);
}
void ExIndexed(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
if( bThroughCallHandler )
{
fprintf(fp, " mov dx, word [_z80%s]\n", mz80Index);
fprintf(fp, " push dx\n");
fprintf(fp, " call PopWord\n");
fprintf(fp, " mov [_z80%s], dx\n", mz80Index);
fprintf(fp, " pop dx\n");
fprintf(fp, " mov [_wordval], dx\n" );
fprintf(fp, " call PushWord\n" );
}
else
{
fprintf(fp, " mov [cyclesRemaining], edi\n");
fprintf(fp, " mov dx, word [_z80sp]\n");
fprintf(fp, " xor edi, edi\n");
fprintf(fp, " mov di, [_z80%s]\n", mz80Index);
fprintf(fp, " xchg di, [ebp+edx]\n");
fprintf(fp, " mov [_z80%s], di\n", mz80Index);
fprintf(fp, " xor edx, edx\n");
fprintf(fp, " mov edi, [cyclesRemaining]\n");
}
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
ReadWordFromMemory("cpu.z80sp", "dwAddr");
WriteWordToMemory("cpu.z80sp", mz80Index);
fprintf(fp, " %s = dwAddr;\n", mz80Index);
}
else
assert(0);
}
void IncDecIndexReg(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
if (dwOpcode == 0x23)
fprintf(fp, " inc word [_z80%s] ; Increment our mz80Index register\n", mz80Index);
else
fprintf(fp, " dec word [_z80%s] ; Increment our mz80Index register\n", mz80Index);
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
if (0x23 == dwOpcode)
{
fprintf(fp, " %s++;\n", mz80Index);
}
else
{
fprintf(fp, " %s--;\n", mz80Index);
}
fprintf(fp, " %s &= 0xffff;\n", mz80Index);
}
else
assert(0);
}
void LdRegIndexOffset(UINT32 dwOpcode)
{
UINT32 dwOpcode1 = 0;
dwOpcode1 = (dwOpcode & 0x38) >> 3;
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
IndexedOffset(mz80Index);
ReadValueFromMemory("dx", pbMathReg[dwOpcode1]);
fprintf(fp, " xor edx, edx ; Make sure we don't hose things\n");
dwGlobalLabel++;
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " sdwAddr = (INT8) *pbPC++; // Get the offset\n");
fprintf(fp, " sdwAddr = ((INT32) %s + sdwAddr) & 0xffff;\n", mz80Index);
ReadValueFromMemory("sdwAddr", pbMathRegC[dwOpcode1]);
}
else
assert(0);
}
void LdByteToIndex(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
fprintf(fp, " mov dx, [esi] ; Get our address\n");
fprintf(fp, " add esi, 2 ; Skip over our storage bytes\n");
fprintf(fp, " mov [cyclesRemaining], edi\n");
fprintf(fp, " mov di, dx ; Store it here for later\n");
fprintf(fp, " xor dh, dh\n");
fprintf(fp, " or dl, dl\n");
fprintf(fp, " jns noNegate%ld\n", dwGlobalLabel);
fprintf(fp, " dec dh\n");
fprintf(fp, "noNegate%ld:\n", dwGlobalLabel);
fprintf(fp, " add dx, [_z80%s] ; Add in our index\n", mz80Index);
fprintf(fp, " mov [_orgval], dx ; Store our address to write to\n");
fprintf(fp, " mov dx, di\n");
fprintf(fp, " xchg dh, dl\n");
fprintf(fp, " mov edi, [cyclesRemaining]\n");
WriteValueToMemory("[_orgval]", "dl");
fprintf(fp, " xor edx, edx\n");
++dwGlobalLabel;
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " sdwAddr = (INT8) *pbPC++; // Get the offset\n");
fprintf(fp, " sdwAddr = ((INT32) %s + sdwAddr) & 0xffff;\n", mz80Index);
WriteValueToMemory("sdwAddr", "*pbPC++");
}
else
assert(0);
}
void SPToIndex(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
fprintf(fp, " mov dx, [_z80%s] ; Get our source register\n", mz80Index);
fprintf(fp, " mov word [_z80sp], dx ; Store our new SP\n");
fprintf(fp, " xor edx, edx\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " cpu.z80sp = %s;\n", mz80Index);
}
else
assert(0);
}
void AddIndexHandler(UINT32 dwOpcode)
{
UINT8 bRegPair;
bRegPair = dwOpcode >> 4;
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
fprintf(fp, " mov dh, ah ; Get our flags\n");
fprintf(fp, " and dh, 0ech ; Preserve the top three and bits 2 & 3\n");
fprintf(fp, " mov [cyclesRemaining], edi\n");
fprintf(fp, " mov di, [_z80%s] ; Get our value\n", mz80Index);
fprintf(fp, " mov [_orgval], di ; Store our original value\n");
fprintf(fp, " add di, %s\n", pbIndexedRegPairs[(dwOpcode & 0x30) >> 4]);
fprintf(fp, " lahf\n");
fprintf(fp, " mov [_z80%s], di ; Store our register back\n", mz80Index);
fprintf(fp, " mov di, [_orgval] ; Get original\n");
fprintf(fp, " xor di, word [_z80%s] ; XOR It with our computed value\n", mz80Index);
fprintf(fp, " xor di, %s\n", pbIndexedRegPairs[(dwOpcode & 0x30) >> 4]);
fprintf(fp, " and di, 1000h ; Just our half carry\n");
fprintf(fp, " or dx, di ; Or in our flags\n");
fprintf(fp, " and ah, 01h ; Just carry\n");
fprintf(fp, " or ah, dh\n");
fprintf(fp, " mov edi, [cyclesRemaining]\n");
fprintf(fp, " xor edx, edx\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
if (bRegPair != 2)
{
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_CARRY | Z80_FLAG_NEGATIVE | Z80_FLAG_HALF_CARRY);\n");
fprintf(fp, " dwTemp = %s + %s;\n", mz80Index, pbRegPairsC[bRegPair]);
fprintf(fp, " cpu.z80F |= ((dwTemp >> 16) & Z80_FLAG_CARRY) | (((%s ^ dwTemp ^ %s) >> 8) & Z80_FLAG_HALF_CARRY);\n", mz80Index, pbRegPairsC[bRegPair]);
fprintf(fp, " %s = dwTemp & 0xffff;\n", mz80Index);
}
else
{
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_CARRY | Z80_FLAG_NEGATIVE | Z80_FLAG_HALF_CARRY);\n");
fprintf(fp, " dwTemp = %s + %s;\n", mz80Index, mz80Index);
fprintf(fp, " cpu.z80F |= ((dwTemp >> 16) & Z80_FLAG_CARRY) | (((%s ^ dwTemp ^ %s) >> 8) & Z80_FLAG_HALF_CARRY);\n", mz80Index, pbRegPairsC[bRegPair]);
fprintf(fp, " %s = dwTemp & 0xffff;\n", mz80Index);
}
}
else
assert(0);
}
void JPIXIYHandler(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
fprintf(fp, " mov dx, [_z80%s] ; Get our value\n", mz80Index);
fprintf(fp, " mov esi, edx ; New PC!\n");
fprintf(fp, " add esi, ebp ; Add in our base\n");
fprintf(fp, " xor edx, edx\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " pbPC = cpu.z80Base + %s;\n", mz80Index);
}
else
assert(0);
}
void IncDecIndexed(UINT32 dwOpcode)
{
UINT8 szIndex[30];
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
IndexedOffset(mz80Index);
fprintf(fp, " mov [_orgval], dx\n");
ReadValueFromMemory("dx", "dl");
fprintf(fp, " sahf\n");
if (dwOpcode == 0x34)
fprintf(fp, " inc dl\n");
else
fprintf(fp, " dec dl\n");
fprintf(fp, " lahf\n");
fprintf(fp, " o16 pushf\n");
fprintf(fp, " shl edx, 16\n");
fprintf(fp, " and ah, 0fbh ; Knock out parity/overflow\n");
fprintf(fp, " pop dx\n");
fprintf(fp, " and dh, 08h ; Just the overflow\n");
fprintf(fp, " shr dh, 1 ; Shift it into position\n");
fprintf(fp, " or ah, dh ; OR It in with the real flags\n");
fprintf(fp, " shr edx, 16\n");
if (dwOpcode == 0x34)
fprintf(fp, " and ah, 0fdh ; Knock out N!\n");
else
fprintf(fp, " or ah, 02h ; Make it N!\n");
WriteValueToMemory("[_orgval]", "dl");
fprintf(fp, " xor edx, edx\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " sdwAddr = (INT8) *pbPC++; /* Get LSB first */\n");
fprintf(fp, " dwAddr = (sdwAddr + (INT32) %s) & 0xffff;\n", mz80Index);
ReadValueFromMemory("dwAddr", "bTemp");
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_SIGN | Z80_FLAG_ZERO | Z80_FLAG_HALF_CARRY | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_NEGATIVE);\n");
if (0x34 == dwOpcode)
{
fprintf(fp ," cpu.z80F |= bPostIncFlags[bTemp++];\n");
}
else
{
fprintf(fp ," cpu.z80F |= bPostDecFlags[bTemp--];\n");
}
WriteValueToMemory("dwAddr", "bTemp");
}
else
assert(0);
}
void MathOperationIndexed(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
IndexedOffset(mz80Index);
ReadValueFromMemory("dx", "dl");
fprintf(fp, " sahf\n");
if (dwOpcode == 0x86) // Add
fprintf(fp, " add al, dl\n");
if (dwOpcode == 0x8e) // Adc
fprintf(fp, " adc al, dl\n");
if (dwOpcode == 0x96) // Sub
fprintf(fp, " sub al, dl\n");
if (dwOpcode == 0x9e) // Sbc
fprintf(fp, " sbb al, dl\n");
if (dwOpcode == 0xa6) // And
fprintf(fp, " and al, dl\n");
if (dwOpcode == 0xae) // Xor
fprintf(fp, " xor al, dl\n");
if (dwOpcode == 0xb6) // Or
fprintf(fp, " or al, dl\n");
if (dwOpcode == 0xbe) // Cp
fprintf(fp, " cmp al, dl\n");
fprintf(fp, " lahf\n");
if (dwOpcode == 0x86 || dwOpcode == 0x8e)
{
SetOverflow();
fprintf(fp, " and ah, 0fdh ; Knock out negative\n");
}
if (dwOpcode == 0x96 || dwOpcode == 0x9e || dwOpcode == 0xbe)
{
SetOverflow();
fprintf(fp, " or ah, 02h ; Set negative\n");
}
if (dwOpcode == 0xae || dwOpcode == 0xb6)
fprintf(fp, " and ah, 0ech ; Knock out H, N, and C\n");
if (dwOpcode == 0xa6)
{
fprintf(fp, " and ah,0fch ; Knock out N & C\n");
fprintf(fp, " or ah, 10h ; Set half carry\n");
}
fprintf(fp, " xor edx, edx\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " sdwAddr = (INT8) *pbPC++; /* Get LSB first */\n");
fprintf(fp, " dwAddr = (sdwAddr + (INT32) %s) & 0xffff;\n", mz80Index);
ReadValueFromMemory("dwAddr", "bTemp");
if (0x86 == dwOpcode) // ADD A, (IX/IY+nn)
{
SetAddFlagsSZHVC("cpu.z80A", "bTemp");
fprintf(fp, " cpu.z80A += bTemp;\n");
}
else
if (0x8e == dwOpcode) // ADC A, (IX/IY+nn)
{
fprintf(fp, " bTemp2 = (cpu.z80F & Z80_FLAG_CARRY);\n");
SetAdcFlagsSZHVC("cpu.z80A", "bTemp");
fprintf(fp, " cpu.z80A += bTemp + bTemp2;\n");
}
else
if (0x96 == dwOpcode) // SUB A, (IX/IY+nn)
{
SetSubFlagsSZHVC("cpu.z80A", "bTemp");
fprintf(fp, " cpu.z80A -= bTemp;\n");
}
else
if (0x9e == dwOpcode) // SBC A, (IX/IY+nn)
{
fprintf(fp, " bTemp2 = cpu.z80A;\n");
fprintf(fp, " cpu.z80A = cpu.z80A - bTemp - (cpu.z80F & Z80_FLAG_CARRY);\n");
SetSbcFlagsSZHVC("bTemp2", "bTemp");
}
else
if (0xa6 == dwOpcode) // AND A, (IX/IY+nn)
{
fprintf(fp, " cpu.z80A &= bTemp;\n");
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_CARRY | Z80_FLAG_NEGATIVE | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_HALF_CARRY | Z80_FLAG_ZERO | Z80_FLAG_SIGN);\n");
fprintf(fp, " cpu.z80F |= bPostANDFlags[cpu.z80A];\n\n");
}
else
if (0xae == dwOpcode) // XOR A, (IX/IY+nn)
{
fprintf(fp, " cpu.z80A ^= bTemp;\n");
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_CARRY | Z80_FLAG_NEGATIVE | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_HALF_CARRY | Z80_FLAG_ZERO | Z80_FLAG_SIGN);\n");
fprintf(fp, " cpu.z80F |= bPostORFlags[cpu.z80A];\n\n");
}
else
if (0xb6 == dwOpcode) // OR A, (IX/IY+nn)
{
fprintf(fp, " cpu.z80A |= bTemp;\n");
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_CARRY | Z80_FLAG_NEGATIVE | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_HALF_CARRY | Z80_FLAG_ZERO | Z80_FLAG_SIGN);\n");
fprintf(fp, " cpu.z80F |= bPostORFlags[cpu.z80A];\n\n");
}
else
if (0xbe == dwOpcode) // CP A, (IX/IY+nn)
{
SetSubFlagsSZHVC("cpu.z80A", "bTemp");
}
else
InvalidInstructionC(2);
}
else
assert(0);
}
void UndocIndexToReg(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
if ((dwOpcode & 0x07) == 2 || (dwOpcode & 0x07) == 3)
fprintf(fp, " mov dx, [_z80de] ; Get DE\n");
if ((dwOpcode & 0x07) == 4)
fprintf(fp, " mov dh, byte [_z80%s + 1]\n", mz80Index);
if ((dwOpcode & 0x07) == 5)
fprintf(fp, " mov dl, byte [_z80%s]\n", mz80Index);
fprintf(fp, " mov byte [_z80%s + %ld], %s\n", mz80Index, 1 - ((dwOpcode & 0x08) >> 3), pbLocalReg[dwOpcode & 0x07]);
fprintf(fp, " xor edx, edx\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
if (dwOpcode != 0x64 && dwOpcode != 0x65 && dwOpcode != 0x6c && dwOpcode != 0x6d)
{
if (dwOpcode & 0x08)
fprintf(fp, " %s = %s;\n", mz80IndexHalfLow, pbLocalRegC[dwOpcode & 0x07]);
else
fprintf(fp, " %s = %s;\n", mz80IndexHalfHigh, pbLocalRegC[dwOpcode & 0x07]);
}
else // IX/IY High/low weirdness afoot...
{
// We don't generate any code for ld indexH, indexH and ld indexL, indexL
if (0x65 == dwOpcode) // LD indexH, indexL
{
fprintf(fp, " %s = %s;\n", mz80IndexHalfHigh, mz80IndexHalfLow);
}
else
if (0x6c == dwOpcode) // LD indexH, indexL
{
fprintf(fp, " %s = %s;\n", mz80IndexHalfLow, mz80IndexHalfHigh);
}
}
}
else
assert(0);
}
void UndocRegToIndex(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
if ((dwOpcode & 0x38) == 0x10 || (dwOpcode & 0x38) == 0x18)
fprintf(fp, " mov dx, [_z80de] ; Get a usable copy of DE here\n");
fprintf(fp, " mov %s, byte [_z80%s + %ld]\n", pbLocalReg[(dwOpcode >> 3) & 0x07], mz80Index, 1 - (dwOpcode & 1));
if ((dwOpcode & 0x38) == 0x10 || (dwOpcode & 0x38) == 0x18)
fprintf(fp, " mov [_z80de], dx ; Put it back!\n");
fprintf(fp, " xor edx, edx\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
if (dwOpcode & 1)
fprintf(fp, " %s = %s;\n", pbLocalRegC[(dwOpcode >> 3) & 0x07], mz80IndexHalfLow);
else
fprintf(fp, " %s = %s;\n", pbLocalRegC[(dwOpcode >> 3) & 0x07], mz80IndexHalfHigh);
}
else
assert(0);
}
void LoadImmediate(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
fprintf(fp, " mov dx, [esi] ; Get our word to load\n");
fprintf(fp, " add esi, 2 ; Advance past the word\n");
fprintf(fp, " mov [_z80%s], dx ; Store our new value\n", mz80Index);
fprintf(fp, " xor edx, edx\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, " %s = *pbPC++;\n", mz80Index);
fprintf(fp, " %s |= ((UINT32) *pbPC++ << 8);\n", mz80Index);
}
else
assert(0);
}
void PushPopOperationsIndexed(UINT32 dwOpcode)
{
UINT8 bRegPair;
UINT8 bRegBaseLsb[25];
UINT8 bRegBaseMsb[25];
UINT8 string[150];
if (MZ80_ASSEMBLY_X86 == bWhat)
{
sprintf(bRegBaseLsb, "byte [_z80%s]", mz80Index);
sprintf(bRegBaseMsb, "byte [_z80%s + 1]", mz80Index);
sprintf(string, "[_z80%s]", mz80Index);
ProcBegin(dwOpcode);
if (dwOpcode == 0xe5) // Push IX/IY
{
fprintf(fp, " sub word [_z80sp], 2\n");
fprintf(fp, " mov dx, [_z80sp]\n");
WriteWordToMemory("dx", string);
}
else // Pop
{
fprintf(fp, " mov dx, [_z80sp]\n");
ReadWordFromMemory("dx", string);
fprintf(fp, " add word [_z80sp], 2\n");
}
fprintf(fp, " xor edx, edx\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
if (0xe5 == dwOpcode) // Push IX/IY
{
fprintf(fp, " cpu.z80sp -= 2;\n");
fprintf(fp, " pbSP = (cpu.z80Base + cpu.z80sp); /* Normalize the stack pointer */\n");
WriteWordToMemory("cpu.z80sp", mz80Index);
}
else
if (0xe1 == dwOpcode) // Pop IX/IY
{
ReadWordFromMemory("cpu.z80sp", mz80Index);
fprintf(fp, " cpu.z80sp += 2;\n");
fprintf(fp, " pbSP = (cpu.z80Base + cpu.z80sp); /* Normalize the stack pointer */\n");
return;
}
}
else
assert(0);
}
// DDFD XXCB Instructions
void ddcbBitWise(UINT32 dwOpcode)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
ProcBegin(dwOpcode);
// NOTE: _orgval contains the address to get from. It includes the offset
// already computed plus the mz80Index register.
// Read our byte
fprintf(fp, " mov dx, [_orgval] ; Get our target address\n");
ReadValueFromMemory("dx", "dl");
// Do the operation
if (dwOpcode != 0x06 && dwOpcode != 0x0e &&
dwOpcode != 0x16 && dwOpcode != 0x1e &&
dwOpcode != 0x26 && dwOpcode != 0x2e &&
dwOpcode != 0x3e && (dwOpcode & 0xc7) != 0x86 &&
(dwOpcode & 0xc7) != 0xc6)
{
fprintf(fp, " mov dh, ah ; Store our original flags\n");
fprintf(fp, " and dh, 29h ; Keep our old flags\n");
}
if ((dwOpcode & 0xc7) != 0x86 && (dwOpcode & 0xc7) != 0xc6)
fprintf(fp, " sahf ; Restore our flags\n");
if (dwOpcode == 0x06)
fprintf(fp, " rol dl, 1\n");
if (dwOpcode == 0x0e)
fprintf(fp, " ror dl, 1\n");
if (dwOpcode == 0x16)
fprintf(fp, " rcl dl, 1\n");
if (dwOpcode == 0x1e)
fprintf(fp, " rcr dl, 1\n");
if (dwOpcode == 0x26)
fprintf(fp, " shl dl, 1\n");
if (dwOpcode == 0x2e)
fprintf(fp, " sar dl, 1\n");
if (dwOpcode == 0x3e)
fprintf(fp, " shr dl, 1\n");
// BIT, AND, and OR
if ((dwOpcode & 0xc7) == 0x46)
fprintf(fp, " test dl, 0%.2xh ; Is it set?\n", (1 << ((dwOpcode >> 3) & 0x07)));
else
if ((dwOpcode & 0xc7) == 0x86)
fprintf(fp, " and dl, 0%.2xh ; Reset the bit\n",
0xff - (1 << ((dwOpcode >> 3) & 0x07)));
else
if ((dwOpcode & 0xc7) == 0xc6)
fprintf(fp, " or dl, 0%.2xh ; Set the bit\n",
(1 << ((dwOpcode >> 3) & 0x07)));
if ((dwOpcode & 0xc7) != 0x86 && (dwOpcode & 0xc7) != 0xc6)
fprintf(fp, " lahf ; Get our flags back\n");
// Do the flag fixup (if any)
if (dwOpcode == 0x26 || dwOpcode == 0x2e || ((dwOpcode & 0xc7) == 0x46))
fprintf(fp, " and ah, 0edh ; No Half carry or negative!\n");
if (dwOpcode == 0x06 || dwOpcode == 0x0e ||
dwOpcode == 0x16 || dwOpcode == 0x1e ||
dwOpcode == 0x3e)
fprintf(fp, " and ah, 0edh ; Knock out H & N\n");
// BIT!
if ((dwOpcode & 0xc7) == 0x46)
{
fprintf(fp, " or ah, 10h ; OR In our half carry\n");
fprintf(fp, " and ah, 0d0h ; New flags\n");
fprintf(fp, " or ah, dh ; OR In our old flags\n");
}
// Now write our data back if it's not a BIT instruction
if ((dwOpcode & 0xc7) != 0x46) // If it's not a BIT, write it back
WriteValueToMemory("[_orgval]", "dl");
fprintf(fp, " xor edx, edx\n");
FetchNextInstruction(dwOpcode);
}
else
if (MZ80_C == bWhat)
{
if (0x06 == dwOpcode) // RLC
{
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_ZERO | Z80_FLAG_SIGN | Z80_FLAG_HALF_CARRY | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_NEGATIVE | Z80_FLAG_CARRY);\n");
fprintf(fp, " bTemp2 = (bTemp >> 7);\n");
fprintf(fp, " bTemp = (bTemp << 1) | bTemp2;\n");
fprintf(fp, " cpu.z80F |= bTemp2 | bPostORFlags[bTemp];\n");
WriteValueToMemory("dwAddr", "bTemp");
}
else
if (0x0e == dwOpcode) // RRC
{
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_ZERO | Z80_FLAG_SIGN | Z80_FLAG_HALF_CARRY | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_NEGATIVE | Z80_FLAG_CARRY);\n");
fprintf(fp, " cpu.z80F |= (bTemp & Z80_FLAG_CARRY);\n");
fprintf(fp, " bTemp = (bTemp >> 1) | (bTemp << 7);\n");
fprintf(fp, " cpu.z80F |= bPostORFlags[bTemp];\n");
WriteValueToMemory("dwAddr", "bTemp");
}
else
if (0x16 == dwOpcode) // RL
{
fprintf(fp, " bTemp2 = cpu.z80F & Z80_FLAG_CARRY;\n");
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_ZERO | Z80_FLAG_SIGN | Z80_FLAG_HALF_CARRY | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_NEGATIVE | Z80_FLAG_CARRY);\n");
fprintf(fp, " cpu.z80F |= (bTemp >> 7);\n");
fprintf(fp, " bTemp = (bTemp << 1) | bTemp2;\n");
fprintf(fp, " cpu.z80F |= bPostORFlags[bTemp];\n");
WriteValueToMemory("dwAddr", "bTemp");
}
else
if (0x1e == dwOpcode) // RR
{
fprintf(fp, " bTemp2 = (cpu.z80F & Z80_FLAG_CARRY) << 7;\n");
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_ZERO | Z80_FLAG_SIGN | Z80_FLAG_HALF_CARRY | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_NEGATIVE | Z80_FLAG_CARRY);\n");
fprintf(fp, " cpu.z80F |= (bTemp & Z80_FLAG_CARRY);\n");
fprintf(fp, " bTemp = (bTemp >> 1) | bTemp2;\n");
fprintf(fp, " cpu.z80F |= bPostORFlags[bTemp];\n");
WriteValueToMemory("dwAddr", "bTemp");
}
else
if (0x26 == dwOpcode) // SLA
{
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_ZERO | Z80_FLAG_SIGN | Z80_FLAG_HALF_CARRY | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_NEGATIVE | Z80_FLAG_CARRY);\n");
fprintf(fp, " cpu.z80F |= (bTemp >> 7);\n");
fprintf(fp, " bTemp = (bTemp << 1);\n");
fprintf(fp, " cpu.z80F |= bPostORFlags[bTemp];\n");
WriteValueToMemory("dwAddr", "bTemp");
}
else
if (0x2e == dwOpcode) // SRA
{
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_ZERO | Z80_FLAG_SIGN | Z80_FLAG_HALF_CARRY | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_NEGATIVE | Z80_FLAG_CARRY);\n");
fprintf(fp, " cpu.z80F |= (bTemp & Z80_FLAG_CARRY);\n");
fprintf(fp, " bTemp = (bTemp >> 1) | (bTemp & 0x80);\n");
fprintf(fp, " cpu.z80F |= bPostORFlags[bTemp];\n");
WriteValueToMemory("dwAddr", "bTemp");
}
else
if (0x3e == dwOpcode) // SRL
{
fprintf(fp, " cpu.z80F &= ~(Z80_FLAG_ZERO | Z80_FLAG_SIGN | Z80_FLAG_HALF_CARRY | Z80_FLAG_OVERFLOW_PARITY | Z80_FLAG_NEGATIVE | Z80_FLAG_CARRY);\n");
fprintf(fp, " cpu.z80F |= (bTemp & Z80_FLAG_CARRY);\n");
fprintf(fp, " bTemp = (bTemp >> 1);\n");
fprintf(fp, " cpu.z80F |= bPostORFlags[bTemp];\n");
WriteValueToMemory("dwAddr", "bTemp");
}
else
if ((dwOpcode & 0xc0) == 0x40) // BIT
{
fprintf(fp, " cpu.z80F = (cpu.z80F & ~(Z80_FLAG_ZERO | Z80_FLAG_NEGATIVE)) | Z80_FLAG_HALF_CARRY;\n");
fprintf(fp, " if (!(bTemp & 0x%.2x))\n", 1 << ((dwOpcode >> 3) & 0x07));
fprintf(fp, " {\n");
fprintf(fp, " cpu.z80F |= Z80_FLAG_ZERO;\n");
fprintf(fp, " }\n");
}
else
if ((dwOpcode & 0xc0) == 0x80) // RES
{
fprintf(fp, " bTemp &= 0x%.2x;\n", ~(1 << ((dwOpcode >> 3) & 0x07)) & 0xff);
WriteValueToMemory("dwAddr", "bTemp");
}
else
if ((dwOpcode & 0xc0) == 0xC0) // SET
{
fprintf(fp, " bTemp |= 0x%.2x;\n", 1 << ((dwOpcode >> 3) & 0x07));
WriteValueToMemory("dwAddr", "bTemp");
}
else
InvalidInstructionC(4);
}
else
assert(0);
}
GetTicksCode()
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
fprintf(fp, " global _%sGetElapsedTicks\n", cpubasename);
fprintf(fp, " global %sGetElapsedTicks_\n", cpubasename);
fprintf(fp, " global %sGetElapsedTicks\n", cpubasename);
Alignment();
sprintf(procname, "%sGetElapsedTicks_", cpubasename);
ProcBegin(0xffffffff);
fprintf(fp, "_%sGetElapsedTicks:\n", cpubasename);
fprintf(fp, "%sGetElapsedTicks:\n", cpubasename);
if (bUseStack)
fprintf(fp, " mov eax, [esp+4] ; Get our context address\n");
fprintf(fp, " or eax, eax ; Should we clear it?\n");
fprintf(fp, " jz getTicks\n");
fprintf(fp, " xor eax, eax\n");
fprintf(fp, " xchg eax, [dwElapsedTicks]\n");
fprintf(fp, " ret\n");
fprintf(fp, "getTicks:\n");
fprintf(fp, " mov eax, [dwElapsedTicks]\n");
fprintf(fp, " ret\n");
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, "/* This will return the elapsed ticks */\n\n");
fprintf(fp, "UINT32 %sGetElapsedTicks(UINT32 dwClear)\n", cpubasename);
fprintf(fp, "{\n");
fprintf(fp, " UINT32 dwTemp = dwElapsedTicks;\n\n");
fprintf(fp, " if (dwClear)\n");
fprintf(fp, " {\n");
fprintf(fp, " dwElapsedTicks = 0;\n");
fprintf(fp, " }\n\n");
fprintf(fp, " return(dwTemp);\n");
fprintf(fp, "}\n\n");
}
else
{
assert(0);
}
}
ReleaseTimesliceCode()
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
fprintf(fp, " global _%sReleaseTimeslice\n", cpubasename);
fprintf(fp, " global %sReleaseTimeslice_\n", cpubasename);
fprintf(fp, " global %sReleaseTimeslice\n", cpubasename);
Alignment();
sprintf(procname, "%sReleaseTimeslice_", cpubasename);
ProcBegin(0xffffffff);
fprintf(fp, "_%sReleaseTimeslice:\n", cpubasename);
fprintf(fp, "%sReleaseTimeslice:\n", cpubasename);
fprintf(fp, " mov eax, [cyclesRemaining]\n");
fprintf(fp, " sub [dwOriginalExec], eax\n");
fprintf(fp, " mov [cyclesRemaining], dword 0\n");
fprintf(fp, " ret\n\n");
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, "/* Releases mz80 from its current timeslice */\n\n");
fprintf(fp, "void %sReleaseTimeslice(void)\n", cpubasename);
fprintf(fp, "{\n");
fprintf(fp, " dwOriginalCycles -= sdwCyclesRemaining;\n");
fprintf(fp, " sdwCyclesRemaining = 0;\n");
fprintf(fp, "}\n\n");
}
else
{
assert(0);
}
}
DataSegment()
{
UINT32 dwLoop = 0;
UINT8 bUsed[256];
if (MZ80_ASSEMBLY_X86 == bWhat)
{
if (bOS2)
fprintf(fp, " section .DATA32 use32 flat class=data\n");
else
fprintf(fp, " section .data use32 flat class=data\n");
Alignment();
fprintf(fp, " global _%scontextBegin\n", cpubasename);
fprintf(fp, "_%scontextBegin:\n", cpubasename);
fprintf(fp, " global _z80pc\n");
fprintf(fp, " global z80pc_\n");
if (bPlain)
fprintf(fp, " global z80pc\n");
fprintf(fp, " global _z80nmiAddr\n");
fprintf(fp, " global _z80intAddr\n");
fprintf(fp, " global z80intAddr\n");
fprintf(fp, "\n");
fprintf(fp, "; DO NOT CHANGE THE ORDER OF AF, BC, DE, HL and THE PRIME REGISTERS!\n");
fprintf(fp, "\n");
fprintf(fp, "_z80Base dd 0 ; Base address for Z80 stuff\n");
fprintf(fp, "_z80MemRead dd 0 ; Offset of memory read structure array\n");
fprintf(fp, "_z80MemWrite dd 0 ; Offset of memory write structure array\n");
fprintf(fp, "_z80IoRead dd 0 ; Base address for I/O reads list\n");
fprintf(fp, "_z80IoWrite dd 0 ; Base address for I/O write list\n");
fprintf(fp, "_z80clockticks dd 0 ; # Of clock tips that have elapsed\n");
fprintf(fp, "_z80iff dd 0 ; Non-zero if we're in an interrupt\n");
fprintf(fp, "_z80interruptMode dd 0 ; Interrupt mode\n");
fprintf(fp, "_z80halted dd 0 ; 0=Not halted, 1=Halted\n");
#ifdef MZ80_TRAP
fprintf(fp, "_z80trapList dd 0 ; pointer to trap list\n");
fprintf(fp, "_z80trapAddr dw 0 ; PC where trap occurred\n");
#endif
fprintf(fp, "_z80af dd 0 ; A Flag & Flags\n");
fprintf(fp, "_z80bc dd 0 ; BC\n");
fprintf(fp, "_z80de dd 0 ; DE\n");
fprintf(fp, "_z80hl dd 0 ; HL\n");
fprintf(fp, "_z80afprime dd 0 ; A Flag & Flags prime\n");
fprintf(fp, "_z80bcprime dd 0 ; BC prime\n");
fprintf(fp, "_z80deprime dd 0 ; DE prime\n");
fprintf(fp, "_z80hlprime dd 0 ; HL prime\n");
fprintf(fp, "\n");
fprintf(fp, "; The order of the following registers can be changed without adverse\n");
fprintf(fp, "; effect. Keep the WORD and DWORDs on boundaries of two for faster access\n");
fprintf(fp, "\n");
fprintf(fp, "_z80ix dd 0 ; IX\n");
fprintf(fp, "_z80iy dd 0 ; IY\n");
fprintf(fp, "_z80sp dd 0 ; Stack pointer\n");
if (bPlain)
fprintf(fp,"z80pc:\n");
fprintf(fp, "z80pc_:\n");
fprintf(fp, "_z80pc dd 0 ; PC\n");
fprintf(fp, "_z80nmiAddr dd 0 ; Address to jump to for NMI\n");
fprintf(fp, "z80intAddr:\n");
fprintf(fp, "_z80intAddr dd 0 ; Address to jump to for INT\n");
fprintf(fp, "_z80rCounter dd 0 ; R Register counter\n");
fprintf(fp, "_z80i db 0 ; I register\n");
fprintf(fp, "_z80r db 0 ; R register\n");
fprintf(fp, "_z80intPending db 0 ; Non-zero if an interrupt is pending\n");
fprintf(fp, "\n");
fprintf(fp, "_%scontextEnd:\n", cpubasename);
Alignment();
fprintf(fp, "dwElapsedTicks dd 0 ; # Of ticks elapsed\n");
fprintf(fp, "cyclesRemaining dd 0 ; # Of cycles remaining\n");
fprintf(fp, "dwOriginalExec dd 0 ; # Of cycles originally executing\n");
fprintf(fp, "dwLastRSample dd 0 ; Last sample for R computation\n");
fprintf(fp, "dwEITiming dd 0 ; Used when we cause an interrupt\n");
fprintf(fp, "_orgval dw 0 ; Scratch area\n");
fprintf(fp, "_orgval2 dw 0 ; Scratch area\n");
fprintf(fp, "_wordval dw 0 ; Scratch area\n");
fprintf(fp, "_intData db 0 ; Interrupt data when an interrupt is pending\n");
fprintf(fp, "bEIExit db 0 ; Are we exiting because of an EI instruction?\n");
fprintf(fp, "\n");
// Debugger junk
fprintf(fp, "RegTextPC db 'PC',0\n");
fprintf(fp, "RegTextAF db 'AF',0\n");
fprintf(fp, "RegTextBC db 'BC',0\n");
fprintf(fp, "RegTextDE db 'DE',0\n");
fprintf(fp, "RegTextHL db 'HL',0\n");
fprintf(fp, "RegTextAFP db 'AF',27h,0\n");
fprintf(fp, "RegTextBCP db 'BC',27h,0\n");
fprintf(fp, "RegTextDEP db 'DE',27h,0\n");
fprintf(fp, "RegTextHLP db 'HL',27h,0\n");
fprintf(fp, "RegTextIX db 'IX',0\n");
fprintf(fp, "RegTextIY db 'IY',0\n");
fprintf(fp, "RegTextSP db 'SP',0\n");
fprintf(fp, "RegTextI db 'I',0\n");
fprintf(fp, "RegTextR db 'R',0\n");
// 8 Byte textual info
fprintf(fp, "RegTextA db 'A',0\n");
fprintf(fp, "RegTextB db 'B',0\n");
fprintf(fp, "RegTextC db 'C',0\n");
fprintf(fp, "RegTextD db 'D',0\n");
fprintf(fp, "RegTextE db 'E',0\n");
fprintf(fp, "RegTextH db 'H',0\n");
fprintf(fp, "RegTextL db 'L',0\n");
fprintf(fp, "RegTextF db 'F',0\n");
// Individual flags
fprintf(fp, "RegTextCarry db 'Carry',0\n");
fprintf(fp, "RegTextNegative db 'Negative',0\n");
fprintf(fp, "RegTextParity db 'Parity',0\n");
fprintf(fp, "RegTextOverflow db 'Overflow',0\n");
fprintf(fp, "RegTextHalfCarry db 'HalfCarry',0\n");
fprintf(fp, "RegTextZero db 'Zero',0\n");
fprintf(fp, "RegTextSign db 'Sign',0\n");
fprintf(fp, "RegTextIFF1 db 'IFF1',0\n");
fprintf(fp, "RegTextIFF2 db 'IFF2',0\n\n");
// Timing for interrupt modes
fprintf(fp, "intModeTStates:\n");
fprintf(fp, " db 13 ; IM 0 - 13 T-States\n");
fprintf(fp, " db 11 ; IM 1 - 11 T-States\n");
fprintf(fp, " db 11 ; IM 2 - 11 T-States\n\n");
// Now the master reg/flag table
fprintf(fp, "\n;\n");
fprintf(fp, "; Info is in: pointer to text, address, shift value, mask value, size of data chunk\n");
fprintf(fp, ";\n\n");
fprintf(fp, "RegTable:\n");
// Pointer to text, address, shift value, mask, size
fprintf(fp, " dd RegTextPC, _z80pc - _%scontextBegin, 0, 0ffffh\n", cpubasename);
fprintf(fp, " dd RegTextSP, _z80sp - _%scontextBegin, 0, 0ffffh\n", cpubasename);
fprintf(fp, " dd RegTextAF, _z80af - _%scontextBegin, 0, 0ffffh\n", cpubasename);
fprintf(fp, " dd RegTextBC, _z80bc - _%scontextBegin, 0, 0ffffh\n", cpubasename);
fprintf(fp, " dd RegTextDE, _z80de - _%scontextBegin, 0, 0ffffh\n", cpubasename);
fprintf(fp, " dd RegTextHL, _z80hl - _%scontextBegin, 0, 0ffffh\n", cpubasename);
fprintf(fp, " dd RegTextAFP, _z80af - _%scontextBegin, 0, 0ffffh\n", cpubasename);
fprintf(fp, " dd RegTextBCP, _z80bc - _%scontextBegin, 0, 0ffffh\n", cpubasename);
fprintf(fp, " dd RegTextDEP, _z80de - _%scontextBegin, 0, 0ffffh\n", cpubasename);
fprintf(fp, " dd RegTextHLP, _z80hl - _%scontextBegin, 0, 0ffffh\n", cpubasename);
fprintf(fp, " dd RegTextIX, _z80ix - _%scontextBegin, 0, 0ffffh\n", cpubasename);
fprintf(fp, " dd RegTextIY, _z80iy - _%scontextBegin, 0, 0ffffh\n", cpubasename);
fprintf(fp, " dd RegTextI, _z80i - _%scontextBegin, 0, 0ffh\n", cpubasename);
fprintf(fp, " dd RegTextR, _z80r - _%scontextBegin, 0, 0ffh\n", cpubasename);
// Individual regs
fprintf(fp, " dd RegTextA, (_z80af + 1) - _%scontextBegin, 0, 0ffh\n", cpubasename);
fprintf(fp, " dd RegTextF, _z80af - _%scontextBegin, 0, 0ffh\n", cpubasename);
fprintf(fp, " dd RegTextB, (_z80bc + 1) - _%scontextBegin, 0, 0ffh\n", cpubasename);
fprintf(fp, " dd RegTextC, _z80bc - _%scontextBegin, 0, 0ffh\n", cpubasename);
fprintf(fp, " dd RegTextD, (_z80de + 1) - _%scontextBegin, 0, 0ffh\n", cpubasename);
fprintf(fp, " dd RegTextE, _z80de - _%scontextBegin, 0, 0ffh\n", cpubasename);
fprintf(fp, " dd RegTextH, (_z80hl + 1) - _%scontextBegin, 0, 0ffh\n", cpubasename);
fprintf(fp, " dd RegTextL, _z80hl - _%scontextBegin, 0, 0ffh\n", cpubasename);
// IFF register
fprintf(fp, " dd RegTextIFF1, _z80iff - _%scontextBegin, 0, 01h\n", cpubasename);
fprintf(fp, " dd RegTextIFF2, _z80iff - _%scontextBegin, 1, 01h\n", cpubasename);
// Individual flags
fprintf(fp, " dd RegTextCarry, _z80af - _%scontextBegin, 0, 01h\n", cpubasename);
fprintf(fp, " dd RegTextNegative, _z80af - _%scontextBegin, 1, 01h\n", cpubasename);
fprintf(fp, " dd RegTextParity, _z80af - _%scontextBegin, 2, 01h\n", cpubasename);
fprintf(fp, " dd RegTextOverflow, _z80af - _%scontextBegin, 2, 01h\n", cpubasename);
fprintf(fp, " dd RegTextHalfCarry, _z80af - _%scontextBegin, 4, 01h\n", cpubasename);
fprintf(fp, " dd RegTextZero, _z80af - _%scontextBegin, 6, 01h\n", cpubasename);
fprintf(fp, " dd RegTextSign, _z80af - _%scontextBegin, 7, 01h\n", cpubasename);
// Now we write out our tables
Alignment();
for (dwLoop = 0; dwLoop < 256; dwLoop++)
bUsed[dwLoop] = 0;
// Now rip through and find out what is and isn't used
dwLoop = 0;
while (StandardOps[dwLoop].Emitter)
{
assert(StandardOps[dwLoop].bOpCode < 0x100);
if (bUsed[StandardOps[dwLoop].bOpCode])
{
fprintf(stderr, "Oops! %.2x\n", dwLoop);
fclose(fp);
exit(1);
}
bUsed[StandardOps[dwLoop].bOpCode] = 1;
dwLoop++;
}
// Now that that's taken care of, emit the table
fprintf(fp, "z80regular:\n");
dwLoop = 0;
while (dwLoop < 0x100)
{
fprintf(fp, " dd ");
if (bUsed[dwLoop])
fprintf(fp, "RegInst%.2x", dwLoop);
else
fprintf(fp, "invalidInsByte");
fprintf(fp, "\n");
dwLoop++;
}
fprintf(fp, "\n");
// Now rip through and find out what is and isn't used (CB Ops)
for (dwLoop = 0; dwLoop < 0x100; dwLoop++)
bUsed[dwLoop] = 0;
dwLoop = 0;
while (CBOps[dwLoop].Emitter)
{
assert(CBOps[dwLoop].bOpCode < 0x100);
if (bUsed[CBOps[dwLoop].bOpCode])
{
fprintf(stderr, "Oops CB! %.2x\n", dwLoop);
fclose(fp);
exit(1);
}
bUsed[CBOps[dwLoop].bOpCode] = 1;
dwLoop++;
}
dwLoop = 0;
// Let's emit the CB prefixes
fprintf(fp, "z80PrefixCB:\n");
while (dwLoop < 0x100)
{
fprintf(fp, " dd ");
if (bUsed[dwLoop])
fprintf(fp, "CBInst%.2x", dwLoop);
else
fprintf(fp, "invalidInsWord");
fprintf(fp, "\n");
dwLoop++;
}
fprintf(fp, "\n");
// Now rip through and find out what is and isn't used (ED Ops)
for (dwLoop = 0; dwLoop < 0x100; dwLoop++)
bUsed[dwLoop] = 0;
dwLoop = 0;
while (EDOps[dwLoop].Emitter)
{
assert(EDOps[dwLoop].bOpCode < 0x100);
if (bUsed[EDOps[dwLoop].bOpCode])
{
fprintf(stderr, "Oops ED! %.2x\n", dwLoop);
fclose(fp);
exit(1);
}
bUsed[EDOps[dwLoop].bOpCode] = 1;
dwLoop++;
}
dwLoop = 0;
// Let's emit the ED prefixes
fprintf(fp, "z80PrefixED:\n");
while (dwLoop < 0x100)
{
fprintf(fp, " dd ");
if (bUsed[dwLoop])
fprintf(fp, "EDInst%.2x", dwLoop);
else
fprintf(fp, "invalidInsWord");
fprintf(fp, "\n");
dwLoop++;
}
fprintf(fp, "\n");
// Now rip through and find out what is and isn't used (DD Ops)
for (dwLoop = 0; dwLoop < 0x100; dwLoop++)
bUsed[dwLoop] = 0;
dwLoop = 0;
while (DDFDOps[dwLoop].Emitter)
{
assert(DDFDOps[dwLoop].bOpCode < 0x100);
if (bUsed[DDFDOps[dwLoop].bOpCode])
{
fprintf(stderr, "Oops DD! %.2x\n", bUsed[DDFDOps[dwLoop].bOpCode]);
fclose(fp);
exit(1);
}
bUsed[DDFDOps[dwLoop].bOpCode] = 1;
dwLoop++;
}
dwLoop = 0;
// Let's emit the DD prefixes
fprintf(fp, "z80PrefixDD:\n");
while (dwLoop < 0x100)
{
fprintf(fp, " dd ");
if (bUsed[dwLoop])
fprintf(fp, "DDInst%.2x", dwLoop);
else
fprintf(fp, "invalidInsWord");
fprintf(fp, "\n");
dwLoop++;
}
fprintf(fp, "\n");
// Now rip through and find out what is and isn't used (FD Ops)
for (dwLoop = 0; dwLoop < 0x100; dwLoop++)
bUsed[dwLoop] = 0;
dwLoop = 0;
while (DDFDOps[dwLoop].Emitter)
{
assert(DDFDOps[dwLoop].bOpCode < 0x100);
if (bUsed[DDFDOps[dwLoop].bOpCode])
{
fprintf(stderr, "Oops FD! %.2x\n", dwLoop);
fclose(fp);
exit(1);
}
bUsed[DDFDOps[dwLoop].bOpCode] = 1;
dwLoop++;
}
for (dwLoop = 0; dwLoop < 0x100; dwLoop++)
bUsed[dwLoop] = 0;
// Let's emit the DDFD prefixes
for (dwLoop = 0; dwLoop < 0x100; dwLoop++)
bUsed[dwLoop] = 0;
dwLoop = 0;
while (DDFDOps[dwLoop].Emitter)
{
assert(DDFDOps[dwLoop].bOpCode < 0x100);
if (bUsed[DDFDOps[dwLoop].bOpCode])
{
fprintf(stderr, "Oops FD! %.2x\n", dwLoop);
exit(1);
}
bUsed[DDFDOps[dwLoop].bOpCode] = 1;
dwLoop++;
}
dwLoop = 0;
// Let's emit the DDFD prefixes
fprintf(fp, "z80PrefixFD:\n");
while (dwLoop < 0x100)
{
fprintf(fp, " dd ");
if (bUsed[dwLoop])
fprintf(fp, "FDInst%.2x", dwLoop);
else
fprintf(fp, "invalidInsWord");
fprintf(fp, "\n");
dwLoop++;
}
for (dwLoop = 0; dwLoop < 0x100; dwLoop++)
bUsed[dwLoop] = 0;
dwLoop = 0;
while (DDFDCBOps[dwLoop].Emitter)
{
assert(DDFDCBOps[dwLoop].bOpCode < 0x100);
if (bUsed[DDFDCBOps[dwLoop].bOpCode])
{
fprintf(stderr, "Oops CBFDDD! %.2x\n", bUsed[DDFDCBOps[dwLoop].bOpCode]);
fclose(fp);
exit(1);
}
bUsed[DDFDCBOps[dwLoop].bOpCode] = 1;
dwLoop++;
}
// Let's emit the DDFD prefixes
dwLoop = 0;
fprintf(fp, "z80ddfdcbInstructions:\n");
while (dwLoop < 0x100)
{
fprintf(fp, " dd ");
if (bUsed[dwLoop])
fprintf(fp, "DDFDCBInst%.2x", dwLoop);
else
fprintf(fp, "invalidInsWord");
fprintf(fp, "\n");
dwLoop++;
}
fprintf(fp, "\n");
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, "/* Modular global variables go here*/\n\n");
fprintf(fp, "static CONTEXTMZ80 cpu; /* CPU Context */\n");
fprintf(fp, "static UINT8 *pbPC; /* Program counter normalized */\n");
fprintf(fp, "static UINT8 *pbSP; /* Stack pointer normalized */\n");
fprintf(fp, "static struct MemoryReadByte *psMemRead; /* Read memory structure */\n");
fprintf(fp, "static struct MemoryWriteByte *psMemWrite; /* Write memory structure */\n");
fprintf(fp, "static struct z80PortRead *psIoRead; /* Read I/O structure */\n");
fprintf(fp, "static struct z80PortWrite *psIoWrite; /* Write memory structure */\n");
fprintf(fp, "static INT32 sdwCyclesRemaining; /* Used as a countdown */\n");
fprintf(fp, "static UINT32 dwReturnCode; /* Return code from exec() */\n");
fprintf(fp, "static UINT32 dwOriginalCycles; /* How many cycles did we start with? */\n");
fprintf(fp, "static UINT32 dwElapsedTicks; /* How many ticks did we elapse? */\n");
fprintf(fp, "static INT32 sdwAddr; /* Temporary address storage */\n");
fprintf(fp, "static UINT32 dwAddr; /* Temporary stack address */\n");
fprintf(fp, "static UINT8 *pbAddAdcTable; /* Pointer to add/adc flag table */\n");
fprintf(fp, "static UINT8 *pbSubSbcTable; /* Pointer to sub/sbc flag table */\n");
fprintf(fp, "static UINT32 dwTemp; /* Temporary value */\n\n");
fprintf(fp, "static UINT8 bTemp; /* Temporary value */\n\n");
fprintf(fp, "static UINT8 bTemp2; /* Temporary value */\n\n");
fprintf(fp, "/* Precomputed flag tables */\n\n");
fprintf(fp, "static UINT8 bPostIncFlags[0x100] = \n");
fprintf(fp, "{\n");
fprintf(fp, " 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,\n");
fprintf(fp, " 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,\n");
fprintf(fp, " 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,\n");
fprintf(fp, " 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,\n");
fprintf(fp, " 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,\n");
fprintf(fp, " 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,\n");
fprintf(fp, " 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,\n");
fprintf(fp, " 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x94,\n");
fprintf(fp, " 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x90,\n");
fprintf(fp, " 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x90,\n");
fprintf(fp, " 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x90,\n");
fprintf(fp, " 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x90,\n");
fprintf(fp, " 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x90,\n");
fprintf(fp, " 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x90,\n");
fprintf(fp, " 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x90,\n");
fprintf(fp, " 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x50\n");
fprintf(fp, "};\n\n");
fprintf(fp, "static UINT8 bPostDecFlags[0x100] = \n");
fprintf(fp, "{\n");
fprintf(fp, " 0x92,0x42,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,\n");
fprintf(fp, " 0x12,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,\n");
fprintf(fp, " 0x12,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,\n");
fprintf(fp, " 0x12,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,\n");
fprintf(fp, " 0x12,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,\n");
fprintf(fp, " 0x12,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,\n");
fprintf(fp, " 0x12,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,\n");
fprintf(fp, " 0x12,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,\n");
fprintf(fp, " 0x16,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,\n");
fprintf(fp, " 0x92,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,\n");
fprintf(fp, " 0x92,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,\n");
fprintf(fp, " 0x92,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,\n");
fprintf(fp, " 0x92,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,\n");
fprintf(fp, " 0x92,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,\n");
fprintf(fp, " 0x92,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,\n");
fprintf(fp, " 0x92,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x82\n");
fprintf(fp, "};\n\n");
fprintf(fp, "static UINT8 bPostORFlags[0x100] = \n");
fprintf(fp, "{\n");
fprintf(fp, " 0x44,0x00,0x00,0x04,0x00,0x04,0x04,0x00,0x00,0x04,0x04,0x00,0x04,0x00,0x00,0x04,\n");
fprintf(fp, " 0x00,0x04,0x04,0x00,0x04,0x00,0x00,0x04,0x04,0x00,0x00,0x04,0x00,0x04,0x04,0x00,\n");
fprintf(fp, " 0x00,0x04,0x04,0x00,0x04,0x00,0x00,0x04,0x04,0x00,0x00,0x04,0x00,0x04,0x04,0x00,\n");
fprintf(fp, " 0x04,0x00,0x00,0x04,0x00,0x04,0x04,0x00,0x00,0x04,0x04,0x00,0x04,0x00,0x00,0x04,\n");
fprintf(fp, " 0x00,0x04,0x04,0x00,0x04,0x00,0x00,0x04,0x04,0x00,0x00,0x04,0x00,0x04,0x04,0x00,\n");
fprintf(fp, " 0x04,0x00,0x00,0x04,0x00,0x04,0x04,0x00,0x00,0x04,0x04,0x00,0x04,0x00,0x00,0x04,\n");
fprintf(fp, " 0x04,0x00,0x00,0x04,0x00,0x04,0x04,0x00,0x00,0x04,0x04,0x00,0x04,0x00,0x00,0x04,\n");
fprintf(fp, " 0x00,0x04,0x04,0x00,0x04,0x00,0x00,0x04,0x04,0x00,0x00,0x04,0x00,0x04,0x04,0x00,\n");
fprintf(fp, " 0x80,0x84,0x84,0x80,0x84,0x80,0x80,0x84,0x84,0x80,0x80,0x84,0x80,0x84,0x84,0x80,\n");
fprintf(fp, " 0x84,0x80,0x80,0x84,0x80,0x84,0x84,0x80,0x80,0x84,0x84,0x80,0x84,0x80,0x80,0x84,\n");
fprintf(fp, " 0x84,0x80,0x80,0x84,0x80,0x84,0x84,0x80,0x80,0x84,0x84,0x80,0x84,0x80,0x80,0x84,\n");
fprintf(fp, " 0x80,0x84,0x84,0x80,0x84,0x80,0x80,0x84,0x84,0x80,0x80,0x84,0x80,0x84,0x84,0x80,\n");
fprintf(fp, " 0x84,0x80,0x80,0x84,0x80,0x84,0x84,0x80,0x80,0x84,0x84,0x80,0x84,0x80,0x80,0x84,\n");
fprintf(fp, " 0x80,0x84,0x84,0x80,0x84,0x80,0x80,0x84,0x84,0x80,0x80,0x84,0x80,0x84,0x84,0x80,\n");
fprintf(fp, " 0x80,0x84,0x84,0x80,0x84,0x80,0x80,0x84,0x84,0x80,0x80,0x84,0x80,0x84,0x84,0x80,\n");
fprintf(fp, " 0x84,0x80,0x80,0x84,0x80,0x84,0x84,0x80,0x80,0x84,0x84,0x80,0x84,0x80,0x80,0x84\n");
fprintf(fp, "};\n\n");
fprintf(fp, "static UINT8 bPostANDFlags[0x100] = \n");
fprintf(fp, "{\n");
fprintf(fp, " 0x54,0x10,0x10,0x14,0x10,0x14,0x14,0x10,0x10,0x14,0x14,0x10,0x14,0x10,0x10,0x14,\n");
fprintf(fp, " 0x10,0x14,0x14,0x10,0x14,0x10,0x10,0x14,0x14,0x10,0x10,0x14,0x10,0x14,0x14,0x10,\n");
fprintf(fp, " 0x10,0x14,0x14,0x10,0x14,0x10,0x10,0x14,0x14,0x10,0x10,0x14,0x10,0x14,0x14,0x10,\n");
fprintf(fp, " 0x14,0x10,0x10,0x14,0x10,0x14,0x14,0x10,0x10,0x14,0x14,0x10,0x14,0x10,0x10,0x14,\n");
fprintf(fp, " 0x10,0x14,0x14,0x10,0x14,0x10,0x10,0x14,0x14,0x10,0x10,0x14,0x10,0x14,0x14,0x10,\n");
fprintf(fp, " 0x14,0x10,0x10,0x14,0x10,0x14,0x14,0x10,0x10,0x14,0x14,0x10,0x14,0x10,0x10,0x14,\n");
fprintf(fp, " 0x14,0x10,0x10,0x14,0x10,0x14,0x14,0x10,0x10,0x14,0x14,0x10,0x14,0x10,0x10,0x14,\n");
fprintf(fp, " 0x10,0x14,0x14,0x10,0x14,0x10,0x10,0x14,0x14,0x10,0x10,0x14,0x10,0x14,0x14,0x10,\n");
fprintf(fp, " 0x90,0x94,0x94,0x90,0x94,0x90,0x90,0x94,0x94,0x90,0x90,0x94,0x90,0x94,0x94,0x90,\n");
fprintf(fp, " 0x94,0x90,0x90,0x94,0x90,0x94,0x94,0x90,0x90,0x94,0x94,0x90,0x94,0x90,0x90,0x94,\n");
fprintf(fp, " 0x94,0x90,0x90,0x94,0x90,0x94,0x94,0x90,0x90,0x94,0x94,0x90,0x94,0x90,0x90,0x94,\n");
fprintf(fp, " 0x90,0x94,0x94,0x90,0x94,0x90,0x90,0x94,0x94,0x90,0x90,0x94,0x90,0x94,0x94,0x90,\n");
fprintf(fp, " 0x94,0x90,0x90,0x94,0x90,0x94,0x94,0x90,0x90,0x94,0x94,0x90,0x94,0x90,0x90,0x94,\n");
fprintf(fp, " 0x90,0x94,0x94,0x90,0x94,0x90,0x90,0x94,0x94,0x90,0x90,0x94,0x90,0x94,0x94,0x90,\n");
fprintf(fp, " 0x90,0x94,0x94,0x90,0x94,0x90,0x90,0x94,0x94,0x90,0x90,0x94,0x90,0x94,0x94,0x90,\n");
fprintf(fp, " 0x94,0x90,0x90,0x94,0x90,0x94,0x94,0x90,0x90,0x94,0x94,0x90,0x94,0x90,0x90,0x94\n");
fprintf(fp, "};\n\n");
fprintf(fp, "static UINT16 wDAATable[0x800] = \n");
fprintf(fp, "{\n");
fprintf(fp, " 0x5400,0x1001,0x1002,0x1403,0x1004,0x1405,0x1406,0x1007,\n");
fprintf(fp, " 0x1008,0x1409,0x1010,0x1411,0x1412,0x1013,0x1414,0x1015,\n");
fprintf(fp, " 0x1010,0x1411,0x1412,0x1013,0x1414,0x1015,0x1016,0x1417,\n");
fprintf(fp, " 0x1418,0x1019,0x1020,0x1421,0x1422,0x1023,0x1424,0x1025,\n");
fprintf(fp, " 0x1020,0x1421,0x1422,0x1023,0x1424,0x1025,0x1026,0x1427,\n");
fprintf(fp, " 0x1428,0x1029,0x1430,0x1031,0x1032,0x1433,0x1034,0x1435,\n");
fprintf(fp, " 0x1430,0x1031,0x1032,0x1433,0x1034,0x1435,0x1436,0x1037,\n");
fprintf(fp, " 0x1038,0x1439,0x1040,0x1441,0x1442,0x1043,0x1444,0x1045,\n");
fprintf(fp, " 0x1040,0x1441,0x1442,0x1043,0x1444,0x1045,0x1046,0x1447,\n");
fprintf(fp, " 0x1448,0x1049,0x1450,0x1051,0x1052,0x1453,0x1054,0x1455,\n");
fprintf(fp, " 0x1450,0x1051,0x1052,0x1453,0x1054,0x1455,0x1456,0x1057,\n");
fprintf(fp, " 0x1058,0x1459,0x1460,0x1061,0x1062,0x1463,0x1064,0x1465,\n");
fprintf(fp, " 0x1460,0x1061,0x1062,0x1463,0x1064,0x1465,0x1466,0x1067,\n");
fprintf(fp, " 0x1068,0x1469,0x1070,0x1471,0x1472,0x1073,0x1474,0x1075,\n");
fprintf(fp, " 0x1070,0x1471,0x1472,0x1073,0x1474,0x1075,0x1076,0x1477,\n");
fprintf(fp, " 0x1478,0x1079,0x9080,0x9481,0x9482,0x9083,0x9484,0x9085,\n");
fprintf(fp, " 0x9080,0x9481,0x9482,0x9083,0x9484,0x9085,0x9086,0x9487,\n");
fprintf(fp, " 0x9488,0x9089,0x9490,0x9091,0x9092,0x9493,0x9094,0x9495,\n");
fprintf(fp, " 0x9490,0x9091,0x9092,0x9493,0x9094,0x9495,0x9496,0x9097,\n");
fprintf(fp, " 0x9098,0x9499,0x5500,0x1101,0x1102,0x1503,0x1104,0x1505,\n");
fprintf(fp, " 0x5500,0x1101,0x1102,0x1503,0x1104,0x1505,0x1506,0x1107,\n");
fprintf(fp, " 0x1108,0x1509,0x1110,0x1511,0x1512,0x1113,0x1514,0x1115,\n");
fprintf(fp, " 0x1110,0x1511,0x1512,0x1113,0x1514,0x1115,0x1116,0x1517,\n");
fprintf(fp, " 0x1518,0x1119,0x1120,0x1521,0x1522,0x1123,0x1524,0x1125,\n");
fprintf(fp, " 0x1120,0x1521,0x1522,0x1123,0x1524,0x1125,0x1126,0x1527,\n");
fprintf(fp, " 0x1528,0x1129,0x1530,0x1131,0x1132,0x1533,0x1134,0x1535,\n");
fprintf(fp, " 0x1530,0x1131,0x1132,0x1533,0x1134,0x1535,0x1536,0x1137,\n");
fprintf(fp, " 0x1138,0x1539,0x1140,0x1541,0x1542,0x1143,0x1544,0x1145,\n");
fprintf(fp, " 0x1140,0x1541,0x1542,0x1143,0x1544,0x1145,0x1146,0x1547,\n");
fprintf(fp, " 0x1548,0x1149,0x1550,0x1151,0x1152,0x1553,0x1154,0x1555,\n");
fprintf(fp, " 0x1550,0x1151,0x1152,0x1553,0x1154,0x1555,0x1556,0x1157,\n");
fprintf(fp, " 0x1158,0x1559,0x1560,0x1161,0x1162,0x1563,0x1164,0x1565,\n");
fprintf(fp, " 0x1560,0x1161,0x1162,0x1563,0x1164,0x1565,0x1566,0x1167,\n");
fprintf(fp, " 0x1168,0x1569,0x1170,0x1571,0x1572,0x1173,0x1574,0x1175,\n");
fprintf(fp, " 0x1170,0x1571,0x1572,0x1173,0x1574,0x1175,0x1176,0x1577,\n");
fprintf(fp, " 0x1578,0x1179,0x9180,0x9581,0x9582,0x9183,0x9584,0x9185,\n");
fprintf(fp, " 0x9180,0x9581,0x9582,0x9183,0x9584,0x9185,0x9186,0x9587,\n");
fprintf(fp, " 0x9588,0x9189,0x9590,0x9191,0x9192,0x9593,0x9194,0x9595,\n");
fprintf(fp, " 0x9590,0x9191,0x9192,0x9593,0x9194,0x9595,0x9596,0x9197,\n");
fprintf(fp, " 0x9198,0x9599,0x95a0,0x91a1,0x91a2,0x95a3,0x91a4,0x95a5,\n");
fprintf(fp, " 0x95a0,0x91a1,0x91a2,0x95a3,0x91a4,0x95a5,0x95a6,0x91a7,\n");
fprintf(fp, " 0x91a8,0x95a9,0x91b0,0x95b1,0x95b2,0x91b3,0x95b4,0x91b5,\n");
fprintf(fp, " 0x91b0,0x95b1,0x95b2,0x91b3,0x95b4,0x91b5,0x91b6,0x95b7,\n");
fprintf(fp, " 0x95b8,0x91b9,0x95c0,0x91c1,0x91c2,0x95c3,0x91c4,0x95c5,\n");
fprintf(fp, " 0x95c0,0x91c1,0x91c2,0x95c3,0x91c4,0x95c5,0x95c6,0x91c7,\n");
fprintf(fp, " 0x91c8,0x95c9,0x91d0,0x95d1,0x95d2,0x91d3,0x95d4,0x91d5,\n");
fprintf(fp, " 0x91d0,0x95d1,0x95d2,0x91d3,0x95d4,0x91d5,0x91d6,0x95d7,\n");
fprintf(fp, " 0x95d8,0x91d9,0x91e0,0x95e1,0x95e2,0x91e3,0x95e4,0x91e5,\n");
fprintf(fp, " 0x91e0,0x95e1,0x95e2,0x91e3,0x95e4,0x91e5,0x91e6,0x95e7,\n");
fprintf(fp, " 0x95e8,0x91e9,0x95f0,0x91f1,0x91f2,0x95f3,0x91f4,0x95f5,\n");
fprintf(fp, " 0x95f0,0x91f1,0x91f2,0x95f3,0x91f4,0x95f5,0x95f6,0x91f7,\n");
fprintf(fp, " 0x91f8,0x95f9,0x5500,0x1101,0x1102,0x1503,0x1104,0x1505,\n");
fprintf(fp, " 0x5500,0x1101,0x1102,0x1503,0x1104,0x1505,0x1506,0x1107,\n");
fprintf(fp, " 0x1108,0x1509,0x1110,0x1511,0x1512,0x1113,0x1514,0x1115,\n");
fprintf(fp, " 0x1110,0x1511,0x1512,0x1113,0x1514,0x1115,0x1116,0x1517,\n");
fprintf(fp, " 0x1518,0x1119,0x1120,0x1521,0x1522,0x1123,0x1524,0x1125,\n");
fprintf(fp, " 0x1120,0x1521,0x1522,0x1123,0x1524,0x1125,0x1126,0x1527,\n");
fprintf(fp, " 0x1528,0x1129,0x1530,0x1131,0x1132,0x1533,0x1134,0x1535,\n");
fprintf(fp, " 0x1530,0x1131,0x1132,0x1533,0x1134,0x1535,0x1536,0x1137,\n");
fprintf(fp, " 0x1138,0x1539,0x1140,0x1541,0x1542,0x1143,0x1544,0x1145,\n");
fprintf(fp, " 0x1140,0x1541,0x1542,0x1143,0x1544,0x1145,0x1146,0x1547,\n");
fprintf(fp, " 0x1548,0x1149,0x1550,0x1151,0x1152,0x1553,0x1154,0x1555,\n");
fprintf(fp, " 0x1550,0x1151,0x1152,0x1553,0x1154,0x1555,0x1556,0x1157,\n");
fprintf(fp, " 0x1158,0x1559,0x1560,0x1161,0x1162,0x1563,0x1164,0x1565,\n");
fprintf(fp, " 0x1406,0x1007,0x1008,0x1409,0x140a,0x100b,0x140c,0x100d,\n");
fprintf(fp, " 0x100e,0x140f,0x1010,0x1411,0x1412,0x1013,0x1414,0x1015,\n");
fprintf(fp, " 0x1016,0x1417,0x1418,0x1019,0x101a,0x141b,0x101c,0x141d,\n");
fprintf(fp, " 0x141e,0x101f,0x1020,0x1421,0x1422,0x1023,0x1424,0x1025,\n");
fprintf(fp, " 0x1026,0x1427,0x1428,0x1029,0x102a,0x142b,0x102c,0x142d,\n");
fprintf(fp, " 0x142e,0x102f,0x1430,0x1031,0x1032,0x1433,0x1034,0x1435,\n");
fprintf(fp, " 0x1436,0x1037,0x1038,0x1439,0x143a,0x103b,0x143c,0x103d,\n");
fprintf(fp, " 0x103e,0x143f,0x1040,0x1441,0x1442,0x1043,0x1444,0x1045,\n");
fprintf(fp, " 0x1046,0x1447,0x1448,0x1049,0x104a,0x144b,0x104c,0x144d,\n");
fprintf(fp, " 0x144e,0x104f,0x1450,0x1051,0x1052,0x1453,0x1054,0x1455,\n");
fprintf(fp, " 0x1456,0x1057,0x1058,0x1459,0x145a,0x105b,0x145c,0x105d,\n");
fprintf(fp, " 0x105e,0x145f,0x1460,0x1061,0x1062,0x1463,0x1064,0x1465,\n");
fprintf(fp, " 0x1466,0x1067,0x1068,0x1469,0x146a,0x106b,0x146c,0x106d,\n");
fprintf(fp, " 0x106e,0x146f,0x1070,0x1471,0x1472,0x1073,0x1474,0x1075,\n");
fprintf(fp, " 0x1076,0x1477,0x1478,0x1079,0x107a,0x147b,0x107c,0x147d,\n");
fprintf(fp, " 0x147e,0x107f,0x9080,0x9481,0x9482,0x9083,0x9484,0x9085,\n");
fprintf(fp, " 0x9086,0x9487,0x9488,0x9089,0x908a,0x948b,0x908c,0x948d,\n");
fprintf(fp, " 0x948e,0x908f,0x9490,0x9091,0x9092,0x9493,0x9094,0x9495,\n");
fprintf(fp, " 0x9496,0x9097,0x9098,0x9499,0x949a,0x909b,0x949c,0x909d,\n");
fprintf(fp, " 0x909e,0x949f,0x5500,0x1101,0x1102,0x1503,0x1104,0x1505,\n");
fprintf(fp, " 0x1506,0x1107,0x1108,0x1509,0x150a,0x110b,0x150c,0x110d,\n");
fprintf(fp, " 0x110e,0x150f,0x1110,0x1511,0x1512,0x1113,0x1514,0x1115,\n");
fprintf(fp, " 0x1116,0x1517,0x1518,0x1119,0x111a,0x151b,0x111c,0x151d,\n");
fprintf(fp, " 0x151e,0x111f,0x1120,0x1521,0x1522,0x1123,0x1524,0x1125,\n");
fprintf(fp, " 0x1126,0x1527,0x1528,0x1129,0x112a,0x152b,0x112c,0x152d,\n");
fprintf(fp, " 0x152e,0x112f,0x1530,0x1131,0x1132,0x1533,0x1134,0x1535,\n");
fprintf(fp, " 0x1536,0x1137,0x1138,0x1539,0x153a,0x113b,0x153c,0x113d,\n");
fprintf(fp, " 0x113e,0x153f,0x1140,0x1541,0x1542,0x1143,0x1544,0x1145,\n");
fprintf(fp, " 0x1146,0x1547,0x1548,0x1149,0x114a,0x154b,0x114c,0x154d,\n");
fprintf(fp, " 0x154e,0x114f,0x1550,0x1151,0x1152,0x1553,0x1154,0x1555,\n");
fprintf(fp, " 0x1556,0x1157,0x1158,0x1559,0x155a,0x115b,0x155c,0x115d,\n");
fprintf(fp, " 0x115e,0x155f,0x1560,0x1161,0x1162,0x1563,0x1164,0x1565,\n");
fprintf(fp, " 0x1566,0x1167,0x1168,0x1569,0x156a,0x116b,0x156c,0x116d,\n");
fprintf(fp, " 0x116e,0x156f,0x1170,0x1571,0x1572,0x1173,0x1574,0x1175,\n");
fprintf(fp, " 0x1176,0x1577,0x1578,0x1179,0x117a,0x157b,0x117c,0x157d,\n");
fprintf(fp, " 0x157e,0x117f,0x9180,0x9581,0x9582,0x9183,0x9584,0x9185,\n");
fprintf(fp, " 0x9186,0x9587,0x9588,0x9189,0x918a,0x958b,0x918c,0x958d,\n");
fprintf(fp, " 0x958e,0x918f,0x9590,0x9191,0x9192,0x9593,0x9194,0x9595,\n");
fprintf(fp, " 0x9596,0x9197,0x9198,0x9599,0x959a,0x919b,0x959c,0x919d,\n");
fprintf(fp, " 0x919e,0x959f,0x95a0,0x91a1,0x91a2,0x95a3,0x91a4,0x95a5,\n");
fprintf(fp, " 0x95a6,0x91a7,0x91a8,0x95a9,0x95aa,0x91ab,0x95ac,0x91ad,\n");
fprintf(fp, " 0x91ae,0x95af,0x91b0,0x95b1,0x95b2,0x91b3,0x95b4,0x91b5,\n");
fprintf(fp, " 0x91b6,0x95b7,0x95b8,0x91b9,0x91ba,0x95bb,0x91bc,0x95bd,\n");
fprintf(fp, " 0x95be,0x91bf,0x95c0,0x91c1,0x91c2,0x95c3,0x91c4,0x95c5,\n");
fprintf(fp, " 0x95c6,0x91c7,0x91c8,0x95c9,0x95ca,0x91cb,0x95cc,0x91cd,\n");
fprintf(fp, " 0x91ce,0x95cf,0x91d0,0x95d1,0x95d2,0x91d3,0x95d4,0x91d5,\n");
fprintf(fp, " 0x91d6,0x95d7,0x95d8,0x91d9,0x91da,0x95db,0x91dc,0x95dd,\n");
fprintf(fp, " 0x95de,0x91df,0x91e0,0x95e1,0x95e2,0x91e3,0x95e4,0x91e5,\n");
fprintf(fp, " 0x91e6,0x95e7,0x95e8,0x91e9,0x91ea,0x95eb,0x91ec,0x95ed,\n");
fprintf(fp, " 0x95ee,0x91ef,0x95f0,0x91f1,0x91f2,0x95f3,0x91f4,0x95f5,\n");
fprintf(fp, " 0x95f6,0x91f7,0x91f8,0x95f9,0x95fa,0x91fb,0x95fc,0x91fd,\n");
fprintf(fp, " 0x91fe,0x95ff,0x5500,0x1101,0x1102,0x1503,0x1104,0x1505,\n");
fprintf(fp, " 0x1506,0x1107,0x1108,0x1509,0x150a,0x110b,0x150c,0x110d,\n");
fprintf(fp, " 0x110e,0x150f,0x1110,0x1511,0x1512,0x1113,0x1514,0x1115,\n");
fprintf(fp, " 0x1116,0x1517,0x1518,0x1119,0x111a,0x151b,0x111c,0x151d,\n");
fprintf(fp, " 0x151e,0x111f,0x1120,0x1521,0x1522,0x1123,0x1524,0x1125,\n");
fprintf(fp, " 0x1126,0x1527,0x1528,0x1129,0x112a,0x152b,0x112c,0x152d,\n");
fprintf(fp, " 0x152e,0x112f,0x1530,0x1131,0x1132,0x1533,0x1134,0x1535,\n");
fprintf(fp, " 0x1536,0x1137,0x1138,0x1539,0x153a,0x113b,0x153c,0x113d,\n");
fprintf(fp, " 0x113e,0x153f,0x1140,0x1541,0x1542,0x1143,0x1544,0x1145,\n");
fprintf(fp, " 0x1146,0x1547,0x1548,0x1149,0x114a,0x154b,0x114c,0x154d,\n");
fprintf(fp, " 0x154e,0x114f,0x1550,0x1151,0x1152,0x1553,0x1154,0x1555,\n");
fprintf(fp, " 0x1556,0x1157,0x1158,0x1559,0x155a,0x115b,0x155c,0x115d,\n");
fprintf(fp, " 0x115e,0x155f,0x1560,0x1161,0x1162,0x1563,0x1164,0x1565,\n");
fprintf(fp, " 0x5600,0x1201,0x1202,0x1603,0x1204,0x1605,0x1606,0x1207,\n");
fprintf(fp, " 0x1208,0x1609,0x1204,0x1605,0x1606,0x1207,0x1208,0x1609,\n");
fprintf(fp, " 0x1210,0x1611,0x1612,0x1213,0x1614,0x1215,0x1216,0x1617,\n");
fprintf(fp, " 0x1618,0x1219,0x1614,0x1215,0x1216,0x1617,0x1618,0x1219,\n");
fprintf(fp, " 0x1220,0x1621,0x1622,0x1223,0x1624,0x1225,0x1226,0x1627,\n");
fprintf(fp, " 0x1628,0x1229,0x1624,0x1225,0x1226,0x1627,0x1628,0x1229,\n");
fprintf(fp, " 0x1630,0x1231,0x1232,0x1633,0x1234,0x1635,0x1636,0x1237,\n");
fprintf(fp, " 0x1238,0x1639,0x1234,0x1635,0x1636,0x1237,0x1238,0x1639,\n");
fprintf(fp, " 0x1240,0x1641,0x1642,0x1243,0x1644,0x1245,0x1246,0x1647,\n");
fprintf(fp, " 0x1648,0x1249,0x1644,0x1245,0x1246,0x1647,0x1648,0x1249,\n");
fprintf(fp, " 0x1650,0x1251,0x1252,0x1653,0x1254,0x1655,0x1656,0x1257,\n");
fprintf(fp, " 0x1258,0x1659,0x1254,0x1655,0x1656,0x1257,0x1258,0x1659,\n");
fprintf(fp, " 0x1660,0x1261,0x1262,0x1663,0x1264,0x1665,0x1666,0x1267,\n");
fprintf(fp, " 0x1268,0x1669,0x1264,0x1665,0x1666,0x1267,0x1268,0x1669,\n");
fprintf(fp, " 0x1270,0x1671,0x1672,0x1273,0x1674,0x1275,0x1276,0x1677,\n");
fprintf(fp, " 0x1678,0x1279,0x1674,0x1275,0x1276,0x1677,0x1678,0x1279,\n");
fprintf(fp, " 0x9280,0x9681,0x9682,0x9283,0x9684,0x9285,0x9286,0x9687,\n");
fprintf(fp, " 0x9688,0x9289,0x9684,0x9285,0x9286,0x9687,0x9688,0x9289,\n");
fprintf(fp, " 0x9690,0x9291,0x9292,0x9693,0x9294,0x9695,0x9696,0x9297,\n");
fprintf(fp, " 0x9298,0x9699,0x1334,0x1735,0x1736,0x1337,0x1338,0x1739,\n");
fprintf(fp, " 0x1340,0x1741,0x1742,0x1343,0x1744,0x1345,0x1346,0x1747,\n");
fprintf(fp, " 0x1748,0x1349,0x1744,0x1345,0x1346,0x1747,0x1748,0x1349,\n");
fprintf(fp, " 0x1750,0x1351,0x1352,0x1753,0x1354,0x1755,0x1756,0x1357,\n");
fprintf(fp, " 0x1358,0x1759,0x1354,0x1755,0x1756,0x1357,0x1358,0x1759,\n");
fprintf(fp, " 0x1760,0x1361,0x1362,0x1763,0x1364,0x1765,0x1766,0x1367,\n");
fprintf(fp, " 0x1368,0x1769,0x1364,0x1765,0x1766,0x1367,0x1368,0x1769,\n");
fprintf(fp, " 0x1370,0x1771,0x1772,0x1373,0x1774,0x1375,0x1376,0x1777,\n");
fprintf(fp, " 0x1778,0x1379,0x1774,0x1375,0x1376,0x1777,0x1778,0x1379,\n");
fprintf(fp, " 0x9380,0x9781,0x9782,0x9383,0x9784,0x9385,0x9386,0x9787,\n");
fprintf(fp, " 0x9788,0x9389,0x9784,0x9385,0x9386,0x9787,0x9788,0x9389,\n");
fprintf(fp, " 0x9790,0x9391,0x9392,0x9793,0x9394,0x9795,0x9796,0x9397,\n");
fprintf(fp, " 0x9398,0x9799,0x9394,0x9795,0x9796,0x9397,0x9398,0x9799,\n");
fprintf(fp, " 0x97a0,0x93a1,0x93a2,0x97a3,0x93a4,0x97a5,0x97a6,0x93a7,\n");
fprintf(fp, " 0x93a8,0x97a9,0x93a4,0x97a5,0x97a6,0x93a7,0x93a8,0x97a9,\n");
fprintf(fp, " 0x93b0,0x97b1,0x97b2,0x93b3,0x97b4,0x93b5,0x93b6,0x97b7,\n");
fprintf(fp, " 0x97b8,0x93b9,0x97b4,0x93b5,0x93b6,0x97b7,0x97b8,0x93b9,\n");
fprintf(fp, " 0x97c0,0x93c1,0x93c2,0x97c3,0x93c4,0x97c5,0x97c6,0x93c7,\n");
fprintf(fp, " 0x93c8,0x97c9,0x93c4,0x97c5,0x97c6,0x93c7,0x93c8,0x97c9,\n");
fprintf(fp, " 0x93d0,0x97d1,0x97d2,0x93d3,0x97d4,0x93d5,0x93d6,0x97d7,\n");
fprintf(fp, " 0x97d8,0x93d9,0x97d4,0x93d5,0x93d6,0x97d7,0x97d8,0x93d9,\n");
fprintf(fp, " 0x93e0,0x97e1,0x97e2,0x93e3,0x97e4,0x93e5,0x93e6,0x97e7,\n");
fprintf(fp, " 0x97e8,0x93e9,0x97e4,0x93e5,0x93e6,0x97e7,0x97e8,0x93e9,\n");
fprintf(fp, " 0x97f0,0x93f1,0x93f2,0x97f3,0x93f4,0x97f5,0x97f6,0x93f7,\n");
fprintf(fp, " 0x93f8,0x97f9,0x93f4,0x97f5,0x97f6,0x93f7,0x93f8,0x97f9,\n");
fprintf(fp, " 0x5700,0x1301,0x1302,0x1703,0x1304,0x1705,0x1706,0x1307,\n");
fprintf(fp, " 0x1308,0x1709,0x1304,0x1705,0x1706,0x1307,0x1308,0x1709,\n");
fprintf(fp, " 0x1310,0x1711,0x1712,0x1313,0x1714,0x1315,0x1316,0x1717,\n");
fprintf(fp, " 0x1718,0x1319,0x1714,0x1315,0x1316,0x1717,0x1718,0x1319,\n");
fprintf(fp, " 0x1320,0x1721,0x1722,0x1323,0x1724,0x1325,0x1326,0x1727,\n");
fprintf(fp, " 0x1728,0x1329,0x1724,0x1325,0x1326,0x1727,0x1728,0x1329,\n");
fprintf(fp, " 0x1730,0x1331,0x1332,0x1733,0x1334,0x1735,0x1736,0x1337,\n");
fprintf(fp, " 0x1338,0x1739,0x1334,0x1735,0x1736,0x1337,0x1338,0x1739,\n");
fprintf(fp, " 0x1340,0x1741,0x1742,0x1343,0x1744,0x1345,0x1346,0x1747,\n");
fprintf(fp, " 0x1748,0x1349,0x1744,0x1345,0x1346,0x1747,0x1748,0x1349,\n");
fprintf(fp, " 0x1750,0x1351,0x1352,0x1753,0x1354,0x1755,0x1756,0x1357,\n");
fprintf(fp, " 0x1358,0x1759,0x1354,0x1755,0x1756,0x1357,0x1358,0x1759,\n");
fprintf(fp, " 0x1760,0x1361,0x1362,0x1763,0x1364,0x1765,0x1766,0x1367,\n");
fprintf(fp, " 0x1368,0x1769,0x1364,0x1765,0x1766,0x1367,0x1368,0x1769,\n");
fprintf(fp, " 0x1370,0x1771,0x1772,0x1373,0x1774,0x1375,0x1376,0x1777,\n");
fprintf(fp, " 0x1778,0x1379,0x1774,0x1375,0x1376,0x1777,0x1778,0x1379,\n");
fprintf(fp, " 0x9380,0x9781,0x9782,0x9383,0x9784,0x9385,0x9386,0x9787,\n");
fprintf(fp, " 0x9788,0x9389,0x9784,0x9385,0x9386,0x9787,0x9788,0x9389,\n");
fprintf(fp, " 0x9790,0x9391,0x9392,0x9793,0x9394,0x9795,0x9796,0x9397,\n");
fprintf(fp, " 0x9398,0x9799,0x9394,0x9795,0x9796,0x9397,0x9398,0x9799,\n");
fprintf(fp, " 0x97fa,0x93fb,0x97fc,0x93fd,0x93fe,0x97ff,0x5600,0x1201,\n");
fprintf(fp, " 0x1202,0x1603,0x1204,0x1605,0x1606,0x1207,0x1208,0x1609,\n");
fprintf(fp, " 0x160a,0x120b,0x160c,0x120d,0x120e,0x160f,0x1210,0x1611,\n");
fprintf(fp, " 0x1612,0x1213,0x1614,0x1215,0x1216,0x1617,0x1618,0x1219,\n");
fprintf(fp, " 0x121a,0x161b,0x121c,0x161d,0x161e,0x121f,0x1220,0x1621,\n");
fprintf(fp, " 0x1622,0x1223,0x1624,0x1225,0x1226,0x1627,0x1628,0x1229,\n");
fprintf(fp, " 0x122a,0x162b,0x122c,0x162d,0x162e,0x122f,0x1630,0x1231,\n");
fprintf(fp, " 0x1232,0x1633,0x1234,0x1635,0x1636,0x1237,0x1238,0x1639,\n");
fprintf(fp, " 0x163a,0x123b,0x163c,0x123d,0x123e,0x163f,0x1240,0x1641,\n");
fprintf(fp, " 0x1642,0x1243,0x1644,0x1245,0x1246,0x1647,0x1648,0x1249,\n");
fprintf(fp, " 0x124a,0x164b,0x124c,0x164d,0x164e,0x124f,0x1650,0x1251,\n");
fprintf(fp, " 0x1252,0x1653,0x1254,0x1655,0x1656,0x1257,0x1258,0x1659,\n");
fprintf(fp, " 0x165a,0x125b,0x165c,0x125d,0x125e,0x165f,0x1660,0x1261,\n");
fprintf(fp, " 0x1262,0x1663,0x1264,0x1665,0x1666,0x1267,0x1268,0x1669,\n");
fprintf(fp, " 0x166a,0x126b,0x166c,0x126d,0x126e,0x166f,0x1270,0x1671,\n");
fprintf(fp, " 0x1672,0x1273,0x1674,0x1275,0x1276,0x1677,0x1678,0x1279,\n");
fprintf(fp, " 0x127a,0x167b,0x127c,0x167d,0x167e,0x127f,0x9280,0x9681,\n");
fprintf(fp, " 0x9682,0x9283,0x9684,0x9285,0x9286,0x9687,0x9688,0x9289,\n");
fprintf(fp, " 0x928a,0x968b,0x928c,0x968d,0x968e,0x928f,0x9690,0x9291,\n");
fprintf(fp, " 0x9292,0x9693,0x1334,0x1735,0x1736,0x1337,0x1338,0x1739,\n");
fprintf(fp, " 0x173a,0x133b,0x173c,0x133d,0x133e,0x173f,0x1340,0x1741,\n");
fprintf(fp, " 0x1742,0x1343,0x1744,0x1345,0x1346,0x1747,0x1748,0x1349,\n");
fprintf(fp, " 0x134a,0x174b,0x134c,0x174d,0x174e,0x134f,0x1750,0x1351,\n");
fprintf(fp, " 0x1352,0x1753,0x1354,0x1755,0x1756,0x1357,0x1358,0x1759,\n");
fprintf(fp, " 0x175a,0x135b,0x175c,0x135d,0x135e,0x175f,0x1760,0x1361,\n");
fprintf(fp, " 0x1362,0x1763,0x1364,0x1765,0x1766,0x1367,0x1368,0x1769,\n");
fprintf(fp, " 0x176a,0x136b,0x176c,0x136d,0x136e,0x176f,0x1370,0x1771,\n");
fprintf(fp, " 0x1772,0x1373,0x1774,0x1375,0x1376,0x1777,0x1778,0x1379,\n");
fprintf(fp, " 0x137a,0x177b,0x137c,0x177d,0x177e,0x137f,0x9380,0x9781,\n");
fprintf(fp, " 0x9782,0x9383,0x9784,0x9385,0x9386,0x9787,0x9788,0x9389,\n");
fprintf(fp, " 0x938a,0x978b,0x938c,0x978d,0x978e,0x938f,0x9790,0x9391,\n");
fprintf(fp, " 0x9392,0x9793,0x9394,0x9795,0x9796,0x9397,0x9398,0x9799,\n");
fprintf(fp, " 0x979a,0x939b,0x979c,0x939d,0x939e,0x979f,0x97a0,0x93a1,\n");
fprintf(fp, " 0x93a2,0x97a3,0x93a4,0x97a5,0x97a6,0x93a7,0x93a8,0x97a9,\n");
fprintf(fp, " 0x97aa,0x93ab,0x97ac,0x93ad,0x93ae,0x97af,0x93b0,0x97b1,\n");
fprintf(fp, " 0x97b2,0x93b3,0x97b4,0x93b5,0x93b6,0x97b7,0x97b8,0x93b9,\n");
fprintf(fp, " 0x93ba,0x97bb,0x93bc,0x97bd,0x97be,0x93bf,0x97c0,0x93c1,\n");
fprintf(fp, " 0x93c2,0x97c3,0x93c4,0x97c5,0x97c6,0x93c7,0x93c8,0x97c9,\n");
fprintf(fp, " 0x97ca,0x93cb,0x97cc,0x93cd,0x93ce,0x97cf,0x93d0,0x97d1,\n");
fprintf(fp, " 0x97d2,0x93d3,0x97d4,0x93d5,0x93d6,0x97d7,0x97d8,0x93d9,\n");
fprintf(fp, " 0x93da,0x97db,0x93dc,0x97dd,0x97de,0x93df,0x93e0,0x97e1,\n");
fprintf(fp, " 0x97e2,0x93e3,0x97e4,0x93e5,0x93e6,0x97e7,0x97e8,0x93e9,\n");
fprintf(fp, " 0x93ea,0x97eb,0x93ec,0x97ed,0x97ee,0x93ef,0x97f0,0x93f1,\n");
fprintf(fp, " 0x93f2,0x97f3,0x93f4,0x97f5,0x97f6,0x93f7,0x93f8,0x97f9,\n");
fprintf(fp, " 0x97fa,0x93fb,0x97fc,0x93fd,0x93fe,0x97ff,0x5700,0x1301,\n");
fprintf(fp, " 0x1302,0x1703,0x1304,0x1705,0x1706,0x1307,0x1308,0x1709,\n");
fprintf(fp, " 0x170a,0x130b,0x170c,0x130d,0x130e,0x170f,0x1310,0x1711,\n");
fprintf(fp, " 0x1712,0x1313,0x1714,0x1315,0x1316,0x1717,0x1718,0x1319,\n");
fprintf(fp, " 0x131a,0x171b,0x131c,0x171d,0x171e,0x131f,0x1320,0x1721,\n");
fprintf(fp, " 0x1722,0x1323,0x1724,0x1325,0x1326,0x1727,0x1728,0x1329,\n");
fprintf(fp, " 0x132a,0x172b,0x132c,0x172d,0x172e,0x132f,0x1730,0x1331,\n");
fprintf(fp, " 0x1332,0x1733,0x1334,0x1735,0x1736,0x1337,0x1338,0x1739,\n");
fprintf(fp, " 0x173a,0x133b,0x173c,0x133d,0x133e,0x173f,0x1340,0x1741,\n");
fprintf(fp, " 0x1742,0x1343,0x1744,0x1345,0x1346,0x1747,0x1748,0x1349,\n");
fprintf(fp, " 0x134a,0x174b,0x134c,0x174d,0x174e,0x134f,0x1750,0x1351,\n");
fprintf(fp, " 0x1352,0x1753,0x1354,0x1755,0x1756,0x1357,0x1358,0x1759,\n");
fprintf(fp, " 0x175a,0x135b,0x175c,0x135d,0x135e,0x175f,0x1760,0x1361,\n");
fprintf(fp, " 0x1362,0x1763,0x1364,0x1765,0x1766,0x1367,0x1368,0x1769,\n");
fprintf(fp, " 0x176a,0x136b,0x176c,0x136d,0x136e,0x176f,0x1370,0x1771,\n");
fprintf(fp, " 0x1772,0x1373,0x1774,0x1375,0x1376,0x1777,0x1778,0x1379,\n");
fprintf(fp, " 0x137a,0x177b,0x137c,0x177d,0x177e,0x137f,0x9380,0x9781,\n");
fprintf(fp, " 0x9782,0x9383,0x9784,0x9385,0x9386,0x9787,0x9788,0x9389,\n");
fprintf(fp, " 0x938a,0x978b,0x938c,0x978d,0x978e,0x938f,0x9790,0x9391,\n");
fprintf(fp, " 0x9392,0x9793,0x9394,0x9795,0x9796,0x9397,0x9398,0x9799 \n");
fprintf(fp, "};\n\n");
fprintf(fp, "void DDFDCBHandler(UINT32 dwWhich);\n\n");
fprintf(fp, "\n");
}
else
{
assert(0);
}
}
CodeSegmentBegin()
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
fprintf(fp, " section .text use32 flat class=code\n");
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, "static void InvalidInstruction(UINT32 dwCount)\n");
fprintf(fp, "{\n");
fprintf(fp, " pbPC -= dwCount; /* Invalid instruction - back up */\n");
fprintf(fp, " dwReturnCode = (UINT32) pbPC - (UINT32) cpu.z80Base;\n");
fprintf(fp, " dwOriginalCycles -= sdwCyclesRemaining;\n");
fprintf(fp, " sdwCyclesRemaining = 0;\n");
fprintf(fp, "}\n\n");
}
else
{
assert(0);
}
}
CodeSegmentEnd()
{
}
ProgramEnd()
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
fprintf(fp, " end\n");
}
else
if (MZ80_C == bWhat)
{
}
else
{
assert(0);
}
}
EmitRegularInstructions()
{
UINT32 dwLoop = 0;
UINT32 dwLoop2 = 0;
bCurrentMode = TIMING_REGULAR;
if (MZ80_ASSEMBLY_X86 == bWhat)
{
while (dwLoop < 0x100)
{
dwLoop2 = 0;
sprintf(procname, "RegInst%.2x", dwLoop);
while (StandardOps[dwLoop2].bOpCode != dwLoop && StandardOps[dwLoop2].bOpCode != 0xffffffff)
dwLoop2++;
assert(dwLoop2 < 0x100);
if (StandardOps[dwLoop2].Emitter
&& StandardOps[dwLoop2].bOpCode != 0xffffffff)
StandardOps[dwLoop2].Emitter((UINT32) dwLoop);
dwLoop++;
}
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, "/* Main execution entry point */\n\n");
fprintf(fp, "UINT32 %sexec(UINT32 dwCycles)\n", cpubasename);
fprintf(fp, "{\n");
fprintf(fp, " UINT8 bOpcode;\n\n");
fprintf(fp, " dwReturnCode = 0x80000000; /* Assume it'll work */\n");
fprintf(fp, " sdwCyclesRemaining = dwCycles;\n");
fprintf(fp, " dwOriginalCycles = dwCycles;\n");
fprintf(fp, " if (cpu.z80halted)\n");
fprintf(fp, " {\n");
fprintf(fp, " dwElapsedTicks += dwCycles;\n");
fprintf(fp, " return(0x80000000);\n");
fprintf(fp, " }\n\n");
fprintf(fp, " pbPC = cpu.z80Base + cpu.z80pc;\n\n");
fprintf(fp, " while (sdwCyclesRemaining > 0)\n");
fprintf(fp, " {\n");
fprintf(fp, " bOpcode = *pbPC++;\n");
fprintf(fp, " switch (bOpcode)\n");
fprintf(fp, " {\n");
while (dwLoop < 0x100)
{
dwLoop2 = 0;
fprintf(fp, " case 0x%.2x:\n", dwLoop);
fprintf(fp, " {\n");
while (StandardOps[dwLoop2].bOpCode != dwLoop && StandardOps[dwLoop2].bOpCode != 0xffffffff)
dwLoop2++;
if (bTimingRegular[dwLoop])
{
fprintf(fp, " sdwCyclesRemaining -= %ld;\n", bTimingRegular[dwLoop]);
}
if (StandardOps[dwLoop2].Emitter)
{
StandardOps[dwLoop2].Emitter(dwLoop);
}
fprintf(fp, " break;\n");
fprintf(fp, " }\n");
++dwLoop;
}
fprintf(fp, " }\n");
fprintf(fp, " }\n\n");
fprintf(fp, " dwElapsedTicks += (dwOriginalCycles - sdwCyclesRemaining);\n\n");
fprintf(fp, " cpu.z80pc = (UINT32) pbPC - (UINT32) cpu.z80Base;\n");
fprintf(fp, " return(dwReturnCode); /* Indicate success */\n");
fprintf(fp, "}\n\n");
}
else
{
assert(0);
}
}
EmitCBInstructions()
{
UINT32 dwLoop = 0;
UINT32 dwLoop2 = 0;
bCurrentMode = TIMING_CB;
if (MZ80_ASSEMBLY_X86 == bWhat)
{
while (dwLoop < 0x100)
{
sprintf(procname, "CBInst%.2x", dwLoop);
dwLoop2 = 0;
while (CBOps[dwLoop2].bOpCode != dwLoop && CBOps[dwLoop2].bOpCode != 0xffffffff)
dwLoop2++;
assert(dwLoop2 < 0x100);
if (CBOps[dwLoop2].Emitter && CBOps[dwLoop2].bOpCode != 0xffffffff)
CBOps[dwLoop2].Emitter((UINT32) dwLoop);
dwLoop++;
}
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, "void CBHandler(void)\n");
fprintf(fp, "{\n");
fprintf(fp, " switch (*pbPC++)\n");
fprintf(fp, " {\n");
while (dwLoop < 0x100)
{
dwLoop2 = 0;
fprintf(fp, " case 0x%.2x:\n", dwLoop);
fprintf(fp, " {\n");
while (CBOps[dwLoop2].bOpCode != dwLoop && CBOps[dwLoop2].bOpCode != 0xffffffff)
dwLoop2++;
if (bTimingCB[dwLoop])
{
fprintf(fp, " sdwCyclesRemaining -= %ld;\n", bTimingCB[dwLoop]);
}
if (CBOps[dwLoop2].Emitter)
{
CBOps[dwLoop2].Emitter(dwLoop);
}
else
{
InvalidInstructionC(2);
}
fprintf(fp, " break;\n");
fprintf(fp, " }\n");
++dwLoop;
}
fprintf(fp, " }\n");
fprintf(fp, "}\n");
}
else
{
assert(0);
}
}
EmitEDInstructions()
{
UINT32 dwLoop = 0;
UINT32 dwLoop2 = 0;
bCurrentMode = TIMING_ED;
if (MZ80_ASSEMBLY_X86 == bWhat)
{
while (dwLoop < 0x100)
{
sprintf(procname, "EDInst%.2x", dwLoop);
dwLoop2 = 0;
while (EDOps[dwLoop2].bOpCode != dwLoop && EDOps[dwLoop2].bOpCode != 0xffffffff)
dwLoop2++;
assert(dwLoop2 < 0x100);
if (EDOps[dwLoop2].Emitter && EDOps[dwLoop2].bOpCode != 0xffffffff)
EDOps[dwLoop2].Emitter((UINT32) dwLoop);
dwLoop++;
}
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, "void EDHandler(void)\n");
fprintf(fp, "{\n");
fprintf(fp, " switch (*pbPC++)\n");
fprintf(fp, " {\n");
while (dwLoop < 0x100)
{
dwLoop2 = 0;
fprintf(fp, " case 0x%.2x:\n", dwLoop);
fprintf(fp, " {\n");
while (EDOps[dwLoop2].bOpCode != dwLoop && EDOps[dwLoop2].bOpCode != 0xffffffff)
dwLoop2++;
if (bTimingED[dwLoop])
{
fprintf(fp, " sdwCyclesRemaining -= %ld;\n", bTimingED[dwLoop]);
}
if (EDOps[dwLoop2].Emitter)
{
EDOps[dwLoop2].Emitter(dwLoop);
}
else
{
InvalidInstructionC(2);
}
fprintf(fp, " break;\n");
fprintf(fp, " }\n");
++dwLoop;
}
fprintf(fp, " }\n");
fprintf(fp, "}\n");
}
else
{
assert(0);
}
fprintf(fp, "\n");
}
EmitDDInstructions()
{
UINT32 dwLoop = 0;
UINT32 dwLoop2 = 0;
bCurrentMode = TIMING_DDFD;
if (MZ80_ASSEMBLY_X86 == bWhat)
{
while (dwLoop < 0x100)
{
sprintf(procname, "DDInst%.2x", dwLoop);
dwLoop2 = 0;
while (DDFDOps[dwLoop2].bOpCode != dwLoop && DDFDOps[dwLoop2].bOpCode != 0xffffffff)
dwLoop2++;
assert(dwLoop2 < 0x100);
if (DDFDOps[dwLoop2].Emitter && DDFDOps[dwLoop2].bOpCode != 0xffffffff)
DDFDOps[dwLoop2].Emitter((UINT32) dwLoop);
dwLoop++;
}
bCurrentMode = TIMING_XXCB;
dwLoop = 0;
while (dwLoop < 0x100)
{
sprintf(procname, "DDFDCBInst%.2x", dwLoop);
dwLoop2 = 0;
while (DDFDCBOps[dwLoop2].bOpCode != dwLoop && DDFDCBOps[dwLoop2].bOpCode != 0xffffffff)
dwLoop2++;
assert(dwLoop2 < 0x100);
if (DDFDCBOps[dwLoop2].Emitter && DDFDCBOps[dwLoop2].bOpCode != 0xffffffff)
DDFDCBOps[dwLoop2].Emitter((UINT32) dwLoop);
dwLoop++;
}
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, "void DDHandler(void)\n");
fprintf(fp, "{\n");
fprintf(fp, " switch (*pbPC++)\n");
fprintf(fp, " {\n");
while (dwLoop < 0x100)
{
dwLoop2 = 0;
fprintf(fp, " case 0x%.2x:\n", dwLoop);
fprintf(fp, " {\n");
while (DDFDOps[dwLoop2].bOpCode != dwLoop && DDFDOps[dwLoop2].bOpCode != 0xffffffff)
dwLoop2++;
if (bTimingDDFD[dwLoop])
{
fprintf(fp, " sdwCyclesRemaining -= %ld;\n", bTimingDDFD[dwLoop]);
}
if (DDFDOps[dwLoop2].Emitter)
{
DDFDOps[dwLoop2].Emitter(dwLoop);
}
else
{
InvalidInstructionC(2);
}
fprintf(fp, " break;\n");
fprintf(fp, " }\n");
++dwLoop;
}
fprintf(fp, " }\n");
fprintf(fp, "}\n");
// DDFD Handler
bCurrentMode = TIMING_XXCB;
dwLoop = 0;
fprintf(fp, "void DDFDCBHandler(UINT32 dwWhich)\n");
fprintf(fp, "{\n");
fprintf(fp, " if (dwWhich)\n");
fprintf(fp, " {\n");
fprintf(fp, " dwAddr = (UINT32) ((INT32) cpu.z80IY + ((INT32) *pbPC++)) & 0xffff;\n");
fprintf(fp, " }\n");
fprintf(fp, " else\n");
fprintf(fp, " {\n");
fprintf(fp, " dwAddr = (UINT32) ((INT32) cpu.z80IX + ((INT32) *pbPC++)) & 0xffff;\n");
fprintf(fp, " }\n\n");
ReadValueFromMemory("dwAddr", "bTemp");
fprintf(fp, " switch (*pbPC++)\n");
fprintf(fp, " {\n");
while (dwLoop < 0x100)
{
dwLoop2 = 0;
fprintf(fp, " case 0x%.2x:\n", dwLoop);
fprintf(fp, " {\n");
while (DDFDCBOps[dwLoop2].bOpCode != dwLoop && DDFDCBOps[dwLoop2].bOpCode != 0xffffffff)
dwLoop2++;
if (bTimingXXCB[dwLoop])
{
fprintf(fp, " sdwCyclesRemaining -= %ld;\n", bTimingXXCB[dwLoop]);
}
if (DDFDCBOps[dwLoop2].Emitter)
{
DDFDCBOps[dwLoop2].Emitter(dwLoop);
}
else
{
InvalidInstructionC(4);
}
fprintf(fp, " break;\n");
fprintf(fp, " }\n");
++dwLoop;
}
fprintf(fp, " }\n");
fprintf(fp, "}\n");
}
else
{
assert(0);
}
}
EmitFDInstructions()
{
UINT32 dwLoop = 0;
UINT32 dwLoop2 = 0;
bCurrentMode = TIMING_DDFD;
if (MZ80_ASSEMBLY_X86 == bWhat)
{
while (dwLoop < 0x100)
{
sprintf(procname, "FDInst%.2x", dwLoop);
dwLoop2 = 0;
while (DDFDOps[dwLoop2].bOpCode != dwLoop && DDFDOps[dwLoop2].bOpCode != 0xffffffff)
dwLoop2++;
assert(dwLoop2 < 0x100);
if (DDFDOps[dwLoop2].Emitter && DDFDOps[dwLoop2].bOpCode != 0xffffffff)
DDFDOps[dwLoop2].Emitter((UINT32) dwLoop);
dwLoop++;
}
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, "void FDHandler(void)\n");
fprintf(fp, "{\n");
fprintf(fp, " switch (*pbPC++)\n");
fprintf(fp, " {\n");
while (dwLoop < 0x100)
{
dwLoop2 = 0;
fprintf(fp, " case 0x%.2x:\n", dwLoop);
fprintf(fp, " {\n");
while (DDFDOps[dwLoop2].bOpCode != dwLoop && DDFDOps[dwLoop2].bOpCode != 0xffffffff)
dwLoop2++;
if (bTimingDDFD[dwLoop])
{
fprintf(fp, " sdwCyclesRemaining -= %ld;\n", bTimingDDFD[dwLoop]);
}
if (DDFDOps[dwLoop2].Emitter)
{
DDFDOps[dwLoop2].Emitter(dwLoop);
}
else
{
InvalidInstructionC(2);
}
fprintf(fp, " break;\n");
fprintf(fp, " }\n");
++dwLoop;
}
fprintf(fp, " }\n");
fprintf(fp, "}\n");
}
else
{
assert(0);
}
}
/* These are the meta routines */
void ReadMemoryByteHandler()
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
Alignment();
fprintf(fp, "; This is a generic read memory byte handler when a foreign\n");
fprintf(fp, "; handler is to be called\n\n");
fprintf(fp, "; EDI=Handler address, EDX=Address\n");
fprintf(fp, "; On return, EDX & EDI are undisturbed and AL=Byte read\n\n");
fprintf(fp, "ReadMemoryByte:\n");
fprintf(fp, " mov [_z80af], ax ; Save AF\n");
fprintf(fp, " cmp [edi+8], dword 0 ; Null handler?\n");
fprintf(fp, " je directReadHandler ; Yep! It's a direct read!\n\n");
fprintf(fp, " mov [_z80hl], bx ; Save HL\n");
fprintf(fp, " mov [_z80bc], cx ; Save BC\n");
fprintf(fp, " sub esi, ebp ; Our program counter\n", cpubasename);
fprintf(fp, " mov [_z80pc], si ; Save our program counter\n", cpubasename);
// Now adjust the proper timing
fprintf(fp, " mov esi, [dwOriginalExec] \n");
fprintf(fp, " sub esi, [cyclesRemaining]\n");
fprintf(fp, " add [dwElapsedTicks], esi\n");
fprintf(fp, " add [_z80rCounter], esi\n");
fprintf(fp, " sub [dwOriginalExec], esi\n");
fprintf(fp, " push edi ; Save our structure address\n");
fprintf(fp, " push edx ; And our desired address\n");
if (FALSE == bUseStack)
{
fprintf(fp, " mov eax, edx ; Get our desired address reg\n");
fprintf(fp, " mov edx, edi ; Pointer to the structure\n");
}
fprintf(fp, " call dword [edi + 8] ; Go call our handler\n");
fprintf(fp, " pop edx ; Restore our address\n");
fprintf(fp, " pop edi ; Restore our handler's address\n");
fprintf(fp, " xor ebx, ebx ; Zero our future HL\n");
fprintf(fp, " xor esi, esi ; Zero it!\n");
fprintf(fp, " mov ebp, [_z80Base] ; Base pointer comes back\n", cpubasename);
fprintf(fp, " mov si, [_z80pc] ; Get our program counter back\n", cpubasename);
fprintf(fp, " xor ecx, ecx ; Zero our future BC\n");
fprintf(fp, " add esi, ebp ; Rebase it properly\n");
fprintf(fp, " mov bx, [_z80hl] ; Get HL back\n");
fprintf(fp, " mov cx, [_z80bc] ; Get BC back\n");
// Note: the callee must restore AF!
fprintf(fp, " ret\n\n");
fprintf(fp, "directReadHandler:\n");
fprintf(fp, " mov eax, [edi+12] ; Get our base address\n");
fprintf(fp, " sub edx, [edi] ; Subtract our base (low) address\n");
fprintf(fp, " mov al, [edx+eax] ; Get our data byte\n");
fprintf(fp, " and eax, 0ffh ; Only the lower byte matters!\n");
fprintf(fp, " add edx, [edi] ; Add our base back\n");
fprintf(fp, " ret ; Return to caller!\n\n");
}
else
if (MZ80_C == bWhat)
{
}
else
{
assert(0);
}
}
void WriteMemoryByteHandler()
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
Alignment();
fprintf(fp, "; This is a generic read memory byte handler when a foreign\n");
fprintf(fp, "; handler is to be called.\n");
fprintf(fp, "; EDI=Handler address, AL=Byte to write, EDX=Address\n");
fprintf(fp, "; EDI and EDX Are undisturbed on exit\n\n");
fprintf(fp, "WriteMemoryByte:\n");
fprintf(fp, " cmp [edi+8], dword 0 ; Null handler?\n");
fprintf(fp, " je directWriteHandler\n\n");
fprintf(fp, " mov [_z80hl], bx ; Save HL\n");
fprintf(fp, " mov [_z80bc], cx ; Save BX\n");
fprintf(fp, " sub esi, ebp ; Our program counter\n", cpubasename);
fprintf(fp, " mov [_z80pc], si ; Save our program counter\n", cpubasename);
// Now adjust the proper timing
fprintf(fp, " mov esi, [dwOriginalExec] \n");
fprintf(fp, " sub esi, [cyclesRemaining]\n");
fprintf(fp, " add [dwElapsedTicks], esi\n");
fprintf(fp, " add [_z80rCounter], esi\n");
fprintf(fp, " sub [dwOriginalExec], esi\n");
fprintf(fp, " push edi ; Save our structure address\n");
if (bUseStack)
fprintf(fp, " push eax ; Data to write\n");
fprintf(fp, " push edx ; And our desired address\n");
if (FALSE == bUseStack)
{
fprintf(fp, " xchg eax, edx ; Swap address/data around\n");
fprintf(fp, " mov ebx, edi ; Our MemoryWriteByte structure address\n");
}
fprintf(fp, " call dword [edi + 8] ; Go call our handler\n");
fprintf(fp, " pop edx ; Restore our address\n");
if (bUseStack)
fprintf(fp, " pop eax ; Restore our data written\n");
fprintf(fp, " pop edi ; Save our structure address\n");
fprintf(fp, " xor ebx, ebx ; Zero our future HL\n");
fprintf(fp, " xor ecx, ecx ; Zero our future BC\n");
fprintf(fp, " mov bx, [_z80hl] ; Get HL back\n");
fprintf(fp, " mov cx, [_z80bc] ; Get BC back\n");
fprintf(fp, " mov ax, [_z80af] ; Get AF back\n");
fprintf(fp, " xor esi, esi ; Zero it!\n");
fprintf(fp, " mov si, [_z80pc] ; Get our program counter back\n", cpubasename);
fprintf(fp, " mov ebp, [_z80Base] ; Base pointer comes back\n", cpubasename);
fprintf(fp, " add esi, ebp ; Rebase it properly\n");
fprintf(fp, " ret\n\n");
fprintf(fp, "directWriteHandler:\n");
fprintf(fp, " sub edx, [edi] ; Subtract our offset\n");
fprintf(fp, " add edx, [edi+12] ; Add in the base address\n");
fprintf(fp, " mov [edx], al ; Store our byte\n");
fprintf(fp, " sub edx, [edi+12] ; Restore our base address\n");
fprintf(fp, " add edx, [edi] ; And put our offset back\n");
fprintf(fp, " ret\n\n");
}
else
if (MZ80_C == bWhat)
{
}
else
{
assert(0);
}
}
void PushWordHandler()
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
Alignment();
fprintf(fp, ";\n");
fprintf(fp, "; DX=Top of SP, [_wordval]=word value to push\n");
fprintf(fp, ";\n\n");
fprintf(fp, "PushWord:\n");
fprintf(fp, " mov dx, [_z80sp]\n");
fprintf(fp, " dec dx\n");
WriteValueToMemory("dx", "byte [_wordval+1]");
fprintf(fp, " dec dx\n");
WriteValueToMemory("dx", "byte [_wordval]");
fprintf(fp, " sub [_z80sp], word 2\n");
fprintf(fp, " xor edx, edx\n");
fprintf(fp, " ret\n\n");
}
}
void PopWordHandler()
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
Alignment();
fprintf(fp, ";\n");
fprintf(fp, "; [_z80sp]=Top of SP, DX=Word value read\n");
fprintf(fp, ";\n\n");
fprintf(fp, "PopWord:\n");
fprintf(fp, " mov dx, [_z80sp]\n");
ReadWordFromMemory("dx", "dx");
fprintf(fp, " ret\n\n");
}
}
void ReadIoHandler()
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
Alignment();
fprintf(fp, "; This is a generic I/O read byte handler for when a foreign\n");
fprintf(fp, "; handler is to be called\n");
fprintf(fp, "; EDI=Handler address, EDX=I/O Address\n");
fprintf(fp, "; On return, EDX & EDI are undisturbed and AL=Byte read\n\n");
fprintf(fp, "ReadIOByte:\n");
fprintf(fp, " mov [_z80af], ax ; Save AF\n");
fprintf(fp, " mov [_z80hl], bx ; Save HL\n");
fprintf(fp, " mov [_z80bc], cx ; Save BC\n");
fprintf(fp, " sub esi, ebp ; Our program counter\n", cpubasename);
fprintf(fp, " mov [_z80pc], si ; Save our program counter\n", cpubasename);
// Now adjust the proper timing
fprintf(fp, " mov esi, [dwOriginalExec] \n");
fprintf(fp, " sub esi, [cyclesRemaining]\n");
fprintf(fp, " add [dwElapsedTicks], esi\n");
fprintf(fp, " add [_z80rCounter], esi\n");
fprintf(fp, " sub [dwOriginalExec], esi\n");
fprintf(fp, " push edi ; Save our structure address\n");
fprintf(fp, " push edx ; And our desired I/O port\n");
if (FALSE == bUseStack)
{
fprintf(fp, " mov eax, edx ; Get our desired address reg\n");
fprintf(fp, " mov edx, edi ; Pointer to the structure\n");
}
fprintf(fp, " call dword [edi + 4] ; Go call our handler\n");
fprintf(fp, " pop edx ; Restore our address\n");
fprintf(fp, " pop edi ; Restore our handler's address\n");
fprintf(fp, " xor ebx, ebx ; Zero our future HL\n");
fprintf(fp, " xor ecx, ecx ; Zero our future BC\n");
fprintf(fp, " xor esi, esi ; Zero it!\n");
fprintf(fp, " mov si, [_z80pc] ; Get our program counter back\n", cpubasename);
fprintf(fp, " mov ebp, [_z80Base] ; Base pointer comes back\n", cpubasename);
fprintf(fp, " add esi, ebp ; Rebase it properly\n");
fprintf(fp, " mov bx, [_z80hl] ; Get HL back\n");
fprintf(fp, " mov cx, [_z80bc] ; Get BC back\n");
// Note: the callee must restore AF!
fprintf(fp, " ret\n\n");
}
else
if (MZ80_C == bWhat)
{
}
else
{
assert(0);
}
}
void WriteIoHandler()
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
Alignment();
fprintf(fp, "; This is a generic write I/O byte handler when a foreign handler is to\n");
fprintf(fp, "; be called\n");
fprintf(fp, "; EDI=Handler address, AL=Byte to write, EDX=I/O Address\n");
fprintf(fp, "; EDI and EDX Are undisturbed on exit\n\n");
fprintf(fp, "WriteIOByte:\n");
fprintf(fp, " mov [_z80hl], bx ; Save HL\n");
fprintf(fp, " mov [_z80bc], cx ; Save BX\n");
fprintf(fp, " sub esi, ebp ; Our program counter\n", cpubasename);
fprintf(fp, " mov [_z80pc], si ; Save our program counter\n", cpubasename);
// Now adjust the proper timing
fprintf(fp, " mov esi, [dwOriginalExec] \n");
fprintf(fp, " sub esi, [cyclesRemaining]\n");
fprintf(fp, " add [dwElapsedTicks], esi\n");
fprintf(fp, " add [_z80rCounter], esi\n");
fprintf(fp, " sub [dwOriginalExec], esi\n");
fprintf(fp, " push edi ; Save our structure address\n");
if (bUseStack)
fprintf(fp, " push eax ; Data to write\n");
fprintf(fp, " push edx ; And our desired I/O address\n");
if (FALSE == bUseStack)
{
fprintf(fp, " xchg eax, edx ; Swap address/data around\n");
fprintf(fp, " mov ebx, edi ; Our z80IoWrite structure address\n");
}
fprintf(fp, " call dword [edi + 4] ; Go call our handler\n");
fprintf(fp, " pop edx ; Restore our address\n");
if (bUseStack)
fprintf(fp, " pop eax ; Restore our data written\n");
fprintf(fp, " pop edi ; Save our structure address\n");
fprintf(fp, " xor ebx, ebx ; Zero our future HL\n");
fprintf(fp, " xor ecx, ecx ; Zero our future BC\n");
fprintf(fp, " mov bx, [_z80hl] ; Get HL back\n");
fprintf(fp, " mov cx, [_z80bc] ; Get BC back\n");
fprintf(fp, " mov ax, [_z80af] ; Get AF back\n");
fprintf(fp, " xor esi, esi ; Zero it!\n");
fprintf(fp, " mov si, [_z80pc] ; Get our program counter back\n", cpubasename);
fprintf(fp, " mov ebp, [_z80Base] ; Base pointer comes back\n", cpubasename);
fprintf(fp, " add esi, ebp ; Rebase it properly\n");
fprintf(fp, " ret\n\n");
}
else
if (MZ80_C == bWhat)
{
}
else
{
assert(0);
}
}
ExecCode()
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
fprintf(fp, " global _%sexec\n", cpubasename);
fprintf(fp, " global %sexec_\n", cpubasename);
if (bPlain)
fprintf(fp, " global %sexec\n", cpubasename);
sprintf(procname, "%sexec_", cpubasename);
ProcBegin(0xffffffff);
fprintf(fp, "_%sexec:\n", cpubasename);
if (bPlain)
fprintf(fp, "%sexec:\n", cpubasename);
if (bUseStack)
fprintf(fp, " mov eax, [esp+4] ; Get our execution cycle count\n");
fprintf(fp, " push ebx ; Save all registers we use\n");
fprintf(fp, " push ecx\n");
fprintf(fp, " push edx\n");
fprintf(fp, " push ebp\n");
fprintf(fp, " push esi\n");
fprintf(fp, " push edi\n");
fprintf(fp, "\n");
fprintf(fp, " mov edi, eax\n");
fprintf(fp, " mov dword [cyclesRemaining], eax ; Store # of instructions to\n");
fprintf(fp, " mov [dwLastRSample], eax\n");
fprintf(fp, " mov [dwOriginalExec], eax ; Store this!\n");
fprintf(fp, " cmp dword [_z80halted], 0\n");
fprintf(fp, " je goCpu\n");
fprintf(fp, " add [_z80rCounter], eax\n");
if (FALSE == bNoTiming)
{
fprintf(fp, " add dword [dwElapsedTicks], eax\n");
}
fprintf(fp, " mov dword [cyclesRemaining], 0 ; Nothing left!\n");
fprintf(fp, " mov eax, 80000000h ; Successful exection\n");
fprintf(fp, " jmp popReg\n");
fprintf(fp, "goCpu:\n");
fprintf(fp, " cld ; Go forward!\n");
fprintf(fp, "\n");
fprintf(fp, " xor eax, eax ; Zero EAX 'cause we use it!\n");
fprintf(fp, " xor ebx, ebx ; Zero EBX, too\n");
fprintf(fp, " xor ecx, ecx ; Zero ECX\n");
fprintf(fp, " xor edx, edx ; And EDX\n");
fprintf(fp, " xor esi, esi ; Zero our source address\n");
fprintf(fp, "\n");
fprintf(fp, " mov ax, [_z80af] ; Accumulator & flags\n");
fprintf(fp, " xchg ah, al ; Swap these for later\n");
fprintf(fp, " mov bx, [_z80hl] ; Get our HL value\n");
fprintf(fp, " mov cx, [_z80bc] ; And our BC value\n");
fprintf(fp, " mov ebp, [_z80Base] ; Get the base address\n");
fprintf(fp, " mov si, [_z80pc] ; Get our program counter\n");
fprintf(fp, " add esi, ebp ; Add in our base address\n");
fprintf(fp, " cmp [_z80intPending], byte 0 ; Interrupt pending?\n");
fprintf(fp, " jz masterExecTarget\n\n");
fprintf(fp, " call causeInternalInterrupt\n\n");
fprintf(fp, "masterExecTarget:\n");
fprintf(fp, " mov dl, [esi]\n");
fprintf(fp, " inc esi\n");
fprintf(fp, " jmp dword [z80regular+edx*4]\n\n");
fprintf(fp, "; We get to invalidInsWord if it's a double byte invalid opcode\n");
fprintf(fp, "\n");
fprintf(fp, "invalidInsWord:\n");
fprintf(fp, " dec esi\n");
fprintf(fp, "\n");
fprintf(fp, "; We get to invalidInsByte if it's a single byte invalid opcode\n");
fprintf(fp, "\n");
fprintf(fp, "invalidInsByte:\n");
fprintf(fp, " xchg ah, al ; Swap them back so they look good\n");
fprintf(fp, " mov [_z80af], ax ; Store A & flags\n");
fprintf(fp, " dec esi ; Back up one instruction...\n");
fprintf(fp, " mov edx, esi ; Get our address in EAX\n");
fprintf(fp, " sub edx, ebp ; And subtract our base for\n");
fprintf(fp, " ; an invalid instruction\n");
fprintf(fp, " jmp short emulateEnd\n");
fprintf(fp, "\n");
fprintf(fp, "noMoreExec:\n");
fprintf(fp, " cmp [bEIExit], byte 0 ; Are we exiting because of an EI?\n");
fprintf(fp, " jne checkEI\n");
fprintf(fp, "noMoreExecNoEI:\n");
fprintf(fp, " xchg ah, al ; Swap these for later\n");
fprintf(fp, " mov [_z80af], ax ; Store A & flags\n");
fprintf(fp, " mov edx, [dwOriginalExec] ; Original exec time\n");
fprintf(fp, " sub edx, edi ; Subtract # of cycles remaining\n");
fprintf(fp, " add [_z80rCounter], edx\n");
fprintf(fp, " add [dwElapsedTicks], edx ; Add our executed time\n");
fprintf(fp, " mov edx, 80000000h ; Indicate successful exec\n");
fprintf(fp, " jmp short emulateEnd ; All finished!\n");
fprintf(fp, "\n");
fprintf(fp, "; Now let's tuck away the virtual registers for next time\n");
fprintf(fp, "\n");
fprintf(fp, "storeFlags:\n");
fprintf(fp, " xchg ah, al ; Swap these for later\n");
fprintf(fp, " mov [_z80af], ax ; Store A & flags\n");
fprintf(fp, "emulateEnd:\n");
fprintf(fp, " mov [_z80hl], bx ; Store HL\n");
fprintf(fp, " mov [_z80bc], cx ; Store BC\n");
fprintf(fp, " sub esi, [_z80Base] ; Knock off physical address\n");
fprintf(fp, " mov [_z80pc], si ; And store virtual address\n");
fprintf(fp, " mov eax, edx ; Result code return\n");
fprintf(fp, "\n");
fprintf(fp, "popReg:\n");
fprintf(fp, " pop edi ; Restore registers\n");
fprintf(fp, " pop esi\n");
fprintf(fp, " pop ebp\n");
fprintf(fp, " pop edx\n");
fprintf(fp, " pop ecx\n");
fprintf(fp, " pop ebx\n");
fprintf(fp, "\n");
fprintf(fp, " ret\n");
fprintf(fp, "\n");
Alignment();
fprintf(fp, "checkEI:\n");
fprintf(fp, " xor edx, edx\n");
fprintf(fp, " mov [bEIExit], byte 0\n");
fprintf(fp, " sub edx, edi ; Find out how much time has passed\n");
fprintf(fp, " mov edi, [dwEITiming]\n");
fprintf(fp, " sub edi, edx\n");
fprintf(fp, " js noMoreExecNoEI\n");
fprintf(fp, " xor edx, edx\n");
fprintf(fp, " cmp [_z80intPending], byte 0\n");
fprintf(fp, " je near masterExecTarget\n");
fprintf(fp, " call causeInternalInterrupt\n");
fprintf(fp, " jmp masterExecTarget\n\n");
Alignment();
fprintf(fp, "causeInternalInterrupt:\n");
fprintf(fp, " mov dword [_z80halted], 0 ; We're not halted anymore!\n");
fprintf(fp, " test [_z80iff], byte IFF1 ; Interrupt enabled yet?\n");
fprintf(fp, " jz near internalInterruptsDisabled\n");
fprintf(fp, "\n; Interrupts enabled. Clear IFF1 and IFF2\n\n");
fprintf(fp, " mov [_z80intPending], byte 0\n");
fprintf(fp, "\n; Save off our active register sets\n\n");
fprintf(fp, " xchg ah, al ; Swap these for later\n");
fprintf(fp, " mov [_z80af], ax ; Store A & flags\n");
fprintf(fp, " mov [_z80hl], bx ; Store HL\n");
fprintf(fp, " mov [_z80bc], cx ; Store BC\n");
fprintf(fp, " sub esi, ebp ; Knock off physical address\n");
fprintf(fp, " mov [_z80pc], si ; And store virtual address\n");
fprintf(fp, " xor eax, eax\n");
fprintf(fp, " mov al, [_intData]\n\n");
fprintf(fp, "\n");
fprintf(fp, " push edi\n");
fprintf(fp, "\n");
if (bThroughCallHandler)
{
fprintf(fp, " pushad\n" );
fprintf(fp, " xor edx, edx\n" );
fprintf(fp, " mov ax, [_z80pc]\n");
fprintf(fp, " mov [_wordval], ax\n");
fprintf(fp, " push ecx\n");
fprintf(fp, " push ebx\n");
fprintf(fp, " push esi\n");
fprintf(fp, " mov ax, [_z80af]\n"); // Get AF
fprintf(fp, " mov bx, [_z80hl]\n"); // Get HL
fprintf(fp, " mov cx, [_z80bc]\n"); // Get BC
fprintf(fp, " call PushWord\n");
fprintf(fp, " pop esi\n");
fprintf(fp, " pop ebx\n");
fprintf(fp, " pop ecx\n");
fprintf(fp, " popad\n" );
}
else
{
fprintf(fp, " mov dx, [_z80pc]\n");
fprintf(fp, " xor edi, edi\n");
fprintf(fp, " mov di, word [_z80sp]\n");
fprintf(fp, " sub di, 2\n");
fprintf(fp, " mov word [_z80sp], di\n");
fprintf(fp, " mov [ebp+edi], dx\n");
}
fprintf(fp, " cmp dword [_z80interruptMode], 2 ; Are we lower than mode 2?\n");
fprintf(fp, " jb internalJustModeTwo\n");
fprintf(fp, " mov ah, [_z80i] ; Get our high address here\n");
fprintf(fp, " and eax, 0ffffh ; Only the lower part\n");
fprintf(fp, " mov ax, [eax+ebp] ; Get our vector\n");
fprintf(fp, " jmp short internalSetNewVector ; Go set it!\n");
fprintf(fp, "internalJustModeTwo:\n");
fprintf(fp, " mov ax, word [_z80intAddr]\n");
fprintf(fp, "internalSetNewVector:\n");
fprintf(fp, " mov [_z80pc], ax\n");
fprintf(fp, "\n");
fprintf(fp, " pop edi\n");
fprintf(fp, "\n");
fprintf(fp, " xor eax, eax ; Zero this so we can use it as an index\n");
fprintf(fp, " mov al, [_z80interruptMode]\n");
fprintf(fp, " mov al, [intModeTStates+eax]\n");
fprintf(fp, " sub edi, eax\n");
fprintf(fp, " add [_z80rCounter], eax\n");
fprintf(fp, "\n; Restore all the registers and whatnot\n\n");
fprintf(fp, " mov ax, [_z80af] ; Accumulator & flags\n");
fprintf(fp, " xchg ah, al ; Swap these for later\n");
fprintf(fp, " mov bx, [_z80hl] ; Get our HL value\n");
fprintf(fp, " mov cx, [_z80bc] ; And our BC value\n");
fprintf(fp, " mov ebp, [_z80Base] ; Get the base address\n");
fprintf(fp, " mov si, [_z80pc] ; Get our program counter\n");
fprintf(fp, " add esi, ebp ; Add in our base address\n");
fprintf(fp, "internalInterruptsDisabled:\n");
fprintf(fp, " xor edx, edx\n");
fprintf(fp, " ret\n");
}
else
if (MZ80_C == bWhat)
{
}
else
{
assert(0);
}
}
NmiCode()
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
fprintf(fp, " global _%snmi\n", cpubasename);
fprintf(fp, " global %snmi_\n", cpubasename);
if (bPlain)
fprintf(fp, " global %snmi\n", cpubasename);
sprintf(procname, "%snmi_", cpubasename);
ProcBegin(0xffffffff);
fprintf(fp, "_%snmi:\n", cpubasename);
if (bPlain)
fprintf(fp, "%snmi:\n", cpubasename);
fprintf(fp, " mov dword [_z80halted], 0 ; We're not halted anymore!\n");
fprintf(fp, " mov al, [_z80iff] ; Get our IFF setting\n");
fprintf(fp, " and al, IFF1 ; Just IFF 1\n");
fprintf(fp, " shl al, 1 ; Makes IFF1->IFF2 and zeros IFF1\n");
fprintf(fp, " mov [_z80iff], al ; Store it back to the interrupt state!\n");
fprintf(fp, "\n");
fprintf(fp, " push ebp\n");
fprintf(fp, " push edi\n");
fprintf(fp, " mov ebp, [_z80Base]\n");
fprintf(fp, "\n");
fprintf(fp, " xor eax, eax\n");
fprintf(fp, " mov ax, [_z80pc]\n");
if (bThroughCallHandler)
{
fprintf(fp, " push esi\n");
fprintf(fp, " push ebx\n");
fprintf(fp, " push ecx\n");
fprintf(fp, " mov [_wordval], ax\n");
fprintf(fp, " mov esi, ebp\n");
fprintf(fp, " add esi, eax\n");
fprintf(fp, " mov ax, [_z80af]\n"); // Get AF
fprintf(fp, " mov bx, [_z80hl]\n"); // Get HL
fprintf(fp, " mov cx, [_z80bc]\n"); // Get BC
fprintf(fp, " push ebx\n");
fprintf(fp, " push ecx\n");
fprintf(fp, " push edx\n");
fprintf(fp, " push esi\n");
fprintf(fp, " push eax\n");
fprintf(fp, " call PushWord\n");
fprintf(fp, " pop eax\n");
fprintf(fp, " pop esi\n");
fprintf(fp, " pop edx\n");
fprintf(fp, " pop ecx\n");
fprintf(fp, " pop ebx\n");
fprintf(fp, " pop ecx\n");
fprintf(fp, " pop ebx\n");
fprintf(fp, " pop esi\n");
}
else
{
fprintf(fp, " xor edi, edi\n");
fprintf(fp, " mov di, word [_z80sp]\n");
fprintf(fp, " sub di, 2\n");
fprintf(fp, " mov word [_z80sp], di\n");
fprintf(fp, " mov [ebp+edi], ax\n");
}
fprintf(fp, " mov ax, [_z80nmiAddr]\n");
fprintf(fp, " mov [_z80pc], ax\n");
fprintf(fp, "\n");
fprintf(fp, " add [dwElapsedTicks], dword 11 ; 11 T-States for NMI\n");
fprintf(fp, " add [_z80rCounter], dword 11\n");
fprintf(fp, " pop edi\n");
fprintf(fp, " pop ebp\n");
fprintf(fp, "\n");
fprintf(fp, " xor eax, eax ; Indicate we took the interrupt\n");
fprintf(fp, " ret\n");
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, "/* NMI Handler */\n\n");
fprintf(fp, "UINT32 %snmi(void)\n", cpubasename);
fprintf(fp, "{\n");
fprintf(fp, " cpu.z80halted = 0;\n");
fprintf(fp, " pbSP = (cpu.z80Base + cpu.z80sp - 1); /* Normalize the stack pointer */\n");
fprintf(fp, " *pbSP-- = cpu.z80pc >> 8; /* LSB */\n");
fprintf(fp, " *pbSP = (UINT8) cpu.z80pc; /* MSB */\n");
fprintf(fp, " cpu.z80sp -= 2; /* Back our stack up */\n");
fprintf(fp, " cpu.z80pc = cpu.z80nmiAddr; /* Our NMI */\n");
fprintf(fp, " return(0);\n");
fprintf(fp, "}\n\n");
}
else
{
assert(0);
}
}
IntCode()
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
fprintf(fp, " global _%sint\n", cpubasename);
fprintf(fp, " global %sint_\n", cpubasename);
if (bPlain)
fprintf(fp, " global %sint\n", cpubasename);
sprintf(procname, "%sint_", cpubasename);
ProcBegin(0xffffffff);
fprintf(fp, "_%sint:\n", cpubasename);
if (bPlain)
fprintf(fp, "%sint:\n", cpubasename);
if (bUseStack)
fprintf(fp, " mov eax, [esp+4] ; Get our (potential) lower interrupt address\n");
fprintf(fp, " mov dword [_z80halted], 0 ; We're not halted anymore!\n");
fprintf(fp, " mov ah, IFF1 ; Is IFF1 enabled?\n");
fprintf(fp, " and ah, [_z80iff] ; Well, is it?\n");
fprintf(fp, " jz near interruptsDisabled\n");
fprintf(fp, "\n; Interrupts enabled. Clear IFF1 and IFF2\n\n");
fprintf(fp, " and dword [_z80iff], ~(IFF1 | IFF2);\n\n");
fprintf(fp, " mov [_z80intPending], byte 0\n");
fprintf(fp, "\n");
fprintf(fp, " push ebp\n");
fprintf(fp, " push edi\n");
fprintf(fp, " push edx\n");
fprintf(fp, " mov ebp, [_z80Base]\n");
fprintf(fp, "\n");
if (bThroughCallHandler)
{
fprintf(fp, " pushad\n" );
fprintf(fp, " xor edx, edx\n" );
fprintf(fp, " mov ax, [_z80pc]\n");
fprintf(fp, " mov [_wordval], ax\n");
fprintf(fp, " push ecx\n");
fprintf(fp, " push ebx\n");
fprintf(fp, " push esi\n");
fprintf(fp, " mov ax, [_z80af]\n"); // Get AF
fprintf(fp, " mov bx, [_z80hl]\n"); // Get HL
fprintf(fp, " mov cx, [_z80bc]\n"); // Get BC
fprintf(fp, " call PushWord\n");
fprintf(fp, " pop esi\n");
fprintf(fp, " pop ebx\n");
fprintf(fp, " pop ecx\n");
fprintf(fp, " popad\n" );
}
else
{
fprintf(fp, " mov dx, [_z80pc]\n");
fprintf(fp, " xor edi, edi\n");
fprintf(fp, " mov di, word [_z80sp]\n");
fprintf(fp, " sub di, 2\n");
fprintf(fp, " mov word [_z80sp], di\n");
fprintf(fp, " mov [ebp+edi], dx\n");
}
fprintf(fp, " cmp dword [_z80interruptMode], 2 ; Are we lower than mode 2?\n");
fprintf(fp, " jb justModeTwo\n");
fprintf(fp, " mov ah, [_z80i] ; Get our high address here\n");
fprintf(fp, " and eax, 0ffffh ; Only the lower part\n");
fprintf(fp, " mov ax, [eax+ebp] ; Get our vector\n");
fprintf(fp, " jmp short setNewVector ; Go set it!\n");
fprintf(fp, "justModeTwo:\n");
fprintf(fp, " mov ax, word [_z80intAddr]\n");
fprintf(fp, "setNewVector:\n");
fprintf(fp, " mov [_z80pc], ax\n");
fprintf(fp, "\n");
fprintf(fp, " pop edx\n");
fprintf(fp, " pop edi\n");
fprintf(fp, " pop ebp\n");
fprintf(fp, "\n");
fprintf(fp, " xor eax, eax ; Zero this so we can use it as an index\n");
fprintf(fp, " mov al, [_z80interruptMode]\n");
fprintf(fp, " mov al, [intModeTStates+eax]\n");
fprintf(fp, " add [dwElapsedTicks], eax\n");
fprintf(fp, " add [_z80rCounter], eax\n");
fprintf(fp, " xor eax, eax ; Indicate we took the interrupt\n");
fprintf(fp, " jmp short z80intExit\n");
fprintf(fp, "\n");
fprintf(fp, "interruptsDisabled:\n");
fprintf(fp, " mov [_z80intPending], byte 1\n");
fprintf(fp, " mov [_intData], al ; Save this info for later\n");
fprintf(fp, " mov eax, 0ffffffffh ; Indicate we didn't take it\n");
fprintf(fp, "\n");
fprintf(fp, "z80intExit:\n");
fprintf(fp, " ret\n\n");
fprintf(fp, " global _%sClearPendingInterrupt\n", cpubasename);
fprintf(fp, " global %sClearPendingInterrupt_\n", cpubasename);
if (bPlain)
fprintf(fp, " global %sClearPendingInterrupt\n", cpubasename);
sprintf(procname, "%sClearPendingInterrupt_", cpubasename);
ProcBegin(0xffffffff);
fprintf(fp, "_%sClearPendingInterrupt:\n", cpubasename);
if (bPlain)
fprintf(fp, "%sClearPendingInterrupt:\n", cpubasename);
fprintf(fp, " mov [_z80intPending], byte 0\n");
fprintf(fp, " ret\n\n");
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, "/* Interrupt handler */\n\n");
fprintf(fp, "UINT32 %sint(UINT32 dwLowAddr)\n", cpubasename);
fprintf(fp, "{\n");
fprintf(fp, " cpu.z80halted = 0;\n");
fprintf(fp, " if (0 == (cpu.z80iff & IFF1))\n");
fprintf(fp, " return(0xffffffff);\n");
fprintf(fp, " cpu.z80iff &= ~(IFF1 | IFF2);\n");
fprintf(fp, " pbSP = (cpu.z80Base + cpu.z80sp - 1); /* Normalize the stack pointer */\n");
fprintf(fp, " *pbSP-- = cpu.z80pc >> 8; /* LSB */\n");
fprintf(fp, " *pbSP = (UINT8) cpu.z80pc; /* MSB */\n");
fprintf(fp, " cpu.z80sp -= 2; /* Back our stack up */\n");
fprintf(fp, " if (2 == cpu.z80interruptMode)\n");
fprintf(fp, " {\n");
fprintf(fp, " cpu.z80pc = ((UINT16) cpu.z80i << 8) | (dwLowAddr & 0xff);\n");
fprintf(fp, " cpu.z80pc = ((UINT16) cpu.z80Base[cpu.z80pc + 1] << 8) | (cpu.z80Base[cpu.z80pc]);\n");
fprintf(fp, " }\n");
fprintf(fp, " else\n");
fprintf(fp, " {\n");
fprintf(fp, " cpu.z80pc = cpu.z80intAddr;\n");
fprintf(fp, " }\n");
fprintf(fp, " pbPC = cpu.z80Base + cpu.z80pc; /* Normalize the address */\n");
fprintf(fp, " return(0);\n");
fprintf(fp, "}\n\n");
}
else
{
assert(0);
}
}
ResetCode()
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
fprintf(fp, " global _%sreset\n", cpubasename);
fprintf(fp, " global %sreset_\n", cpubasename);
if (bPlain)
fprintf(fp, " global %sreset\n", cpubasename);
sprintf(procname, "%sreset_", cpubasename);
ProcBegin(0xffffffff);
fprintf(fp, "_%sreset:\n", cpubasename);
if (bPlain)
fprintf(fp, "%sreset:\n", cpubasename);
fprintf(fp, " xor eax, eax ; Zero AX\n");
fprintf(fp, "\n");
fprintf(fp, " mov dword [_z80halted], eax ; We're not halted anymore!\n");
fprintf(fp, " mov word [_z80af], 0040h ; Zero A & flags - zero flag set\n");
fprintf(fp, " mov word [_z80bc], ax ; Zero BC\n");
fprintf(fp, " mov word [_z80de], ax ; Zero DE\n");
fprintf(fp, " mov word [_z80hl], ax ; Zero HL\n");
fprintf(fp, " mov word [_z80afprime], ax ; Zero AF Prime\n");
fprintf(fp, " mov word [_z80bcprime], ax ; Zero BC prime\n");
fprintf(fp, " mov word [_z80deprime], ax ; Zero DE prime\n");
fprintf(fp, " mov word [_z80hlprime], ax ; Zero HL prime\n");
fprintf(fp, " mov byte [_z80i], al ; Zero Interrupt register\n");
fprintf(fp, " mov byte [_z80r], al ; Zero refresh register\n");
fprintf(fp, " mov word [_z80ix], 0ffffh ; Default mz80Index register\n");
fprintf(fp, " mov word [_z80iy], 0ffffh ; Default mz80Index register\n");
fprintf(fp, " mov word [_z80pc], ax ; Zero program counter\n");
fprintf(fp, " mov word [_z80sp], ax ; And the stack pointer\n");
fprintf(fp, " mov dword [_z80iff], eax ; IFF1/IFF2 disabled!\n");
fprintf(fp, " mov dword [_z80interruptMode], eax ; Clear our interrupt mode (0)\n");
fprintf(fp, " mov word [_z80intAddr], 38h ; Set default interrupt address\n");
fprintf(fp, " mov word [_z80nmiAddr], 66h ; Set default nmi addr\n");
fprintf(fp, "\n");
fprintf(fp, " ret\n");
fprintf(fp, "\n");
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, "/* This routine is mz80's reset handler */\n\n");
fprintf(fp, "void %sreset(void)\n", cpubasename);
fprintf(fp, "{\n");
fprintf(fp, " cpu.z80halted = 0;\n");
fprintf(fp, " cpu.z80AF = 0;\n");
fprintf(fp, " cpu.z80F = Z80_FLAG_ZERO;\n");
fprintf(fp, " cpu.z80BC = 0;\n");
fprintf(fp, " cpu.z80DE = 0;\n");
fprintf(fp, " cpu.z80HL = 0;\n");
fprintf(fp, " cpu.z80afprime = 0;\n");
fprintf(fp, " cpu.z80bcprime = 0;\n");
fprintf(fp, " cpu.z80deprime = 0;\n");
fprintf(fp, " cpu.z80hlprime = 0;\n");
fprintf(fp, " cpu.z80i = 0;\n");
fprintf(fp, " cpu.z80r = 0;\n");
fprintf(fp, " cpu.z80IX = 0xffff; /* Yes, this is intentional */\n");
fprintf(fp, " cpu.z80IY = 0xffff; /* Yes, this is intentional */\n");
fprintf(fp, " cpu.z80pc = 0;\n");
fprintf(fp, " cpu.z80sp = 0;\n");
fprintf(fp, " cpu.z80interruptMode = 0;\n");
fprintf(fp, " cpu.z80intAddr = 0x38;\n");
fprintf(fp, " cpu.z80nmiAddr = 0x66;\n");
fprintf(fp, "}\n\n");
}
else
{
assert(0);
}
}
SetContextCode()
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
fprintf(fp, " global _%sSetContext\n", cpubasename);
fprintf(fp, " global %sSetContext_\n", cpubasename);
if (bPlain)
fprintf(fp, " global %sSetContext\n", cpubasename);
sprintf(procname, "%sSetContext_", cpubasename);
ProcBegin(0xffffffff);
fprintf(fp, "_%sSetContext:\n", cpubasename);
if (bPlain)
fprintf(fp, "%sSetContext:\n", cpubasename);
if (bUseStack)
fprintf(fp, " mov eax, [esp+4] ; Get our context address\n");
fprintf(fp, " push esi ; Save registers we use\n");
fprintf(fp, " push edi\n");
fprintf(fp, " push ecx\n");
fprintf(fp, " push es\n");
fprintf(fp, " mov di, ds\n");
fprintf(fp, " mov es, di\n");
fprintf(fp, " mov edi, _%scontextBegin\n", cpubasename);
fprintf(fp, " mov esi, eax ; Source address in ESI\n");
fprintf(fp, " mov ecx, (_%scontextEnd - _%scontextBegin) >> 2\n", cpubasename, cpubasename);
fprintf(fp, " rep movsd\n");
fprintf(fp, " mov ecx, (_%scontextEnd - _%scontextBegin) & 0x03\n", cpubasename, cpubasename);
fprintf(fp, " rep movsb\n");
fprintf(fp, " pop es\n");
fprintf(fp, " pop ecx\n");
fprintf(fp, " pop edi\n");
fprintf(fp, " pop esi\n");
fprintf(fp, " ret ; No return code\n");
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, "/* Set mz80's context */\n\n");
fprintf(fp, "void %sSetContext(void *pData)\n", cpubasename);
fprintf(fp, "{\n");
fprintf(fp, " memcpy(&cpu, pData, sizeof(CONTEXTMZ80));\n");
fprintf(fp, "}\n\n");
}
else
{
assert(0);
}
}
GetContextCode()
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
fprintf(fp, " global _%sGetContext\n", cpubasename);
fprintf(fp, " global %sGetContext_\n", cpubasename);
if (bPlain)
fprintf(fp, " global %sGetContext\n", cpubasename);
sprintf(procname, "%sGetContext_", cpubasename);
ProcBegin(0xffffffff);
fprintf(fp, "_%sGetContext:\n", cpubasename);
if (bPlain)
fprintf(fp, "%sGetContext:\n", cpubasename);
if (bUseStack)
fprintf(fp, " mov eax, [esp+4] ; Get our context address\n");
fprintf(fp, " push esi ; Save registers we use\n");
fprintf(fp, " push edi\n");
fprintf(fp, " push ecx\n");
fprintf(fp, " push es\n");
fprintf(fp, " mov di, ds\n");
fprintf(fp, " mov es, di\n");
fprintf(fp, " mov esi, _%scontextBegin\n", cpubasename);
fprintf(fp, " mov edi, eax ; Source address in ESI\n");
fprintf(fp, " mov ecx, (_%scontextEnd - _%scontextBegin) >> 2\n", cpubasename, cpubasename);
fprintf(fp, " rep movsd\n");
fprintf(fp, " mov ecx, (_%scontextEnd - _%scontextBegin) & 0x03\n", cpubasename, cpubasename);
fprintf(fp, " rep movsb\n");
fprintf(fp, " pop es\n");
fprintf(fp, " pop ecx\n");
fprintf(fp, " pop edi\n");
fprintf(fp, " pop esi\n");
fprintf(fp, " ret ; No return code\n");
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, "/* Get mz80's context */\n\n");
fprintf(fp, "void %sGetContext(void *pData)\n", cpubasename);
fprintf(fp, "{\n");
fprintf(fp, " memcpy(pData, &cpu, sizeof(CONTEXTMZ80));\n");
fprintf(fp, "}\n\n");
}
else
{
assert(0);
}
}
GetContextSizeCode()
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
fprintf(fp, " global _%sGetContextSize\n", cpubasename);
fprintf(fp, " global %sGetContextSize_\n", cpubasename);
if (bPlain)
fprintf(fp, " global %sGetContextSize\n", cpubasename);
sprintf(procname, "%sGetContextSize_", cpubasename);
ProcBegin(0xffffffff);
fprintf(fp, "_%sGetContextSize:\n", cpubasename);
if (bPlain)
fprintf(fp, "%sGetContextSize:\n", cpubasename);
fprintf(fp, " mov eax, _%scontextEnd - _%scontextBegin\n", cpubasename, cpubasename);
fprintf(fp, " ret\n\n");
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, "/* Get mz80's context size */\n\n");
fprintf(fp, "UINT32 %sGetContextSize(void)\n", cpubasename);
fprintf(fp, "{\n");
fprintf(fp, " return(sizeof(CONTEXTMZ80));\n");
fprintf(fp, "}\n\n");
}
else
{
assert(0);
}
}
void InitCode(void)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
fprintf(fp, " global _%sinit\n", cpubasename);
fprintf(fp, " global %sinit_\n", cpubasename);
if (bPlain)
fprintf(fp, " global %sinit\n", cpubasename);
sprintf(procname, "%sinit_", cpubasename);
ProcBegin(0xffffffff);
fprintf(fp, "_%sinit:\n", cpubasename);
if (bPlain)
fprintf(fp, "%sinit:\n", cpubasename);
fprintf(fp, " ret\n\n");
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, "/* Initialize MZ80 for action */\n\n");
fprintf(fp, "void %sinit(void)\n", cpubasename);
fprintf(fp, "{\n");
fprintf(fp, " UINT32 dwLoop;\n");
fprintf(fp, " UINT8 *pbTempPtr;\n");
fprintf(fp, " UINT8 *pbTempPtr2;\n");
fprintf(fp, " UINT8 bNewAdd;\n");
fprintf(fp, " UINT8 bNewSub;\n");
fprintf(fp, " UINT8 bFlag;\n");
fprintf(fp, " UINT8 bLow;\n");
fprintf(fp, " UINT8 bHigh;\n");
fprintf(fp, " UINT8 bCarry;\n");
fprintf(fp, "\n");
fprintf(fp, " if (NULL == pbAddAdcTable)\n");
fprintf(fp, " {\n");
fprintf(fp, " pbAddAdcTable = malloc(256*256*2);\n");
fprintf(fp, "\n");
fprintf(fp, " if (NULL == pbAddAdcTable)\n");
fprintf(fp, " {\n");
fprintf(fp, " return;\n");
fprintf(fp, " }\n");
fprintf(fp, "\n");
fprintf(fp, " pbTempPtr = pbAddAdcTable;\n\n");
fprintf(fp, " pbSubSbcTable = malloc(256*256*2);\n");
fprintf(fp, "\n");
fprintf(fp, " if (NULL == pbSubSbcTable)\n");
fprintf(fp, " {\n");
fprintf(fp, " return;\n");
fprintf(fp, " }\n");
fprintf(fp, "\n");
fprintf(fp, " pbTempPtr2 = pbSubSbcTable;\n");
fprintf(fp, "\n");
fprintf(fp, " for (dwLoop = 0; dwLoop < (256*256*2); dwLoop++)\n");
fprintf(fp, " {\n");
fprintf(fp, " bLow = dwLoop & 0xff;\n");
fprintf(fp, " bHigh = (dwLoop >> 8) & 0xff;\n");
fprintf(fp, " bCarry = (dwLoop >> 16);\n");
fprintf(fp, "\n");
fprintf(fp, " bFlag = 0;\n");
fprintf(fp, " bNewAdd = bHigh + bLow + bCarry;\n");
fprintf(fp, "\n");
fprintf(fp, " if (0 == bNewAdd)\n");
fprintf(fp, " {\n");
fprintf(fp, " bFlag |= Z80_FLAG_ZERO;\n");
fprintf(fp, " }\n");
fprintf(fp, " else\n");
fprintf(fp, " {\n");
fprintf(fp, " bFlag = bNewAdd & 0x80; /* Sign flag */\n");
fprintf(fp, " }\n");
fprintf(fp, "\n");
fprintf(fp, " if (((UINT32) bLow + (UINT32) bHigh + (UINT32) bCarry) >= 0x100)\n");
fprintf(fp, " {\n");
fprintf(fp, " bFlag |= Z80_FLAG_CARRY;\n");
fprintf(fp, " }\n");
fprintf(fp, "\n");
fprintf(fp, " if ( ((bLow ^ bHigh ^ 0x80) & (bLow ^ (bNewAdd & 0x80))) & 0x80)\n");
fprintf(fp, " {\n");
fprintf(fp, " bFlag |= Z80_FLAG_OVERFLOW_PARITY;\n");
fprintf(fp, " }\n");
fprintf(fp, "\n");
fprintf(fp, " if (((bLow & 0x0f) + (bHigh & 0x0f) + bCarry) >= 0x10)\n");
fprintf(fp, " {\n");
fprintf(fp, " bFlag |= Z80_FLAG_HALF_CARRY;\n");
fprintf(fp, " }\n");
fprintf(fp, "\n");
fprintf(fp, " *pbTempPtr++ = bFlag; /* Store our new flag */\n\n");
fprintf(fp, " // Now do subtract - Zero\n");
fprintf(fp, "\n");
fprintf(fp, " bFlag = Z80_FLAG_NEGATIVE;\n");
fprintf(fp, " bNewSub = bHigh - bLow - bCarry;\n");
fprintf(fp, "\n");
fprintf(fp, " if (0 == bNewSub)\n");
fprintf(fp, " {\n");
fprintf(fp, " bFlag |= Z80_FLAG_ZERO;\n");
fprintf(fp, " }\n");
fprintf(fp, " else\n");
fprintf(fp, " {\n");
fprintf(fp, " bFlag |= bNewSub & 0x80; /* Sign flag */\n");
fprintf(fp, " }\n");
fprintf(fp, "\n");
fprintf(fp, " if ( ((INT32) bHigh - (INT32) bLow - (INT32) bCarry) < 0)\n");
fprintf(fp, " {\n");
fprintf(fp, " bFlag |= Z80_FLAG_CARRY;\n");
fprintf(fp, " }\n");
fprintf(fp, "\n");
fprintf(fp, " if ( ((INT32) (bHigh & 0xf) - (INT32) (bLow & 0x0f) - (INT32) bCarry) < 0)\n");
fprintf(fp, " {\n");
fprintf(fp, " bFlag |= Z80_FLAG_HALF_CARRY;\n");
fprintf(fp, " }\n");
fprintf(fp, "\n");
fprintf(fp, " if ( ((bLow ^ bHigh) & (bHigh ^ bNewSub) & 0x80) )\n");
fprintf(fp, " {\n");
fprintf(fp, " bFlag |= Z80_FLAG_OVERFLOW_PARITY;\n");
fprintf(fp, " }\n");
fprintf(fp, "\n");
fprintf(fp, " *pbTempPtr2++ = bFlag; /* Store our sub flag */\n");
fprintf(fp, "\n");
fprintf(fp, " }\n");
fprintf(fp, " }\n");
fprintf(fp, "}\n");
}
else
{
assert(0);
}
}
void ShutdownCode(void)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
fprintf(fp, " global _%sshutdown\n", cpubasename);
fprintf(fp, " global %sshutdown_\n", cpubasename);
if (bPlain)
fprintf(fp, " global %sshutdown\n", cpubasename);
sprintf(procname, "%sshutdown_", cpubasename);
ProcBegin(0xffffffff);
fprintf(fp, "_%sshutdown:\n", cpubasename);
if (bPlain)
fprintf(fp, "%sshutdown:\n", cpubasename);
fprintf(fp, " ret\n\n");
}
else
if (MZ80_C == bWhat)
{
fprintf(fp, "/* Shut down MZ80 */\n\n");
fprintf(fp, "void %sshutdown(void)\n", cpubasename);
fprintf(fp, "{\n");
fprintf(fp, "}\n\n");
}
else
{
assert(0);
}
}
void DebuggerCode(void)
{
if (MZ80_ASSEMBLY_X86 == bWhat)
{
Alignment();
fprintf(fp, ";\n");
fprintf(fp, "; In : EAX=Reg #, ESI=Context address\n");
fprintf(fp, "; Out: EAX=Value of register\n");
fprintf(fp, ";\n");
fprintf(fp, "getRegValueInternal:\n");
fprintf(fp, " push ecx\n");
fprintf(fp, " push edx\n\n");
fprintf(fp, " cmp eax, CPUREG_MAXINDEX\n");
fprintf(fp, " jae badIndex2\n\n");
fprintf(fp, " shl eax, 4 ; Times 16 for table entry size\n");
fprintf(fp, " add eax, RegTable ; Now it's the memory location\n");
fprintf(fp, " mov edx, [eax+4] ; Get the offset of the register\n");
fprintf(fp, " mov edx, [edx + esi] ; Get our value\n");
fprintf(fp, " mov ecx, [eax+8] ; Get our shift value\n");
fprintf(fp, " shr edx, cl ; Shift it right by a value\n");
fprintf(fp, " and edx, [eax+12] ; Mask off any unneeded bits\n");
fprintf(fp, " mov eax, edx ; Put our value in EAX\n");
fprintf(fp, " jmp short indexExit ; Index's exit!\n");
fprintf(fp, "badIndex2:\n");
fprintf(fp, " mov eax, 0ffffffffh\n\n");
fprintf(fp, "indexExit:\n");
fprintf(fp, " pop edx\n");
fprintf(fp, " pop ecx\n");
fprintf(fp, " ret\n\n");
Alignment();
fprintf(fp, ";\n");
fprintf(fp, "; In : EAX=Value, EDX=Reg #, ESI=Context address\n");
fprintf(fp, "; Out: EAX=Value of register\n");
fprintf(fp, ";\n");
fprintf(fp, "convertValueToText:\n");
fprintf(fp, " push ecx\n");
fprintf(fp, " push edx\n\n");
fprintf(fp, " cmp edx, CPUREG_MAXINDEX\n");
fprintf(fp, " jae badIndex3\n\n");
fprintf(fp, " shl edx, 4 ; Times 16 for table entry size\n");
fprintf(fp, " add edx, RegTable ; Now it's the memory location\n");
fprintf(fp, " mov edx, [edx + 12] ; Shift mask\n");
fprintf(fp, " xor ecx, ecx ; Zero our shift\n");
fprintf(fp, "shiftLoop:\n");
fprintf(fp, " test edx, 0f0000000h ; High nibble nonzero yet?\n");
fprintf(fp, " jnz convertLoop ; Yup!\n");
fprintf(fp, " shl edx, 4 ; Move over, bacon\n");
fprintf(fp, " shl eax, 4 ; Move the value over, too\n");
fprintf(fp, " jmp short shiftLoop ; Keep shiftin'\n\n");
fprintf(fp, "convertLoop:\n");
fprintf(fp, " mov ecx, eax ; Get our value\n");
fprintf(fp, " shr ecx, 28 ; Only the top nibble\n");
fprintf(fp, " add cl, '0' ; Convert to ASCII\n");
fprintf(fp, " cmp cl, '9' ; Greater than 9?\n");
fprintf(fp, " jbe noAdd ; Nope! Don't add it\n");
fprintf(fp, " add cl, 32+7 ; Convert from lowercase a-f\n");
fprintf(fp, "noAdd:\n");
fprintf(fp, " mov [edi], cl ; New value storage\n");
fprintf(fp, " inc edi ; Next byte, please\n");
fprintf(fp, " shl eax, 4 ; Move the mask over\n");
fprintf(fp, " shl edx, 4 ; Move the mask over\n");
fprintf(fp, " jnz convertLoop ; Keep convertin'\n\n");
fprintf(fp, "badIndex3:\n");
fprintf(fp, " mov [edi], byte 0 ; Null terminate the sucker!\n");
fprintf(fp, " pop edx\n");
fprintf(fp, " pop ecx\n");
fprintf(fp, " ret\n\n");
fprintf(fp, " global _%sSetRegisterValue\n", cpubasename);
fprintf(fp, " global %sSetRegisterValue_\n", cpubasename);
if (bPlain)
fprintf(fp, " global %sSetRegisterValue\n", cpubasename);
sprintf(procname, "%sSetRegisterValue_", cpubasename);
ProcBegin(0xffffffff);
fprintf(fp, "_%sSetRegisterValue:\n", cpubasename);
if (bPlain)
fprintf(fp, "%sSetRegisterValue:\n", cpubasename);
fprintf(fp, " push esi\n");
fprintf(fp, " push edi\n");
fprintf(fp, " push edx\n");
fprintf(fp, " push ecx\n");
if (bUseStack)
{
fprintf(fp, " mov eax, [esp+20] ; Get our register #\n");
fprintf(fp, " mov esi, [esp+24] ; Get our context address\n");
fprintf(fp, " mov edi, [esp+28] ; Value to assign\n");
}
else
{
fprintf(fp, " mov esi, eax ; Get context\n");
fprintf(fp, " mov eax, edx ; Get register # in EAX\n");
fprintf(fp, " mov edi, ebx ; Get value to assign\n");
}
fprintf(fp, " or esi, esi ; Are we NULL?\n");
fprintf(fp, " jnz userDefined\n");
fprintf(fp, " mov esi, _%scontextBegin\n", cpubasename);
fprintf(fp, "userDefined:\n\n");
fprintf(fp, " shl eax, 4 ; Times 16 for reg entry size\n");
fprintf(fp, " add eax, RegTable\n");
fprintf(fp, " mov edx, [eax+12] ; Our mask\n");
fprintf(fp, " not edx ; Invert EDX!\n");
fprintf(fp, " test edi, edx ; Did we set any invalid bits?\n");
fprintf(fp, " jnz rangeViolation\n\n");
fprintf(fp, " not edx ; Toggle it back to normal\n");
fprintf(fp, " mov ecx, [eax+8] ; Get our shift value\n");
fprintf(fp, " shl edx, cl ; Shift our mask\n");
fprintf(fp, " shl eax, cl ; And our value to OR in\n");
fprintf(fp, " not edx ; Make it the inverse of what we want\n");
fprintf(fp, " mov eax, [eax+4] ; Get our offset into the context\n");
fprintf(fp, " and [esi+eax], edx ; Mask off the bits we're changin\n");
fprintf(fp, " or [esi+eax], edi ; Or in our new value\n\n");
fprintf(fp, " xor eax, eax\n");
fprintf(fp, " jmp short setExit\n\n");
fprintf(fp, "rangeViolation:\n");
fprintf(fp, " mov eax, 0ffffffffh\n\n");
fprintf(fp, "setExit:\n");
fprintf(fp, " pop ecx\n");
fprintf(fp, " pop edx\n");
fprintf(fp, " pop edi\n");
fprintf(fp, " pop esi\n\n");
fprintf(fp, " ret\n\n");
Alignment();
fprintf(fp, " global _%sGetRegisterValue\n", cpubasename);
fprintf(fp, " global %sGetRegisterValue_\n", cpubasename);
if (bPlain)
fprintf(fp, " global %sGetRegisterValue\n", cpubasename);
sprintf(procname, "%sGetRegisterValue_", cpubasename);
ProcBegin(0xffffffff);
fprintf(fp, "_%sGetRegisterValue:\n", cpubasename);
if (bPlain)
fprintf(fp, "%sGetRegisterValue:\n", cpubasename);
fprintf(fp, " push esi\n");
if (bUseStack)
{
fprintf(fp, " mov eax, [esp+8] ; Get our register #\n");
fprintf(fp, " mov esi, [esp+12] ; Get our context address\n");
}
else
{
fprintf(fp, " mov esi, eax ; Get context\n");
fprintf(fp, " mov eax, edx ; Get register # in EAX\n");
}
fprintf(fp, " or esi, esi ; Is context NULL?\n");
fprintf(fp, " jnz getVal ; Nope - use it!\n");
fprintf(fp, " mov esi, _%scontextBegin\n\n", cpubasename);
fprintf(fp, "getVal:\n");
fprintf(fp, " call getRegValueInternal\n\n");
fprintf(fp, " pop esi\n");
fprintf(fp, " ret\n\n");
Alignment();
fprintf(fp, " global _%sGetRegisterName\n", cpubasename);
fprintf(fp, " global %sGetRegisterName_\n", cpubasename);
if (bPlain)
fprintf(fp, " global %sGetRegisterName\n", cpubasename);
sprintf(procname, "%sGetRegisterName_", cpubasename);
ProcBegin(0xffffffff);
fprintf(fp, "_%sGetRegisterName:\n", cpubasename);
if (bPlain)
fprintf(fp, "%sGetRegisterName:\n", cpubasename);
if (bUseStack)
{
fprintf(fp, " mov eax, [esp+4] ; Get our register #\n");
}
fprintf(fp, " cmp eax, CPUREG_MAXINDEX\n");
fprintf(fp, " jae badIndex\n");
fprintf(fp, " shl eax, 4 ; Times 16 bytes for each entry\n");
fprintf(fp, " mov eax, [eax+RegTable]\n");
fprintf(fp, " jmp nameExit\n\n");
fprintf(fp, "badIndex:\n");
fprintf(fp, " xor eax, eax\n\n");
fprintf(fp, "nameExit:\n");
fprintf(fp, " ret\n\n");
Alignment();
fprintf(fp, " global _%sGetRegisterTextValue\n", cpubasename);
fprintf(fp, " global %sGetRegisterTextValue_\n", cpubasename);
if (bPlain)
fprintf(fp, " global %sGetRegisterTextValue\n", cpubasename);
sprintf(procname, "%sGetRegisterTextValue_", cpubasename);
ProcBegin(0xffffffff);
fprintf(fp, "_%sGetRegisterTextValue:\n", cpubasename);
if (bPlain)
fprintf(fp, "%sGetRegisterTextValue:\n", cpubasename);
fprintf(fp, " push esi\n");
fprintf(fp, " push edi\n");
fprintf(fp, " push edx\n");
if (bUseStack)
{
fprintf(fp, " mov eax, [esp+16] ; Get our register #\n");
fprintf(fp, " mov esi, [esp+20] ; Get our context address\n");
fprintf(fp, " mov edi, [esp+24] ; Address to place text\n");
}
else
{
fprintf(fp, " mov esi, eax ; Get context\n");
fprintf(fp, " mov eax, edx ; Get register # in EAX\n");
fprintf(fp, " mov edi, ebx ; Address to place text\n");
}
fprintf(fp, " or esi, esi ; Is context NULL?\n");
fprintf(fp, " jnz getVal2 ; Nope - use it!\n");
fprintf(fp, " mov esi, _%scontextBegin\n\n", cpubasename);
fprintf(fp, "getVal2:\n");
fprintf(fp, " mov edx, eax ; Save off our index for later\n");
fprintf(fp, " call getRegValueInternal\n\n");
fprintf(fp, "; EAX Holds the value, EDX=Register #, and EDI=Destination!\n\n");
fprintf(fp, " call convertValueToText\n\n");
fprintf(fp, " pop edx\n");
fprintf(fp, " pop esi\n");
fprintf(fp, " pop edi\n");
fprintf(fp, " ret\n\n");
Alignment();
fprintf(fp, " global _%sWriteValue\n", cpubasename);
fprintf(fp, " global %sWriteValue_\n", cpubasename);
if (bPlain)
fprintf(fp, " global %sWriteValue\n", cpubasename);
sprintf(procname, "%sWriteValue_", cpubasename);
ProcBegin(0xffffffff);
fprintf(fp, "_%sWriteValue:\n", cpubasename);
if (bPlain)
fprintf(fp, "%sWriteValue:\n", cpubasename);
fprintf(fp, " push esi\n");
fprintf(fp, " push edi\n");
fprintf(fp, " push edx\n");
fprintf(fp, " push ebx\n");
fprintf(fp, " push ecx\n");
fprintf(fp, " push ebp\n");
if (bUseStack)
{
fprintf(fp, " mov eax, [esp+28] ; What kind of write is this?\n");
fprintf(fp, " mov ebx, [esp+32] ; Address\n");
fprintf(fp, " mov edx, [esp+36] ; Value\n");
}
else
{
fprintf(fp, " xchg edx, ebx ; Addr=EBX, value=EDX\n");
}
fprintf(fp, " cmp eax, 1 ; Is it a word write?\n");
fprintf(fp, " je near invalidWrite ; Yep - it's not valid\n");
fprintf(fp, " cmp eax, 2 ; Is it a dword write?\n");
fprintf(fp, " je near invalidWrite ; Yep - it's not valid\n\n");
fprintf(fp, " or eax, eax ; Is it a byte write?\n");
fprintf(fp, " jnz itsIoDummy ; Nope... it's an I/O write\n\n");
// Here we do a write memory byte
fprintf(fp, " mov ebp, [_z80Base] ; Base pointer comes back\n");
fprintf(fp, " mov edi, [_z80MemWrite] ; Point to the write array\n");
fprintf(fp, "checkLoop:\n");
fprintf(fp, " cmp [edi], word 0ffffh ; End of our list?\n");
fprintf(fp, " je memoryWrite ; Yes - go write it!\n");
fprintf(fp, " cmp bx, [edi] ; Are we smaller?\n");
fprintf(fp, " jb nextAddr ; Yes... go to the next addr\n");
fprintf(fp, " cmp bx, [edi+4] ; Are we smaller?\n");
fprintf(fp, " jbe callRoutine ; If not, go call it!\n");
fprintf(fp, "nextAddr:\n");
fprintf(fp, " add edi, 10h ; Next structure, please\n");
fprintf(fp, " jmp short checkLoop\n");
fprintf(fp, "callRoutine:\n");
fprintf(fp, "\n;\n; EBX=Address to target, DL=Byte to write \n;\n\n");
fprintf(fp, " cmp [edi+8], dword 0 ; Null handler?\n");
fprintf(fp, " je directWriteHandler2\n\n");
if (FALSE == bUseStack)
{
fprintf(fp, " mov eax, ebx ; Address\n");
fprintf(fp, " mov ebx, edi ; Pointer to struct (EDX Already has the byte to write)\n");
}
else
{
fprintf(fp, " push edi ; Handler\n");
fprintf(fp, " push edx ; Byte\n");
fprintf(fp, " push ebx ; Address\n");
}
fprintf(fp, " call dword [edi + 8] ; Go call our handler\n");
if (bUseStack)
{
fprintf(fp, " add esp, 12\n");
}
fprintf(fp, " jmp short itsGood\n");
fprintf(fp, "directWriteHandler2:\n");
fprintf(fp, " sub ebx, [edi] ; Subtract our offset\n");
fprintf(fp, " add ebx, [edi+12] ; Add in the base address\n");
fprintf(fp, " mov [ebx], dl ; Store our byte\n");
fprintf(fp, " jmp short itsGood\n");
fprintf(fp, "memoryWrite:\n");
fprintf(fp, " mov [ebp + ebx], dl\n\n");
fprintf(fp, " jmp short itsGood\n");
// Here we do an "out"
fprintf(fp, "itsIoDummy:\n");
fprintf(fp, " mov edi, [_z80IoWrite] ; Point to the I/O write array\n");
fprintf(fp, "IOCheck:\n");
fprintf(fp, " cmp [edi], word 0ffffh ; End of our list?\n");
fprintf(fp, " je itsGood ; Yes - ignore it!\n");
fprintf(fp, " cmp bx, [edi] ; Are we smaller?\n");
fprintf(fp, " jb nextIOAddr ; Yes... go to the next addr\n");
fprintf(fp, " cmp bx, [edi+2] ; Are we bigger?\n");
fprintf(fp, " jbe callIOHandler ; If not, go call it!\n");
fprintf(fp, "nextIOAddr:\n");
fprintf(fp, " add edi, 0ch ; Next structure, please\n");
fprintf(fp, " jmp short IOCheck\n");
fprintf(fp, "callIOHandler:\n");
if (FALSE == bUseStack)
{
fprintf(fp, " mov eax, ebx ; Address\n");
fprintf(fp, " mov ebx, edi ; Pointer to struct (EDX Already has the byte to write)\n");
}
else
{
fprintf(fp, " push edi ; Handler\n");
fprintf(fp, " push edx ; Byte\n");
fprintf(fp, " push ebx ; Address\n");
}
fprintf(fp, " call dword [edi+4] ; Call the handler!\n");
if (bUseStack)
fprintf(fp, " add esp, 12\n");
fprintf(fp, " jmp short itsGood\n\n");
// Errors and whatnot
fprintf(fp, "invalidWrite:\n");
fprintf(fp, " mov eax, 0ffffffffh\n");
fprintf(fp, " jmp short writeValueExit\n\n");
fprintf(fp, "itsGood:\n");
fprintf(fp, " xor eax, eax\n\n");
fprintf(fp, "writeValueExit:\n");
fprintf(fp, " pop ebp\n");
fprintf(fp, " pop ecx\n");
fprintf(fp, " pop ebx\n");
fprintf(fp, " pop edx\n");
fprintf(fp, " pop esi\n");
fprintf(fp, " pop edi\n");
fprintf(fp, " ret\n\n");
Alignment();
fprintf(fp, " global _%sReadValue\n", cpubasename);
fprintf(fp, " global %sReadValue_\n", cpubasename);
if (bPlain)
fprintf(fp, " global %sReadValue\n", cpubasename);
sprintf(procname, "%sReadValue_", cpubasename);
ProcBegin(0xffffffff);
fprintf(fp, "_%sReadValue:\n", cpubasename);
if (bPlain)
fprintf(fp, "%sReadValue:\n", cpubasename);
fprintf(fp, " push esi\n");
fprintf(fp, " push edi\n");
fprintf(fp, " push edx\n");
fprintf(fp, " push ebx\n");
fprintf(fp, " push ecx\n");
fprintf(fp, " push ebp\n");
if (bUseStack)
{
fprintf(fp, " mov eax, [esp+28] ; What kind of read is this?\n");
fprintf(fp, " mov ebx, [esp+32] ; Address\n");
}
else
{
fprintf(fp, " xchg edx, ebx ; Addr=EBX\n");
}
fprintf(fp, " cmp eax, 1 ; Is it a word read?\n");
fprintf(fp, " je near invalidRead ; Yep - it's not valid\n");
fprintf(fp, " cmp eax, 2 ; Is it a dword read?\n");
fprintf(fp, " je near invalidRead ; Yep - it's not valid\n\n");
fprintf(fp, " or eax, eax ; Is it a byte read?\n");
fprintf(fp, " jnz itsIoDummyRead ; Nope... it's an I/O read\n\n");
// Here we do a read memory byte
fprintf(fp, " mov ebp, [_z80Base] ; Base pointer comes back\n");
fprintf(fp, " mov edi, [_z80MemRead] ; Point to the read array\n");
fprintf(fp, "checkLoopRead:\n");
fprintf(fp, " cmp [edi], word 0ffffh ; End of our list?\n");
fprintf(fp, " je memoryRead ; Yes - go read it!\n");
fprintf(fp, " cmp bx, [edi] ; Are we smaller?\n");
fprintf(fp, " jb nextAddrRead ; Yes... go to the next addr\n");
fprintf(fp, " cmp bx, [edi+4] ; Are we smaller?\n");
fprintf(fp, " jbe callRoutineRead ; If not, go call it!\n");
fprintf(fp, "nextAddrRead:\n");
fprintf(fp, " add edi, 10h ; Next structure, please\n");
fprintf(fp, " jmp short checkLoopRead\n");
fprintf(fp, "callRoutineRead:\n");
fprintf(fp, "\n;\n; EBX=Address to target\n;\n\n");
fprintf(fp, " cmp [edi+8], dword 0 ; NULL HAndler?\n");
fprintf(fp, " je handleSharedRead\n\n");
if (FALSE == bUseStack)
{
fprintf(fp, " mov eax, ebx ; Address\n");
fprintf(fp, " mov edx, edi ; Pointer to struct\n");
}
else
{
fprintf(fp, " push edi ; Handler\n");
fprintf(fp, " push ebx ; Address\n");
}
fprintf(fp, " call dword [edi + 8] ; Go call our handler\n");
fprintf(fp, " mov dl, al ; Get our byte read\n");
if (bUseStack)
{
fprintf(fp, " add esp, 8\n");
}
fprintf(fp, " jmp short itsGoodRead\n\n");
fprintf(fp, "memoryRead:\n");
fprintf(fp, " mov dl, [ebp+ebx]\n\n");
fprintf(fp, " jmp short itsGoodRead\n\n");
fprintf(fp, "handleSharedRead:\n");
fprintf(fp, " sub ebx, [edi]\n");
fprintf(fp, " add ebx, [edi+12]\n");
fprintf(fp, " mov dl, [ebx]\n");
fprintf(fp, " jmp short itsGoodRead\n\n");
// Here we do an "out"
fprintf(fp, "itsIoDummyRead:\n");
fprintf(fp, " mov edi, [_z80IoRead] ; Point to the I/O read array\n");
fprintf(fp, " mov dl, 0ffh ; Assume no handler\n");
fprintf(fp, "IOCheckRead:\n");
fprintf(fp, " cmp [edi], word 0ffffh ; End of our list?\n");
fprintf(fp, " je itsGoodRead ; Yes - ignore it!\n");
fprintf(fp, " cmp bx, [edi] ; Are we smaller?\n");
fprintf(fp, " jb nextIOAddrRead ; Yes... go to the next addr\n");
fprintf(fp, " cmp bx, [edi+2] ; Are we bigger?\n");
fprintf(fp, " jbe callIOHandlerRead ; If not, go call it!\n");
fprintf(fp, "nextIOAddrRead:\n");
fprintf(fp, " add edi, 0ch ; Next structure, please\n");
fprintf(fp, " jmp short IOCheckRead\n");
fprintf(fp, "callIOHandlerRead:\n");
if (FALSE == bUseStack)
{
fprintf(fp, " mov eax, ebx ; Address\n");
fprintf(fp, " mov edx, edi ; Pointer to struct (EDX Already has the byte to write)\n");
}
else
{
fprintf(fp, " push edi ; Handler\n");
fprintf(fp, " push ebx ; Address\n");
}
fprintf(fp, " call dword [edi+4] ; Call the handler!\n");
fprintf(fp, " mov dl, al ; Get our byte read\n");
if (bUseStack)
fprintf(fp, " add esp, 8\n");
fprintf(fp, " jmp short itsGoodRead\n\n");
// Errors and whatnot
fprintf(fp, "invalidRead:\n");
fprintf(fp, " mov eax, 0ffffffffh\n");
fprintf(fp, " jmp short ReadValueExit\n\n");
fprintf(fp, "itsGoodRead:\n");
fprintf(fp, " xor eax, eax\n");
fprintf(fp, " mov al, dl\n\n");
fprintf(fp, "ReadValueExit:\n");
fprintf(fp, " pop ebp\n");
fprintf(fp, " pop ecx\n");
fprintf(fp, " pop ebx\n");
fprintf(fp, " pop edx\n");
fprintf(fp, " pop esi\n");
fprintf(fp, " pop edi\n");
fprintf(fp, " ret\n\n");
}
else
if (MZ80_C == bWhat)
{
}
}
EmitCode()
{
CodeSegmentBegin();
EmitCBInstructions();
EmitEDInstructions();
if (MZ80_ASSEMBLY_X86 == bWhat)
strcpy(mz80Index, "ix");
else
{
strcpy(mz80Index, "cpu.z80IX");
strcpy(mz80IndexHalfHigh, "cpu.z80XH");
strcpy(mz80IndexHalfLow, "cpu.z80XL");
}
strcpy(majorOp, "DD");
EmitDDInstructions();
if (MZ80_ASSEMBLY_X86 == bWhat)
strcpy(mz80Index, "iy");
else
{
strcpy(mz80Index, "cpu.z80IY");
strcpy(mz80IndexHalfHigh, "cpu.z80YH");
strcpy(mz80IndexHalfLow, "cpu.z80YL");
}
strcpy(majorOp, "FD");
EmitFDInstructions();
majorOp[0] = '\0';
EmitRegularInstructions();
ReadMemoryByteHandler();
WriteMemoryByteHandler();
if (bThroughCallHandler)
{
PushWordHandler();
PopWordHandler();
}
ReadIoHandler();
WriteIoHandler();
GetContextCode();
SetContextCode();
GetContextSizeCode();
GetTicksCode();
ReleaseTimesliceCode();
ResetCode();
IntCode();
NmiCode();
ExecCode();
InitCode();
ShutdownCode();
DebuggerCode();
CodeSegmentEnd();
}
main(int argc, char **argv)
{
UINT32 dwLoop = 0;
printf("MakeZ80 - V%s - Copyright 1996-2000 Neil Bradley ([email protected])\n", VERSION);
if (argc < 2)
{
printf("Usage: %s outfile [option1] [option2] ....\n", argv[0]);
printf("\n -s - Stack calling conventions (DJGPP, MSVC, Borland)\n");
printf(" -x86 - Emit an assembly version of mz80\n");
printf(" -c - Emit a C version of mz80\n");
printf(" -cs - All stack operations go through handlers\n");
printf(" -16 - Treat all I/O input and output as 16 bit (BC) instead of (C)\n");
printf(" -l - Create 'plain' labels - ones without leading or trailing _'s\n");
printf(" -nt - No timing additions occur\n");
printf(" -os2 - Emit OS/2 compatible segmentation pragmas\n");
exit(1);
}
dwLoop = 1;
while (dwLoop < argc)
{
if (strcmp("-x86", argv[dwLoop]) == 0 || strcmp("-X86", argv[dwLoop]) == 0)
bWhat = MZ80_ASSEMBLY_X86;
if (strcmp("-c", argv[dwLoop]) == 0 || strcmp("-C", argv[dwLoop]) == 0)
bWhat = MZ80_C;
if (strcmp("-cs", argv[dwLoop]) == 0 || strcmp("-cs", argv[dwLoop]) == 0)
bThroughCallHandler = TRUE;
if (strcmp("-s", argv[dwLoop]) == 0 || strcmp("-S", argv[dwLoop]) == 0)
bUseStack = 1;
if (strcmp("-l", argv[dwLoop]) == 0 || strcmp("-L", argv[dwLoop]) == 0)
bPlain = TRUE;
if (strcmp("-16", argv[dwLoop]) == 0)
b16BitIo = TRUE;
if (strcmp("-os2", argv[dwLoop]) == 0 || strcmp("-OS2", argv[dwLoop]) == 0)
bOS2 = TRUE;
if (strcmp("-nt", argv[dwLoop]) == 0)
{
bNoTiming = TRUE;
}
dwLoop++;
}
if (bWhat == MZ80_UNKNOWN)
{
fprintf(stderr, "Need emitted type qualifier\n");
exit(1);
}
for (dwLoop = 1; dwLoop < argc; dwLoop++)
if (argv[dwLoop][0] != '-')
{
fp = fopen(argv[dwLoop], "w");
break;
}
if (NULL == fp)
{
fprintf(stderr, "Can't open %s for writing\n", argv[1]);
exit(1);
}
strcpy(cpubasename, "mz80");
StandardHeader();
DataSegment();
EmitCode();
ProgramEnd();
fclose(fp);
}
|
the_stack_data/156392436.c |
#include<stdio.h>
int main(){
int i,j,n;
scanf("%d",&n);
//upper half of the pattern
for(i=0;i<n;i++){
for(j=0;j<(2*n);j++){
if(i>=j)//upper left triangle
printf("*");
else
printf(" ");
if(i>=(2*n-1)-j)//upper right triangle
printf("*");
else
printf(" ");
}
printf("\n");
}
//bottom half of the pattern
for(i=0;i<n;i++){
for(j=0;j<(2*n);j++){
if(i+j<=n-1)//bottom left triangle
printf("*");
else
printf(" ");
if((i+n)<=j)//bottom right triangle
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
|
the_stack_data/87636993.c | /* Copyright (C) 2002 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <[email protected]>, 2002.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
#include <errno.h>
#include <fcntl.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
static void
remove_sem (int status, void *arg)
{
sem_unlink (arg);
}
int
main (void)
{
sem_t *s;
sem_t *s2;
pid_t pid;
int val;
s = sem_open ("/glibc-tst-sem4", O_CREAT, 0600, 1);
if (s == SEM_FAILED)
{
if (errno == ENOSYS)
{
puts ("sem_open not supported. Oh well.");
return 0;
}
/* Maybe the shm filesystem has strict permissions. */
if (errno == EACCES)
{
puts ("sem_open not allowed. Oh well.");
return 0;
}
printf ("sem_open: %m\n");
return 1;
}
on_exit (remove_sem, (void *) "/glibc-tst-sem4");
/* We have the semaphore object. Now try again with O_EXCL, this
should fail. */
s2 = sem_open ("/glibc-tst-sem4", O_CREAT | O_EXCL, 0600, 1);
if (s2 != SEM_FAILED)
{
puts ("2nd sem_open didn't fail");
return 1;
}
if (errno != EEXIST)
{
puts ("2nd sem_open returned wrong error");
return 1;
}
/* Check the value. */
if (sem_getvalue (s, &val) == -1)
{
puts ("getvalue failed");
return 1;
}
if (val != 1)
{
printf ("initial value wrong: got %d, expected 1\n", val);
return 1;
}
if (TEMP_FAILURE_RETRY (sem_wait (s)) == -1)
{
puts ("1st sem_wait failed");
return 1;
}
pid = fork ();
if (pid == -1)
{
printf ("fork failed: %m\n");
return 1;
}
if (pid == 0)
{
/* Child. */
/* Check the value. */
if (sem_getvalue (s, &val) == -1)
{
puts ("child: getvalue failed");
return 1;
}
if (val != 0)
{
printf ("child: value wrong: got %d, expect 0\n", val);
return 1;
}
if (sem_post (s) == -1)
{
puts ("child: post failed");
return 1;
}
}
else
{
if (TEMP_FAILURE_RETRY (sem_wait (s)) == -1)
{
puts ("2nd sem_wait failed");
return 1;
}
if (sem_getvalue (s, &val) == -1)
{
puts ("parent: 2nd getvalue failed");
return 1;
}
if (val != 0)
{
printf ("parent: value wrong: got %d, expected 0\n", val);
return 1;
}
}
return 0;
}
|
the_stack_data/133847.c | #include <stdio.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
int main() {
struct stat s;
int f = open("/test", O_RDWR, 0777);
printf("posix_fadvise: %d\n", posix_fadvise(f, 3, 2, POSIX_FADV_DONTNEED));
printf("errno: %d\n", errno);
printf("\n");
errno = 0;
printf("posix_fallocate: %d\n", posix_fallocate(f, 3, 2));
printf("errno: %d\n", errno);
stat("/test", &s);
printf("st_size: %d\n", s.st_size);
memset(&s, 0, sizeof s);
printf("\n");
errno = 0;
printf("posix_fallocate2: %d\n", posix_fallocate(f, 3, 7));
printf("errno: %d\n", errno);
stat("/test", &s);
printf("st_size: %d\n", s.st_size);
memset(&s, 0, sizeof s);
return 0;
}
|
the_stack_data/655686.c | /*
John Carroll
COMP-1200: Assignment 03
Date Completed: February 7th, 2012
I worked on my assignment alone, using only course material.
Program: BMI Calculation
-----------------------------------------------------------------
Find the BMI for a given height(in inches) and weight(in pounds.)
Determine the BMI catagory.
Compute and display the target weight for a given BMI.
Compute the Idea Body Weight (IBW) for given height and gender.
------------------------------------------------------------------
*/
#include <stdio.h>
int main()
{ //VARIABLES
double weight, height, convertedHeight, convertedWeight, bmi, targetBmi, targetConvertedWeight, targetWeight, ibwWomen, ibwMen;
char gender;
/*-----------------------------------------------------------*/
//INPUT
//Prompt for height
printf("Enter the height in inches: "); // the prompt
scanf("%lf",&height); // reads height
//Prompt for weight
printf("Enter the weight in pounds: "); // the prompt
scanf("%lf",&weight); // reads weight
/*-----------------------------------------------------------*/
//COMPUTATION
//Compute the conversions of the inputs
convertedHeight = height*0.0254;
convertedWeight = weight/2.2046;
//Calculate the BMI, using the formula
bmi = (convertedWeight)/(convertedHeight*convertedHeight);
printf("\n"); // insert blank line
/*-----------------------------------------------------------*/
//OUTPUT
//Display BMI
printf("The BMI is: %-6.2lf\n", bmi);
//Determine the BMI's catagory.
if ( bmi < 25 )
printf("BMI Classification: Normal\n");
//Print if the BMI is in the normal category
else if ( bmi >= 25 && bmi < 30 )
printf("BMI Classification: Overweight\n");
//Print if the BMI is in the overweight category
else if ( bmi >= 30 )
printf("BMI Classification: Obese\n");
//Print if the BMI is in the obese category
printf("\n"); // insert blank line
/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/
//INPUT
//Prompt for targetted BMI
printf("Enter the target BMI: "); // the prompt
scanf("%lf",&targetBmi); // reads BMI
/*-----------------------------------------------------------*/
//COMPUTATION
//Calculate and then compute the conversions
targetConvertedWeight = targetBmi*(convertedHeight*convertedHeight);
targetWeight = targetConvertedWeight*2.2046;
/*-----------------------------------------------------------*/
//OUTPUT
//Display target weight
printf("The target weight is: %-6.2lf pounds.\n", targetWeight);
printf("\n"); // insert blank line
/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/
//INPUT
//Prompt for gender
printf("Is the person a female or male? Enter F or M: "); // the prompt
scanf(" %c", &gender); // reads gender
/*-----------------------------------------------------------*/
//COMPUTATION
//Compute the Idea Body Weight (IBW) for given height and gender.
ibwWomen = ((45.5 + 2.3 * (height - 60))*(2.2046));
ibwMen = ((50.0 + 2.3 * (height - 60))*(2.2046));
/*-----------------------------------------------------------*/
//OUTPUT
//if Female
if ((gender == 'F') || (gender == 'f')) printf("The ideal weight is %-6.2lf pounds.\n", ibwWomen);
//if Male
else if ((gender == 'M') || (gender == 'm')) printf("The ideal weight is %-6.2lf pounds.\n", ibwMen);
return 0;
}
|
the_stack_data/43887315.c | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(){
char pass[10] = "AABBCCDD";
char buf[100];
fgets(buf, 100, stdin);
buf[strcspn(buf, "\n")] = '\0';
if(!strncmp(pass, buf, sizeof(pass))){
printf("Greetings!\n");
return EXIT_SUCCESS;
} else {
printf(buf);
printf("\nPointer : %p \n",pass);
printf(" does not have access!\n");
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}
|
the_stack_data/220454395.c | /* This testcase is part of GDB, the GNU debugger.
Copyright 2019-2021 Free Software Foundation, Inc.
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, see <http://www.gnu.org/licenses/>. */
struct S
{
int a;
int b;
int c;
};
struct S g_s = {1, 2, 3};
static void
inc (void)
{
g_s.a++;;
}
int
main (void)
{
inc ();
return 0;
}
|
the_stack_data/1101849.c | extern void abort (void);
extern int inside_main;
__attribute__ ((__noinline__))
void *
memset (void *dst, int c, __SIZE_TYPE__ n)
{
while (n-- != 0)
n[(char *) dst] = c;
/* Single-byte memsets should be done inline when optimisation
is enabled. Do this after the copy in case we're being called to
initialize bss. */
#ifdef __OPTIMIZE__
if (inside_main && n < 2)
abort ();
#endif
return dst;
}
|
the_stack_data/212642588.c | extern char* getenv(const char *name);
struct s2 {
char *t2;
char *t3;
};
struct s1 {
char *t1;
struct s2 s2;
};
struct s2
foo()
{
struct s1 s1;
s1.s2.t2 = getenv("gude");
return s1.s2;
}
int
main()
{
struct s1 s1;
s1.s2 = foo();
char *t1 = s1.s2.t2;
return 0;
}
|
the_stack_data/23574727.c | int main() {
int a[] = {1, 2, 3};
char b[] = "abc";
char c;
int d;
c = b[3];
d = a[2];
return 0;
}
|
the_stack_data/1080738.c | /* Taxonomy Classification: 0000000000000154000100 */
/*
* WRITE/READ 0 write
* WHICH BOUND 0 upper
* DATA TYPE 0 char
* MEMORY LOCATION 0 stack
* SCOPE 0 same
* CONTAINER 0 no
* POINTER 0 no
* INDEX COMPLEXITY 0 constant
* ADDRESS COMPLEXITY 0 constant
* LENGTH COMPLEXITY 0 N/A
* ADDRESS ALIAS 0 none
* INDEX ALIAS 0 none
* LOCAL CONTROL FLOW 0 none
* SECONDARY CONTROL FLOW 1 if
* LOOP STRUCTURE 5 non-standard do-while
* LOOP COMPLEXITY 4 three
* ASYNCHRONY 0 no
* TAINT 0 no
* RUNTIME ENV. DEPENDENCE 0 no
* MAGNITUDE 1 1 byte
* CONTINUOUS/DISCRETE 0 discrete
* SIGNEDNESS 0 no
*/
/*
Copyright 2005 Massachusetts Institute of Technology
All rights reserved.
Redistribution and use of software in source and binary forms, with or without
modification, are permitted provided that the following conditions are met.
- Redistributions of source code must retain the above copyright notice,
this set of conditions and the disclaimer below.
- Redistributions in binary form must reproduce the copyright notice, this
set of conditions, and the disclaimer below in the documentation and/or
other materials provided with the distribution.
- Neither the name of the Massachusetts Institute of Technology nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS".
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 COPYRIGHT OWNER OR CONTRIBUTORS 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.
*/
int main(int argc, char *argv[])
{
int init_value;
int test_value;
int inc_value;
int loop_counter;
char buf[10];
init_value = 0;
test_value = 10;
inc_value = 10 - (10 - 1);
loop_counter = init_value;
do
{
/* BAD */
buf[10] = 'A';
if (loop_counter >= test_value) break;
}
while(loop_counter += inc_value);
return 0;
}
|
the_stack_data/131514.c | #include <stdio.h>
int some_display ();
int main(void)
{
int (*func_ptr)();
func_ptr = some_display;
printf("\nAddress of function some_display is %p\n", func_ptr);
(*func_ptr)();
return 0;
}
int some_display()
{
printf("\n--Displaying some text--\n");
return 0;
} |
the_stack_data/8868.c | int main() { return 0; }
|
the_stack_data/116252.c | /**************************************************************************
* fs_readonly_watchdog.c
*
* This is a small utility to restart a machine if a filesystem is
* remounted read-only. This is made to work around an iSCSI SAN
* intermittently dropping connection.
*
* It works by watching /proc/self/mounts for changes via pselect(2).
* When it changes it looks for any filesystems of type ext? that are
* read-only. If it finds a read-only filesystem, it then waits 60
* seconds and then if there are still read-only filesystems it
* reboots the system by sending SIGINT to PID 1.
*
* Build with:
* make fs_readonly_watchdog
*
* Run (as root):
* ./fs_readonly_watchdog &
*
**************************************************************************
* Copyright (c) 2015 James Klassen
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies of this Software or works derived from this Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
**************************************************************************/
#include<stdio.h>
#include<errno.h>
#include<fcntl.h>
#include<string.h>
#include<signal.h>
#include<sys/select.h>
#include<sys/stat.h>
#include<sys/time.h>
#include<sys/types.h>
#include<unistd.h>
#define LINE_MAXLEN 4096
const char* mounts_path = "/proc/self/mounts";
int read_mounts(int fd) {
char line[LINE_MAXLEN];
char *pos;
char *fs_spec;
char *fs_file;
char *vfs_type;
char *fs_mntops;
char *fs_freq;
char *fs_passno;
char *mntop;
char *saveptr;
int bytes = 0;
int err;
int read_only_count = 0;
/* Read file from beginning */
if(lseek(fd, 0, SEEK_SET) == -1) {
perror("can't seek");
return(1);
}
for(;;) {
memset(line, 0, LINE_MAXLEN);
for( bytes = 0, pos = line; bytes < LINE_MAXLEN; bytes++, pos++ ) {
err = read(fd, pos, 1);
if(err == 0) {
/* Reached end of file */
return(read_only_count);
}
if(err == -1) {
perror("Can't read mounts");
return(-1);
}
if(*pos == '\n') {
/* Got end of line, start parsing */
fs_spec = strtok_r(line, " ", &saveptr);
fs_file = strtok_r(NULL, " ", &saveptr);
vfs_type = strtok_r(NULL, " ", &saveptr);
fs_mntops = strtok_r(NULL, " ", &saveptr);
fs_freq = strtok_r(NULL, " ", &saveptr);
fs_passno = strtok_r(NULL, " ", &saveptr);
/* Look for ext[234] filesystems, ignore the rest */
if( vfs_type != NULL && strncmp( vfs_type, "ext", 3) == 0 ) {
/* Parse options */
for( mntop = strtok_r(fs_mntops, ",", &saveptr);
mntop != NULL;
mntop = strtok_r(NULL, ",", &saveptr)) {
if( strcmp("ro", mntop) == 0 ) {
printf("FOUND %s IS READ ONLY!\n", fs_file);
read_only_count++;
}
}
}
break; /* Drop out of for loop and get the next line */
}
if(bytes == LINE_MAXLEN) {
printf("Line too long, skipping...\n");
}
}
}
return(-1); /* Can't get here */
}
int wait_for_update(int fd) {
int ready;
fd_set fds;
FD_ZERO(&fds);
FD_SET(fd, &fds);
ready = pselect(fd + 1, NULL, NULL, &fds, NULL, NULL);
if (ready == 1) {
printf("Ready\n");
return ready;
} else if(ready == -1) {
perror("pselect failed");
}
return -1;
}
int main() {
int mounts_fd = 0;
int read_only_count;
struct timeval failure_time;
mounts_fd = open(mounts_path, O_RDONLY);
if(mounts_fd == -1) {
perror("Couldn't open mounts");
return(1);
}
for(;;) {
read_only_count = read_mounts(mounts_fd);
if(read_only_count == -1)
return(1);
else if(read_only_count > 0) {
/* We have a problem */
if( gettimeofday(&failure_time, NULL) == -1 ) {
perror("Can't get time of day");
return 1;
}
/* Wait 60 seconds */
sleep(60);
/* If still read only, reboot */
if( read_mounts(mounts_fd) > 0 ) {
/* Reboot the system */
if( kill((pid_t)1, SIGINT) == -1 ) {
perror("Can't reboot");
return 1;
}
}
}
/* Wait for update to mounts_path */
if( wait_for_update(mounts_fd) == -1)
return 1;
}
return 0;
}
|
the_stack_data/150143607.c | #include <stdio.h>
#include <stdlib.h>
int main() {
printf(" /|\n");
printf(" / |\n");
printf(" / |\n");
printf(" / |\n");
printf("/____|\n");
return 0;
} |
the_stack_data/212643600.c | /*54321
4321
321
21
1*/
#include<stdio.h>
int main(){
int i,j;
for (i=5;i>=1;i--){
for(j=i;j>=1;j--){
printf("%d",j);
}
printf("\n");
}
return 0;
} |
the_stack_data/108096.c | /*
Copyright (c) 2017. The YARA Authors. 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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER OR CONTRIBUTORS 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.
*/
#if defined(USE_OPENBSD_PROC)
// Prevent clang-format from complaining about the include order,
// changing the order breaks compilation.
//
// clang-format off
#include <sys/types.h>
#include <sys/ptrace.h>
#include <sys/sysctl.h>
#include <sys/wait.h>
#include <errno.h>
// clang-format on
#include <yara/error.h>
#include <yara/libyara.h>
#include <yara/mem.h>
#include <yara/proc.h>
typedef struct _YR_PROC_INFO
{
int pid;
uint64_t old_end;
struct kinfo_vmentry vm_entry;
} YR_PROC_INFO;
int _yr_process_attach(int pid, YR_PROC_ITERATOR_CTX* context)
{
int status;
size_t len = sizeof(struct kinfo_vmentry);
int mib[] = {CTL_KERN, KERN_PROC_VMMAP, pid};
YR_PROC_INFO* proc_info = (YR_PROC_INFO*) yr_malloc(sizeof(YR_PROC_INFO));
if (proc_info == NULL)
return ERROR_INSUFFICIENT_MEMORY;
memset(proc_info, 0, sizeof(YR_PROC_INFO));
proc_info->pid = pid;
if (ptrace(PT_ATTACH, pid, NULL, 0) == -1)
{
yr_free(proc_info);
return ERROR_COULD_NOT_ATTACH_TO_PROCESS;
}
status = 0;
if (waitpid(pid, &status, 0) == -1)
{
ptrace(PT_DETACH, proc_info->pid, NULL, 0);
yr_free(proc_info);
return ERROR_COULD_NOT_ATTACH_TO_PROCESS;
}
if (sysctl(mib, 3, &proc_info->vm_entry, &len, NULL, 0) < 0)
{
ptrace(PT_DETACH, proc_info->pid, NULL, 0);
yr_free(proc_info);
return ERROR_COULD_NOT_ATTACH_TO_PROCESS;
}
context->proc_info = proc_info;
return ERROR_SUCCESS;
}
int _yr_process_detach(YR_PROC_ITERATOR_CTX* context)
{
YR_PROC_INFO* proc_info = (YR_PROC_INFO*) context->proc_info;
ptrace(PT_DETACH, proc_info->pid, NULL, 0);
return ERROR_SUCCESS;
}
YR_API const uint8_t* yr_process_fetch_memory_block_data(YR_MEMORY_BLOCK* block)
{
YR_PROC_ITERATOR_CTX* context = (YR_PROC_ITERATOR_CTX*) block->context;
YR_PROC_INFO* proc_info = (YR_PROC_INFO*) context->proc_info;
struct ptrace_io_desc io_desc;
if (context->buffer_size < block->size)
{
if (context->buffer != NULL)
yr_free((void*) context->buffer);
context->buffer = (const uint8_t*) yr_malloc(block->size);
if (context->buffer != NULL)
{
context->buffer_size = block->size;
}
else
{
context->buffer_size = 0;
return NULL;
}
}
io_desc.piod_op = PIOD_READ_D;
io_desc.piod_offs = (void*) block->base;
io_desc.piod_addr = (void*) context->buffer;
io_desc.piod_len = block->size;
if (ptrace(PT_IO, proc_info->pid, (char*) &io_desc, 0) == -1)
return NULL;
return context->buffer;
}
YR_API YR_MEMORY_BLOCK* yr_process_get_next_memory_block(
YR_MEMORY_BLOCK_ITERATOR* iterator)
{
YR_PROC_ITERATOR_CTX* context = (YR_PROC_ITERATOR_CTX*) iterator->context;
YR_PROC_INFO* proc_info = (YR_PROC_INFO*) context->proc_info;
int mib[] = {CTL_KERN, KERN_PROC_VMMAP, proc_info->pid};
size_t len = sizeof(struct kinfo_vmentry);
iterator->last_error = ERROR_SUCCESS;
uint64_t current_begin = context->current_block.base +
context->current_block.size;
uint64_t max_process_memory_chunk;
yr_get_configuration(
YR_CONFIG_MAX_PROCESS_MEMORY_CHUNK, (void*) &max_process_memory_chunk);
if (proc_info->old_end <= current_begin)
{
if (sysctl(mib, 3, &proc_info->vm_entry, &len, NULL, 0) < 0)
return NULL;
// no more blocks
if (proc_info->old_end == proc_info->vm_entry.kve_end)
return NULL;
current_begin = proc_info->vm_entry.kve_start;
proc_info->old_end = proc_info->vm_entry.kve_end;
proc_info->vm_entry.kve_start = proc_info->vm_entry.kve_start + 1;
}
context->current_block.base = current_begin;
context->current_block.size = yr_min(
proc_info->old_end - current_begin, max_process_memory_chunk);
assert(context->current_block.size > 0);
return &context->current_block;
}
YR_API YR_MEMORY_BLOCK* yr_process_get_first_memory_block(
YR_MEMORY_BLOCK_ITERATOR* iterator)
{
YR_PROC_ITERATOR_CTX* context = (YR_PROC_ITERATOR_CTX*) iterator->context;
YR_PROC_INFO* proc_info = (YR_PROC_INFO*) context->proc_info;
proc_info->vm_entry.kve_start = 0;
return yr_process_get_next_memory_block(iterator);
}
#endif
|
the_stack_data/27261.c | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <CL/cl.h>
#define MAX_PLATFORMS 32
#define MAX_DEVICES 32
#define MAX_BINARIES 32
char kernel[] = "__kernel void k() {\n return;\n}";
int
main(void){
cl_int err;
cl_platform_id platforms[MAX_PLATFORMS];
cl_uint nplatforms;
cl_device_id devices[MAX_DEVICES + 1]; // + 1 for duplicate test
cl_device_id device_id0;
cl_uint num_devices;
cl_uint i;
size_t num_binaries;
const unsigned char **binaries = NULL;
size_t *binary_sizes = NULL;
size_t num_bytes_copied;
cl_int binary_statuses[MAX_BINARIES];
cl_int binary_statuses2[MAX_BINARIES];
cl_program program = NULL;
cl_program program_with_binary = NULL;
err = clGetPlatformIDs(MAX_PLATFORMS, platforms, &nplatforms);
if (err != CL_SUCCESS && !nplatforms)
return EXIT_FAILURE;
err = clGetDeviceIDs(platforms[0], CL_DEVICE_TYPE_ALL, MAX_DEVICES,
devices, &num_devices);
if (err != CL_SUCCESS)
return EXIT_FAILURE;
cl_context context = clCreateContext(NULL, num_devices, devices, NULL, NULL, &err);
if (err != CL_SUCCESS)
return EXIT_FAILURE;
size_t kernel_size = strlen(kernel);
char* kernel_buffer = kernel;
program = clCreateProgramWithSource(context, 1, (const char**)&kernel_buffer,
&kernel_size, &err);
if (err != CL_SUCCESS)
return EXIT_FAILURE;
err = clBuildProgram(program, num_devices, devices, NULL, NULL, NULL);
if (err != CL_SUCCESS)
return EXIT_FAILURE;
err = clGetProgramInfo(program, CL_PROGRAM_BINARY_SIZES, 0, 0, &num_binaries);
if (err != CL_SUCCESS)
goto FREE_AND_EXIT;
num_binaries = num_binaries/sizeof(size_t);
binary_sizes = (size_t*)malloc(num_binaries * sizeof(size_t));
binaries = (const unsigned char**)calloc(num_binaries, sizeof(unsigned char*));
err = clGetProgramInfo(program, CL_PROGRAM_BINARY_SIZES,
num_binaries*sizeof(size_t), binary_sizes ,
&num_bytes_copied);
if (err != CL_SUCCESS)
goto FREE_AND_EXIT;
for (i = 0; i < num_binaries; ++i)
binaries[i] = (const unsigned char*) malloc(binary_sizes[i] *
sizeof(const unsigned char));
err = clGetProgramInfo(program, CL_PROGRAM_BINARIES,
num_binaries*sizeof(char*), binaries, &num_bytes_copied);
if (err != CL_SUCCESS)
goto FREE_AND_EXIT;
cl_int num = num_binaries < num_devices ? num_binaries : num_devices;
if (num == 0)
{
err = !CL_SUCCESS;
goto FREE_AND_EXIT;
}
program_with_binary = clCreateProgramWithBinary(context, num, devices, binary_sizes,
binaries, binary_statuses, &err);
if (err != CL_SUCCESS)
goto FREE_AND_EXIT;
clReleaseProgram(program_with_binary);
for (i = 0; i < num; i++)
{
if (binary_statuses[i] != CL_SUCCESS)
{
err = !CL_SUCCESS;
goto FREE_AND_EXIT;
}
}
// negative test1: invalid device
device_id0 = devices[0];
devices[0] = NULL; // invalid device
program_with_binary = clCreateProgramWithBinary(context, num, devices, binary_sizes,
binaries, binary_statuses, &err);
if (err != CL_INVALID_DEVICE || program_with_binary != NULL)
{
err = !CL_SUCCESS;
goto FREE_AND_EXIT;
}
err = CL_SUCCESS;
devices[0] = device_id0;
for (i = 0; i < num_binaries; ++i) free((void*)binaries[i]);
free(binary_sizes);
free(binaries);
// negative test2: duplicate device
num_binaries = 2;
devices[1] = devices[0]; // duplicate
binary_sizes = (size_t*)malloc(num_binaries * sizeof(size_t));
binaries = (const unsigned char**)calloc(num_binaries, sizeof(unsigned char*));
err = clGetProgramInfo(program, CL_PROGRAM_BINARY_SIZES, 1*sizeof(size_t),
binary_sizes , &num_bytes_copied);
if (err != CL_SUCCESS)
goto FREE_AND_EXIT;
binary_sizes[1] = binary_sizes[0];
binaries[0] = (const unsigned char*) malloc(binary_sizes[0] *
sizeof(const unsigned char));
binaries[1] = (const unsigned char*) malloc(binary_sizes[1] *
sizeof(const unsigned char));
err = clGetProgramInfo(program, CL_PROGRAM_BINARIES, 1 * sizeof(char*),
binaries, &num_bytes_copied);
if (err != CL_SUCCESS)
goto FREE_AND_EXIT;
memcpy((void*)binaries[1], (void*)binaries[0], binary_sizes[0]);
program_with_binary = clCreateProgramWithBinary(context, 2, devices, binary_sizes,
binaries, binary_statuses2, &err);
if (err != CL_INVALID_DEVICE || program_with_binary != NULL)
{
err = !CL_SUCCESS;
goto FREE_AND_EXIT;
}
err = CL_SUCCESS;
FREE_AND_EXIT:
// Free resources
for (i = 0; i < num_binaries; ++i)
if (binaries)
if(binaries[i])
free((void*)binaries[i]);
if (binary_sizes)
free(binary_sizes);
if (binaries)
free(binaries);
if (program)
clReleaseProgram(program);
if (program_with_binary)
clReleaseProgram(program_with_binary);
return err == CL_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;
}
|
the_stack_data/175142960.c | /* We put the font object files in the 'AdditionalDependencies' list,
* but we need at least one C file to link the library.
* Since we need different object files for 32 and 64 bit builds,
* we can't just include them in the file list.
*/
int libresources_dummy;
|
the_stack_data/1017970.c | const char net_ixgbe_pmd_info[] __attribute__((used)) = "PMD_INFO_STRING= {\"name\" : \"net_ixgbe\", \"kmod\" : \"* igb_uio | uio_pci_generic | vfio-pci\", \"pci_ids\" : [[32902, 4278, 65535, 65535],[32902, 5384, 65535, 65535],[32902, 4294, 65535, 65535],[32902, 4295, 65535, 65535],[32902, 4296, 65535, 65535],[32902, 5387, 65535, 65535],[32902, 4315, 65535, 65535],[32902, 4317, 65535, 65535],[32902, 4332, 65535, 65535],[32902, 4337, 65535, 65535],[32902, 4321, 65535, 65535],[32902, 4340, 65535, 65535],[32902, 4343, 65535, 65535],[32902, 5396, 65535, 65535],[32902, 5399, 65535, 65535],[32902, 4344, 65535, 65535],[32902, 4345, 65535, 65535],[32902, 4347, 65535, 65535],[32902, 5418, 65535, 65535],[32902, 5417, 65535, 65535],[32902, 5383, 65535, 65535],[32902, 5453, 65535, 65535],[32902, 5450, 65535, 65535],[32902, 5464, 65535, 65535],[32902, 5463, 65535, 65535],[32902, 4348, 65535, 65535],[32902, 5404, 65535, 65535],[32902, 5455, 65535, 65535],[32902, 5416, 65535, 65535],[32902, 5472, 65535, 65535],[32902, 5548, 65535, 65535],[32902, 5549, 65535, 65535],[32902, 5550, 65535, 65535],[32902, 5475, 65535, 65535],[32902, 5585, 65535, 65535],[32902, 5570, 65535, 65535],[32902, 5571, 65535, 65535],[32902, 5572, 65535, 65535],[32902, 5574, 65535, 65535],[32902, 5575, 65535, 65535],[32902, 5576, 65535, 65535],[32902, 5578, 65535, 65535],[32902, 5580, 65535, 65535],[32902, 5582, 65535, 65535],[32902, 5604, 65535, 65535],[32902, 5605, 65535, 65535],[32902, 5546, 65535, 65535],[32902, 5547, 65535, 65535] ]}";
const char net_ixgbe_vf_pmd_info[] __attribute__((used)) = "PMD_INFO_STRING= {\"name\" : \"net_ixgbe_vf\", \"kmod\" : \"* igb_uio | vfio-pci\", \"pci_ids\" : [[32902, 4333, 65535, 65535],[32902, 5422, 65535, 65535],[32902, 5397, 65535, 65535],[32902, 5424, 65535, 65535],[32902, 5476, 65535, 65535],[32902, 5477, 65535, 65535],[32902, 5573, 65535, 65535],[32902, 5556, 65535, 65535],[32902, 5544, 65535, 65535],[32902, 5545, 65535, 65535] ]}";
|
the_stack_data/237642967.c | #include <stdio.h>
#include <stdbool.h>
bool alphabetic(const char c)
{
if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
return true;
else
return false;
}
void readLine(char buffer[])
{
char character;
int i = 0;
do
{
character = getchar();
buffer[i] = character;
++i;
}
while(character != '\n');
buffer[i -1]= '\0';
}
int countWords(const char string[])
{
int i, wordCount = 0;
bool lookingForWord = true, alphabetic(const char c);
for(i = 0; string[i] != '\0'; ++i)
if(alphabetic(string[i]))
{
if(lookingForWord)
{
++wordCount;
lookingForWord = false;
}
}
else
lookingForWord = true;
return wordCount;
}
int main(void)
{
char text[81];
int totalWords = 0;
int countWords(const char string[]);
void readLine(char buffer[]);
bool endOfText = false;
printf("Type in your text.\n");
printf("When you are done, press 'RETURN'.\n\n");
while(!endOfText)
{
readLine(text);
if(text[0] == '\0')
endOfText = true;
else
totalWords += countWords(text);
}
printf("\nThere are %i words in the above text.\n", totalWords);
return 0;
} |
the_stack_data/153269069.c | #define _GNU_SOURCE
#include <errno.h>
#include <error.h>
#define errorf(status, fmt, ...) \
error_at_line(status, errno, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
#include <dirent.h>
#include <ftw.h>
#include <sched.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/wait.h>
char *env_whitelist[] = {"TERM",
"DISPLAY",
"XAUTHORITY",
"HOME",
"XDG_RUNTIME_DIR",
"LANG",
"SSL_CERT_FILE",
"DBUS_SESSION_BUS_ADDRESS"};
char **env_build(char *names[], size_t len) {
char *env, **ret = malloc((len + 1) * sizeof(char *)), **ptr = ret;
for (size_t i = 0; i < len; i++) {
if ((env = getenv(names[i]))) {
if (asprintf(ptr++, "%s=%s", names[i], env) < 0)
errorf(EX_OSERR, "asprintf");
}
}
*ptr = NULL;
return ret;
}
struct bind {
char *from;
char *to;
};
struct bind binds[] = {{"/", "host"}, {"/proc", "proc"}, {"/sys", "sys"},
{"/nix", "nix"}, {"/tmp", "tmp"}, {"/var", "var"},
{"/run", "run"}, {"/dev", "dev"}, {"/home", "home"}};
void bind(struct bind *bind) {
DIR *src = opendir(bind->from);
if (src) {
if (closedir(src) < 0)
errorf(EX_IOERR, "closedir");
if (mkdir(bind->to, 0755) < 0)
errorf(EX_IOERR, "mkdir");
if (mount(bind->from, bind->to, "bind", MS_BIND | MS_REC, NULL) < 0)
errorf(EX_OSERR, "mount");
} else {
// https://github.com/NixOS/nixpkgs/issues/31104
if (errno != ENOENT)
errorf(EX_OSERR, "opendir");
}
}
void spitf(char *path, char *fmt, ...) {
va_list args;
va_start(args, fmt);
FILE *f = fopen(path, "w");
if (f == NULL)
errorf(EX_IOERR, "spitf(%s): fopen", path);
if (vfprintf(f, fmt, args) < 0)
errorf(EX_IOERR, "spitf(%s): vfprintf", path);
if (fclose(f) < 0)
errorf(EX_IOERR, "spitf(%s): fclose", path);
}
int nftw_rm(const char *path, const struct stat *sb, int type,
struct FTW *ftw) {
if (remove(path) < 0)
errorf(EX_IOERR, "nftw_rm");
return 0;
}
#define LEN(x) sizeof(x) / sizeof(*x)
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s command [arguments...]\n"
"Requires Linux kernel >= 3.19 with CONFIG_USER_NS.\n",
argv[0]);
exit(EX_USAGE);
}
char tmpl[] = "/tmp/chrootenvXXXXXX";
char *root = mkdtemp(tmpl);
if (root == NULL)
errorf(EX_IOERR, "mkdtemp");
// Don't make root private so that privilege drops inside chroot are possible:
if (chmod(root, 0755) < 0)
errorf(EX_IOERR, "chmod");
pid_t cpid = fork();
if (cpid < 0)
errorf(EX_OSERR, "fork");
if (cpid == 0) {
uid_t uid = getuid();
gid_t gid = getgid();
// If we are root, no need to create new user namespace.
if (uid == 0) {
if (unshare(CLONE_NEWNS) < 0)
errorf(EX_OSERR, "unshare() failed: You may have an old kernel or have CLONE_NEWUSER disabled by your distribution security settings.");
// Mark all mounted filesystems as slave so changes
// don't propagate to the parent mount namespace.
if (mount(NULL, "/", NULL, MS_REC | MS_SLAVE, NULL) < 0)
errorf(EX_OSERR, "mount");
} else {
// Create new mount and user namespaces. CLONE_NEWUSER
// requires a program to be non-threaded.
if (unshare(CLONE_NEWNS | CLONE_NEWUSER) < 0)
errorf(EX_OSERR, "unshare");
// Map users and groups to the parent namespace.
// setgroups is only available since Linux 3.19:
spitf("/proc/self/setgroups", "deny");
spitf("/proc/self/uid_map", "%d %d 1", uid, uid);
spitf("/proc/self/gid_map", "%d %d 1", gid, gid);
}
if (chdir(root) < 0)
errorf(EX_IOERR, "chdir");
for (size_t i = 0; i < LEN(binds); i++)
bind(&binds[i]);
if (chroot(root) < 0)
errorf(EX_OSERR, "chroot");
if (chdir("/") < 0)
errorf(EX_OSERR, "chdir");
argv++;
if (execvpe(*argv, argv, env_build(env_whitelist, LEN(env_whitelist))) < 0)
errorf(EX_OSERR, "execvpe");
}
int status;
if (waitpid(cpid, &status, 0) < 0)
errorf(EX_OSERR, "waitpid");
if (nftw(root, nftw_rm, getdtablesize(), FTW_DEPTH | FTW_MOUNT | FTW_PHYS) < 0)
errorf(EX_IOERR, "nftw");
if (WIFEXITED(status))
return WEXITSTATUS(status);
else if (WIFSIGNALED(status))
kill(getpid(), WTERMSIG(status));
return EX_OSERR;
}
|
the_stack_data/178266044.c | #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
enum Op_type{
generate_type,
shuffle_type,
sort_type
};
enum In_type{
sys,
lib
};
struct Input_set{
enum Op_type op_type;
enum In_type in_type;
char * filename;
int records_number;
int records_width;
};
int sort(char *filename, int number, int width, enum In_type type);
int shuffle(char *filename, int number, int width, enum In_type type);
int parse_input(struct Input_set* input_set, int argc, char*argv[]){
int i = 1;
if(argc == i){
printf("Brak %d argumentu!(typ operacji)\n",i);
return 1;
}
else{
if(strcmp(argv[i],"generate") == 0){
input_set->op_type = generate_type;
}
else if(strcmp(argv[i],"shuffle") == 0){
input_set->op_type = shuffle_type;
}
else if(strcmp(argv[i],"sort") == 0){
input_set->op_type = sort_type;
}
else{
printf(" %d argumentem powinien byc typ operacji generate/shuffle/sort\n", i);
return 1;
}
i++;
}
if(input_set->op_type != generate_type){
if(argc == i){
printf("Brak %d argumentu!(typ funkcji)\n",i);
return 1;
}
if(strcmp(argv[i],"sys") == 0){
input_set->in_type = sys;
}
else if(strcmp(argv[i],"lib") == 0){
input_set->in_type = lib;
}
else{
printf("%d argumentem powinien byc typ funkcji sys/lib\n",i);
return 1;
}
i++;
}
if(argc == i){
printf("Brak %d argumentu(sciezka do pliku)!\n",i);
return 1;
}
else{
input_set->filename = argv[i];
i++;
}
if(argc == i){
printf("Brak %d argumentu(wielkosc rekordu)!\n",i);
return 1;
}
else{
input_set->records_width = atoi(argv[i]);
if(input_set->records_width <= 0){
printf("Wartosc %d argumentu musi byc wieksza od zera\n",i);
return 1;
}
i++;
}
if(argc == i){
printf("Brak %d argumentu(ilosc rekordow)!\n",i);
return 1;
}
else{
input_set->records_number = atoi(argv[i]);
if(input_set->records_number <= 0){
printf("Wartosc %d argumentu musi byc wieksza od zera\n",i);
return 1;
}
i++;
}
return 0;
}
int generate(char * filename,int records_number,int records_width){
int randomData = open("/dev/urandom", O_RDONLY);
if(randomData == -1){
perror("/dev/random");
return 1;
}
int out = open(filename,O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
if(out == -1){
close(randomData);
perror(filename);
return 1;
}
char *myRandomData = calloc(records_width,sizeof (char));
for(int i = 0; i < records_number; i++){
ssize_t result = read(randomData, myRandomData, records_width);
if (result < 0)
{
close(randomData);
close(out);
free(myRandomData);
perror("/dev/random");
return 1;
}
result = write(out,myRandomData,records_width);
if (result < 0)
{
close(randomData);
close(out);
free(myRandomData);
perror(filename);
return 1;
}
}
close(randomData);
close(out);
free(myRandomData);
return 0;
}
int sys_sort(char * filename,int records_number,int records_width) {
int file = open(filename,O_RDWR);
if(file == -1){
perror(filename);
return 1;
}
unsigned char *row_a = calloc(records_width,sizeof (unsigned char));
unsigned char *row_b = calloc(records_width,sizeof (unsigned char));
int n = records_number;
do{
for(int i = 0; i<n-1;i++){
lseek(file,i*records_width,SEEK_SET);
ssize_t result = read(file, row_a, records_width);
if (result < 0)
{
perror(filename);
close(file);
free(row_a);
free(row_b);
return 1;
}
result = read(file, row_b, records_width);
if (result < 0)
{
perror(filename);
close(file);
free(row_a);
free(row_b);
return 1;
}
if(row_a[0] > row_b[0]){
lseek(file,i*records_width,SEEK_SET);
result = write(file, row_b, records_width);
if (result < 0)
{
perror(filename);
close(file);
free(row_a);
free(row_b);
return 1;
}
result = write(file, row_a, records_width);
if (result < 0)
{
perror(filename);
close(file);
free(row_a);
free(row_b);
return 1;
}
}
}
n--;
} while (n > 1);
close(file);
free(row_a);
free(row_b);
return 0;
}
int sys_shuffle(char * filename,int records_number,int records_width){
time_t tt;
srand((unsigned int) time(&tt));
int file = open(filename,O_RDWR);
if(file == -1){
perror(filename);
return 1;
}
unsigned char *row_a = calloc(records_width,sizeof (unsigned char));
unsigned char *row_b = calloc(records_width,sizeof (unsigned char));
for(int i = 0; i<records_number-1;i++){
int j = rand()%(records_number-i)+i;
if(i!=j){
lseek(file,i*records_width,SEEK_SET);
ssize_t result = read(file, row_a, records_width);
if (result < 0)
{
perror(filename);
close(file);
free(row_a);
free(row_b);
return 1;
}
lseek(file,j*records_width,SEEK_SET);
result = read(file, row_b, records_width);
if (result < 0)
{
perror(filename);
close(file);
free(row_a);
free(row_b);
return 1;
}
lseek(file,i*records_width,SEEK_SET);
result = write(file, row_b, records_width);
if (result < 0)
{
perror(filename);
close(file);
free(row_a);
free(row_b);
return 1;
}
lseek(file,j*records_width,SEEK_SET);
result = write(file, row_a, records_width);
if (result < 0)
{
perror(filename);
close(file);
free(row_a);
free(row_b);
return 1;
}
}
}
close(file);
free(row_a);
free(row_b);
return 0;
}
int lib_sort(char * filename,int records_number,int records_width) {
FILE* file = fopen(filename,"r+");
if(file == NULL){
perror(filename);
return 1;
}
unsigned char *row_a = calloc(records_width,sizeof (unsigned char));
unsigned char *row_b = calloc(records_width,sizeof (unsigned char));
int n = records_number;
do{
for(int i = 0; i<n-1;i++){
fseek(file,i*records_width,SEEK_SET);
ssize_t result = fread(row_a,1,records_width,file);
if (result < 0)
{
perror(filename);
fclose(file);
free(row_a);
free(row_b);
return 1;
}
result = fread(row_b,1,records_width,file);
if (result < 0)
{
perror(filename);
fclose(file);
free(row_a);
free(row_b);
return 1;
}
if(row_a[0] > row_b[0]){
fseek(file,i*records_width,SEEK_SET);
result = fwrite(row_b,1,records_width,file);
if (result < 0)
{
perror(filename);
fclose(file);
free(row_a);
free(row_b);
return 1;
}
result = fwrite(row_a,1,records_width,file);
if (result < 0)
{
perror(filename);
fclose(file);
free(row_a);
free(row_b);
return 1;
}
fflush(file);
}
}
n--;
} while (n > 1);
fclose(file);
free(row_a);
free(row_b);
return 0;
}
int lib_shuffle(char * filename,int records_number,int records_width){
time_t tt;
srand((unsigned int) time(&tt));
FILE* file = fopen(filename,"r+");
if(file == NULL){
perror(filename);
return 1;
}
unsigned char *row_a = calloc(records_width,sizeof (unsigned char));
unsigned char *row_b = calloc(records_width,sizeof (unsigned char));
for(int i = 0; i<records_number-1;i++){
int j = rand()%(records_number-i)+i;
if(i!=j){
fseek(file,i*records_width,SEEK_SET);
ssize_t result = fread(row_a,1,records_width,file);
if (result < 0)
{
perror(filename);
fclose(file);
free(row_a);
free(row_b);
return 1;
}
fseek(file,j*records_width,SEEK_SET);
result = fread(row_b,1,records_width,file);
if (result < 0)
{
perror(filename);
fclose(file);
free(row_a);
free(row_b);
return 1;
}
fseek(file,i*records_width,SEEK_SET);
result = fwrite(row_b,1,records_width,file);
if (result < 0)
{
perror(filename);
fclose(file);
free(row_a);
free(row_b);
return 1;
}
fseek(file,j*records_width,SEEK_SET);
result = fwrite(row_a,1,records_width,file);
if (result < 0)
{
perror(filename);
fclose(file);
free(row_a);
free(row_b);
return 1;
}
fflush(file);
}
}
fclose(file);
free(row_a);
free(row_b);
return 0;
}
int shuffle(char *filename, int number, int width, enum In_type type) {
if(type == sys) return sys_shuffle(filename,number,width);
else return lib_shuffle(filename,number,width);
}
int sort(char *filename, int number, int width, enum In_type type) {
if(type == sys) return sys_sort(filename,number,width);
else return lib_sort(filename,number,width);
}
int main(int argc,char * argv[]){
struct Input_set* input_set = malloc(sizeof(struct Input_set));
int result = parse_input(input_set,argc,argv);
if(result == 0){
switch (input_set->op_type){
case generate_type:
result = generate(input_set->filename,input_set->records_number,input_set->records_width);
break;
case sort_type:
result = sort(input_set->filename,input_set->records_number,input_set->records_width,input_set->in_type);
break;
case shuffle_type:
result = shuffle(input_set->filename,input_set->records_number,input_set->records_width,input_set->in_type);
break;
}
}
free(input_set);
return result;
} |
the_stack_data/184517064.c | #include<stdio.h>
void LOG(char* log)
{
// 生成Log文件
FILE* fp = fopen("Log.txt","a+");
fprintf(fp, "%s\n",log);
fclose(fp);
}
|
the_stack_data/31387313.c | /* AND
0 0 | 0
0 1 | 0
1 0 | 0
1 1 | 1
OR
0 0 | 0
0 1 | 1
1 0 | 1
1 1 | 1
XOR
0 0 | 0
0 1 | 1
1 0 | 1
1 1 | 0
<< <number> == <number> * 2 ** k
>> <number> == <number> // 2 ** k (This is a power)
*/
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#define SWAP(a,b) a^=b; b^=a; a^=b; // multiple statements require semi-colons
// Function protypes tell the compiler what they return
bool getBit(int num, int i);
void checkAllBits(int num);
void returnBinaryRep(int num);
void convertToNum(char *arr, int sizee);
void swapBits(uint8_t* b);
int main(){
int a = 0x04;
a = a >> 2;
printf("value of a: %X [%x]\n",a,a);
bool bitt = getBit(0xff, 7);
printf(bitt ? "True\n" : "False\n");
checkAllBits(0xfe);
returnBinaryRep(0xfe);
char arr[8] = {'1', '1', '1', '1','1', '1', '1', '1'};
convertToNum(arr, 8);
uint8_t mypointer = 100;
uint8_t *t = &mypointer;
swapBits(t);
return 0;
}
// Checks to see if a bit is set.
// 0 == LSB and 7 == MSB
bool getBit(int num, int i){
return ((num & (1 << i)) != 0);
}
// Checks all the bits that are set in a byte
void checkAllBits(int num) {
printf("Checking all the bits in a byte\n");
for(int i=0; i<8; i++){
if ((num & (1 << i)) != 0){
printf("Bit %d is set \n", i);
}
}
}
// Prints out the binary representation of a byte
void returnBinaryRep(int num){
printf("Returning the Binary representation of %d\n", num);
for(int i=7; i>=0; i--){
if ((num & (1 << i)) != 0){
printf("1");
}
else {
printf("0");
}
}
printf("\n");
}
// Convert a binary string to number
void convertToNum(char *arr, int sizee){
double summ = 0;
for (int i = 0; i < sizee; i++)
{
// printf("%c %f\n", arr[i], pow(2, sizee - 1 - i));
if (arr[i] != '0'){
summ += pow(2, sizee - 1 - i);
}
}
printf("Sum = %f\n", summ);
}
/**
* Swap 2 nibbles in a byte
* (e.g) 0xCF -> Answer: 0xFC
*/
void swapBits(uint8_t* b)
{
uint8_t *x;
// *x = (*b & 0x0F); // Grab the last nibble (0x0F) and move it to be the MSB
// *y = (*b & 0xF0); // Grab the first nibble (0xC0) and move it to the LSB
*b = ((*b & 0x0F) << 4 | (*b & 0xF0) >> 4);
printf("%d\n", *b);
} |
the_stack_data/248581696.c | #include <stdio.h>
int main(int argc, char **argv){
printf("argc = %d\n", argc);
for(int i = 0; i < argc; i++){
printf("argv[%d] = %s\n", i, *(argv + i));
}
} |
the_stack_data/86074369.c | #include <stdio.h>
void salaryhike(int *var, int b) {
*var = *var+b;
}
int main() {
int salary=0, bonus=0;
printf("Enter the employee current salary:");
scanf("%d", &salary);
printf("Enter bonus:");
scanf("%d", &bonus);
salaryhike(&salary, bonus);
printf("Final salary: %d", salary);
return 0;
}
|
the_stack_data/26701214.c | #include<stdio.h>
#include<stdlib.h>
#define size 100
void main () {
int burst[size] = {0};
int process[size] = {0};
int wait[size] = {0};
//int arrival[size] = {0};
int turnaround[size] = {0};
int x = 0;
int i = 0;
int j = 0;
int sum = 0;
float avg = 0.0, avg2 = 0.0;
printf ("\nEnter size: ");
scanf ("%d", &x);
for (i = 1; i < x+1; ++i) {
printf ("\nEnter Burst Time: ");
scanf ("%d", &burst[i]);
process[i] = i;
}
for (i = 1; i < x+1; ++i) {
printf ("\nBurst Time for Process#%d : %d", i, burst[i]);
}
printf("\n");
int temp = 0, temp2 = 0;
for (i = 1; i < x; ++i) {
for (j = 1; j < x-i; ++j) {
if (burst[j] > burst[j+1]) {
temp = burst[j];
temp2 = process[j];
burst[j] = burst[j+1];
process[j] = burst[j+1];
burst[j+1] = temp;
process[j+1] = temp2;
}
}
}
for (i = 1; i < x+1; ++i) {
sum = 0;
avg = 0.0, avg2 = 0.0;
for (int j = 0; j < i; ++j) {
sum += burst[j];
}
wait[i] = sum;
turnaround[i] = burst[i] + wait[i];
avg += (float) wait[i];
avg2 += (float) turnaround[i];
printf("\nWait Time of P%d = %d\tTurnaround Time of P%d = %d", process[i], wait[i], process[i], turnaround[i]);
}
printf("\n");
printf("\nAverage Wait Time = %f and Average Turnaround Time = %f\n", (avg/x), (avg2/x));
}
|
the_stack_data/117328767.c | #include <stdio.h>
#include <string.h>
int check (char str[], char sub[], int len, int n) {
int flag = 1, i;
for (i = 1; i < len - 1; i++)
if (str[n + i] != sub[i] ) {
flag = 0;
break;
}
return flag;
}
void main () {
char str[30], sub[30];
int mod[30], n, len, x = 0, flag, key;
//fgets (str, 30, stdin);
//fgets (sub, 30, stdin);
scanf ("%s", str);
scanf ("%s", sub);
len = strlen (sub);
for (n = 0; str[n] != '\0'; n++) {
if ( str[n] == sub[0] )
if ( check(str, sub, len, n) == 1)
for (int i = 0; i < len - 1; i++) {
key = n + i;
flag = 0;
for (int j = 0; j < x; j++) {
if (mod[j] == key)
flag = 1;
}
if (flag == 0)
mod[x++] = key;
}
}
n = 0;
for (int i = 0; i < x; i++) {
key = mod[i] - n;
for (int i = key; str[i] != '\0'; i++)
str[i] = str[i + 1];
n++;
}
printf ("%s", str);
}
|
the_stack_data/165768250.c | /*
* Author: Jean-Marc Lienher ( http://oksid.ch )
* Copyright 2000-2003 by O'ksi'D.
*
* This library is free software. Distribution and use rights are outlined in
* the file "COPYING" which should have been included with this file. If this
* file is missing or damaged, see the license at:
*
* https://www.fltk.org/COPYING.php
*
* Please see the following page on how to report bugs and issues:
*
* https://www.fltk.org/bugs.php
*/
/*
* generate the "if(){} else if ..." structure of ucs2fontmap()
*/
#include <wchar.h>
#include <stdio.h>
char buffer[1000000];
int main(int argc, char **argv) {
char buf[80];
int len;
char *encode[256];
int encode_number = 0;
unsigned int i = 0;
unsigned char *ptr;
unsigned char *lst = "";
size_t nb;
int nbb = 0;
len = fread(buffer, 1, 1000000, stdin);
puts(" ");
puts(" /*************** conv_gen.c ************/");
buffer[len] = '\0';
ptr = buffer;
printf("const int ucs2fontmap"
"(char *s, unsigned int ucs, int enc)\n");
printf("{\n");
printf(" switch(enc) {\n");
printf(" case 0:\n");
printf(" s[0] = (char) ((ucs & 0xFF00) >> 8);\n");
printf(" s[1] = (char) (ucs & 0xFF);\n");
printf(" return 0;");
while (len > 0) {
unsigned char *p = ptr;
unsigned char *f, *t;
while (*p != ']') {
i++;
p++;
}
*(p - 1) = '\0';
*(p - 6) = '\0';
f = p - 5;
while (*p != '+') { i++; p++;}
p++;
t = p;
*(p + 4) = '\0';
if (strcmp(lst, ptr)) {
encode_number++;
encode[encode_number] = ptr;
printf("\n break;");
printf("\n case %d:\n", encode_number);
printf(" ");
} else {
printf(" else ");
}
lst = ptr;
printf("if (ucs <= 0x%s) {\n", t);
printf(" if (ucs >= 0x%s) {\n", f);
if (*(f - 3) == '2') {
printf(" int i = (ucs - 0x%s) * 2;\n", f);
printf(" s[0] = %s_%s[i++];\n", ptr, f, f);
printf(" s[1] = %s_%s[i];\n", ptr, f, f);
printf(" if (s[0] || s[1]) return %d;\n", encode_number);
} else {
printf(" s[0] = 0;\n");
printf(" s[1] = %s_%s[ucs - 0x%s];\n", ptr, f, f);
printf(" if (s[1]) return %d;\n", encode_number);
}
printf(" }\n");
printf(" }");
while (*ptr != '\n') {
ptr++;
len--;
}
ptr++;
len--;
}
printf("\n break;\n");
printf("\n default:\n");
printf(" break;\n");
printf(" };\n");
printf(" return -1;\n");
printf("};\n\n");
printf("const int encoding_number(const char *enc)\n{\n");
printf(" if (!enc || !strcmp(enc, \"iso10646-1\")) {\n");
printf(" return 0;\n");
i = 1;
while (i <= encode_number) {
int l;
char *ptr;
l = strlen(encode[i]) - 3;
ptr = encode[i] + l;
*(ptr) = '\0';
ptr--;
while (ptr != encode[i]) {
if (*ptr == '_') {
*ptr = '-';
ptr--;
break;
}
ptr--;
}
while (ptr != encode[i]) {
if (*ptr == '_') {
*ptr = '.';
}
ptr--;
}
printf(" } else if (!strcmp(enc, \"%s\")", encode[i] +11);
if (!strcmp(encode[i] + 11, "big5-0")) {
printf(" || !strcmp(enc, \"big5.eten-0\")");
} else if (!strcmp(encode[i] + 11, "dingbats")) {
printf(" || !strcmp(enc, \"zapfdingbats\")");
printf(" || !strcmp(enc, \"zapf dingbats\")");
printf(" || !strcmp(enc, \"itc zapf dingbats\")");
} else if (!strcmp(encode[i] + 11, "jisx0208.1983-0")) {
printf(" || !strcmp(enc, \"jisx0208.1990-0\")");
}
printf(") {\n");
printf(" return %d;\n", i);
i++;
}
printf(" };\n");
printf(" return -1;\n");
printf("};\n\n");
printf("/*\n");
printf("const char *encoding_name(int num)\n{\n");
printf(" switch (num) {\n");
i = 1;
while (i <= encode_number) {
printf(" case %d:\n", i);
printf(" return \"%s\";\n", encode[i] + 11);
i++;
}
printf(" };\n");
printf(" return \"iso10646-1\";\n");
printf("};\n\n");
printf("*/\n");
return 0;
}
|
the_stack_data/144202.c | #line 185 "/home/zhy/work/test/splash2-master/codes/null_macros/c.m4.null.POSIX_BARRIER"
#line 1 "lu.C"
/*************************************************************************/
/* */
/* Copyright (c) 1994 Stanford University */
/* */
/* All rights reserved. */
/* */
/* Permission is given to use, copy, and modify this software for any */
/* non-commercial purpose as long as this copyright notice is not */
/* removed. All other uses, including redistribution in whole or in */
/* part, are forbidden without prior written permission. */
/* */
/* This software is provided with absolutely no warranty and no */
/* support. */
/* */
/*************************************************************************/
/*************************************************************************/
/* */
/* Parallel dense blocked LU factorization (no pivoting) */
/* */
/* This version contains two dimensional arrays in which the first */
/* dimension is the block to be operated on, and the second contains */
/* all data points in that block. In this manner, all data points in */
/* a block (which are operated on by the same processor) are allocated */
/* contiguously and locally, and false sharing is eliminated. */
/* */
/* Command line options: */
/* */
/* -nN : Decompose NxN matrix. */
/* -pP : P = number of processors. */
/* -bB : Use a block size of B. BxB elements should fit in cache for */
/* good performance. Small block sizes (B=8, B=16) work well. */
/* -s : Print individual processor timing statistics. */
/* -t : Test output. */
/* -o : Print out matrix values. */
/* -h : Print out command line options. */
/* */
/* Note: This version works under both the FORK and SPROC models */
/* */
/*************************************************************************/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#line 45
#include <pthread.h>
#line 45
#include <sys/time.h>
#line 45
#include <unistd.h>
#line 45
#include <stdlib.h>
#line 45
#include <malloc.h>
#line 45
#define MAX_THREADS 32
#line 45
pthread_t PThreadTable[MAX_THREADS];
#line 45
// #include "bullmoose.h"
#define MAXRAND 32767.0
#define DEFAULT_N 512
#define DEFAULT_P 4
#define DEFAULT_B 16
#define min(a,b) ((a) < (b) ? (a) : (b))
//#define PAGE_SIZE 4096
#define PAGE_SIZE 1024
struct GlobalMemory {
double *t_in_fac;
double *t_in_solve;
double *t_in_mod;
double *t_in_bar;
double *completion;
unsigned long starttime;
unsigned long rf;
unsigned long rs;
unsigned long done;
long id;
#line 66
pthread_barrier_t (start);
#line 66
pthread_mutex_t (idlock);
} *Global;
struct LocalCopies {
double t_in_fac;
double t_in_solve;
double t_in_mod;
double t_in_bar;
};
long n = DEFAULT_N; /* The size of the matrix */
long P = DEFAULT_P; /* Number of processors */
long block_size = DEFAULT_B; /* Block dimension */
long nblocks; /* Number of blocks in each dimension */
long num_rows; /* Number of processors per row of processor grid */
long num_cols; /* Number of processors per col of processor grid */
double **a; /* a = lu; l and u both placed back in a */
double *rhs;
long *proc_bytes; /* Bytes to malloc per processor to hold blocks of A*/
double **last_malloc; /* Starting point of last block of A */
long test_result = 0; /* Test result of factorization? */
long doprint = 0; /* Print out matrix values? */
long dostats = 0; /* Print out individual processor statistics? */
void SlaveStart(void);
void OneSolve(long n, long block_size, long MyNum, long dostats);
void lu0(double *a, long n, long stride);
void bdiv(double *a, double *diag, long stride_a, long stride_diag, long dimi, long dimk);
void bmodd(double *a, double *c, long dimi, long dimj, long stride_a, long stride_c);
void bmod(double *a, double *b, double *c, long dimi, long dimj, long dimk, long stridea, long strideb, long stridec);
void daxpy(double *a, double *b, long n, double alpha);
long BlockOwner(long I, long J);
long BlockOwnerColumn(long I, long J);
long BlockOwnerRow(long I, long J);
void lu(long n, long bs, long MyNum, struct LocalCopies *lc, long dostats);
void InitA(double *rhs);
double TouchA(long bs, long MyNum);
void PrintA(void);
void CheckResult(long n, double **a, double *rhs);
void printerr(char *s);
void srand48(long int seedval);
double drand48(void);
#define MNWZ 0x100000000
#define ANWZ 0x5DEECE66D
#define CNWZ 0xB16
static unsigned long long seed = 1;
double drand48(void)
{
seed = (ANWZ * seed + CNWZ) & 0xFFFFFFFFFFFFLL;
unsigned int x = seed >> 16;
return ((double)x / (double)MNWZ);
}
//static unsigned long long seed = 1;
void srand48(long int seedval)
{
seed = (((long long int)seedval) << 16) | rand();
}
int main(int argc, char *argv[])
{
long i, j;
long ch;
extern char *optarg;
double mint, maxt, avgt;
double min_fac, min_solve, min_mod, min_bar;
double max_fac, max_solve, max_mod, max_bar;
double avg_fac, avg_solve, avg_mod, avg_bar;
long proc_num;
long edge;
long size;
unsigned long start;
{
#line 123
struct timeval FullTime;
#line 123
// malicious_start();
#line 123
gettimeofday(&FullTime, NULL);
#line 123
(start) = (unsigned long)(FullTime.tv_usec + FullTime.tv_sec * 1000000);
#line 123
}
while ((ch = getopt(argc, argv, "n:p:b:cstoh")) != -1) {
switch(ch) {
case 'n': n = atoi(optarg); break;
case 'p': P = atoi(optarg); break;
case 'b': block_size = atoi(optarg); break;
case 's': dostats = 1; break;
case 't': test_result = !test_result; break;
case 'o': doprint = !doprint; break;
case 'h': printf("Usage: LU <options>\n\n");
printf("options:\n");
printf(" -nN : Decompose NxN matrix.\n");
printf(" -pP : P = number of processors.\n");
printf(" -bB : Use a block size of B. BxB elements should fit in cache for \n");
printf(" good performance. Small block sizes (B=8, B=16) work well.\n");
printf(" -c : Copy non-locally allocated blocks to local memory before use.\n");
printf(" -s : Print individual processor timing statistics.\n");
printf(" -t : Test output.\n");
printf(" -o : Print out matrix values.\n");
printf(" -h : Print out command line options.\n\n");
printf("Default: LU -n%1d -p%1d -b%1d\n",
DEFAULT_N,DEFAULT_P,DEFAULT_B);
exit(0);
break;
}
}
{;}
printf("\n");
printf("Blocked Dense LU Factorization\n");
printf(" %ld by %ld Matrix\n",n,n);
printf(" %ld Processors\n",P);
printf(" %ld by %ld Element Blocks\n",block_size,block_size);
printf("\n");
printf("\n");
num_rows = (long) sqrt((double) P);
for (;;) {
num_cols = P/num_rows;
if (num_rows*num_cols == P)
break;
num_rows--;
}
nblocks = n/block_size;
if (block_size * nblocks != n) {
nblocks++;
}
edge = n%block_size;
if (edge == 0) {
edge = block_size;
}
proc_bytes = (long *) malloc(P*sizeof(long));
if (proc_bytes == NULL) {
fprintf(stderr,"Could not malloc memory for proc_bytes.\n");
exit(-1);
}
last_malloc = (double **) malloc(P*sizeof(double *));;
if (last_malloc == NULL) {
fprintf(stderr,"Could not malloc memory for last_malloc.\n");
exit(-1);
}
for (i=0;i<P;i++) {
proc_bytes[i] = 0;
last_malloc[i] = NULL;
}
for (i=0;i<nblocks;i++) {
for (j=0;j<nblocks;j++) {
proc_num = BlockOwner(i,j);
if ((i == nblocks-1) && (j == nblocks-1)) {
size = edge*edge;
} else if ((i == nblocks-1) || (j == nblocks-1)) {
size = edge*block_size;
} else {
size = block_size*block_size;
}
proc_bytes[proc_num] += size*sizeof(double);
}
}
for (i=0;i<P;i++) {
last_malloc[i] = (double *) malloc(proc_bytes[i] + PAGE_SIZE);
if (last_malloc[i] == NULL) {
fprintf(stderr,"Could not malloc memory blocks for proc %ld\n",i);
exit(-1);
}
last_malloc[i] = (double *) (((unsigned long) last_malloc[i]) + PAGE_SIZE -
((unsigned long) last_malloc[i]) % PAGE_SIZE);
/* Note that this causes all blocks to start out page-aligned, and that
for block sizes that exceed cache line size, blocks start at cache-line
aligned addresses as well. This reduces false sharing */
}
a = (double **) malloc(nblocks*nblocks*sizeof(double *));;
if (a == NULL) {
printerr("Could not malloc memory for a\n");
exit(-1);
}
for (i=0;i<nblocks;i++) {
for (j=0;j<nblocks;j++) {
proc_num = BlockOwner(i,j);
a[i+j*nblocks] = last_malloc[proc_num];
if ((i == nblocks-1) && (j == nblocks-1)) {
size = edge*edge;
} else if ((i == nblocks-1) || (j == nblocks-1)) {
size = edge*block_size;
} else {
size = block_size*block_size;
}
last_malloc[proc_num] += size;
}
}
rhs = (double *) malloc(n*sizeof(double));;
if (rhs == NULL) {
printerr("Could not malloc memory for rhs\n");
exit(-1);
}
Global = (struct GlobalMemory *) malloc(sizeof(struct GlobalMemory));;
Global->t_in_fac = (double *) malloc(P*sizeof(double));;
Global->t_in_mod = (double *) malloc(P*sizeof(double));;
Global->t_in_solve = (double *) malloc(P*sizeof(double));;
Global->t_in_bar = (double *) malloc(P*sizeof(double));;
Global->completion = (double *) malloc(P*sizeof(double));;
if (Global == NULL) {
printerr("Could not malloc memory for Global\n");
exit(-1);
} else if (Global->t_in_fac == NULL) {
printerr("Could not malloc memory for Global->t_in_fac\n");
exit(-1);
} else if (Global->t_in_mod == NULL) {
printerr("Could not malloc memory for Global->t_in_mod\n");
exit(-1);
} else if (Global->t_in_solve == NULL) {
printerr("Could not malloc memory for Global->t_in_solve\n");
exit(-1);
} else if (Global->t_in_bar == NULL) {
printerr("Could not malloc memory for Global->t_in_bar\n");
exit(-1);
} else if (Global->completion == NULL) {
printerr("Could not malloc memory for Global->completion\n");
exit(-1);
}
/* POSSIBLE ENHANCEMENT: Here is where one might distribute the a[i]
blocks across physically distributed memories as desired.
One way to do this is as follows:
for (i=0;i<nblocks;i++) {
for (j=0;j<nblocks;j++) {
proc_num = BlockOwner(i,j);
if ((i == nblocks-1) && (j == nblocks-1)) {
size = edge*edge;
} else if ((i == nblocks-1) || (j == nblocks-1)) {
size = edge*block_size;
} else {
size = block_size*block_size;
}
Place all addresses x such that
(&(a[i+j*nblocks][0]) <= x < &(a[i+j*nblocks][size-1]))
on node proc_num
}
}
*/
{
#line 295
pthread_barrier_init(&(Global->start), NULL, P);
#line 295
};
{pthread_mutex_init(&(Global->idlock), NULL);};
Global->id = 0;
InitA(rhs);
if (doprint) {
printf("Matrix before decomposition:\n");
PrintA();
}
{
#line 305
long i, Error;
#line 305
#line 305
for (i = 0; i < (P) - 1; i++) {
#line 305
Error = pthread_create(&PThreadTable[i], NULL, (void * (*)(void *))(SlaveStart), NULL);
#line 305
if (Error != 0) {
#line 305
printf("Error in pthread_create().\n");
#line 305
exit(-1);
#line 305
}
#line 305
}
#line 305
#line 305
SlaveStart();
#line 305
};
{
#line 306
long i, Error;
#line 306
for (i = 0; i < (P) - 1; i++) {
#line 306
Error = pthread_join(PThreadTable[i], NULL);
#line 306
if (Error != 0) {
#line 306
printf("Error in pthread_join().\n");
#line 306
exit(-1);
#line 306
}
#line 306
}
#line 306
};
if (doprint) {
printf("\nMatrix after decomposition:\n");
PrintA();
}
if (dostats) {
maxt = avgt = mint = Global->completion[0];
for (i=1; i<P; i++) {
if (Global->completion[i] > maxt) {
maxt = Global->completion[i];
}
if (Global->completion[i] < mint) {
mint = Global->completion[i];
}
avgt += Global->completion[i];
}
avgt = avgt / P;
min_fac = max_fac = avg_fac = Global->t_in_fac[0];
min_solve = max_solve = avg_solve = Global->t_in_solve[0];
min_mod = max_mod = avg_mod = Global->t_in_mod[0];
min_bar = max_bar = avg_bar = Global->t_in_bar[0];
for (i=1; i<P; i++) {
if (Global->t_in_fac[i] > max_fac) {
max_fac = Global->t_in_fac[i];
}
if (Global->t_in_fac[i] < min_fac) {
min_fac = Global->t_in_fac[i];
}
if (Global->t_in_solve[i] > max_solve) {
max_solve = Global->t_in_solve[i];
}
if (Global->t_in_solve[i] < min_solve) {
min_solve = Global->t_in_solve[i];
}
if (Global->t_in_mod[i] > max_mod) {
max_mod = Global->t_in_mod[i];
}
if (Global->t_in_mod[i] < min_mod) {
min_mod = Global->t_in_mod[i];
}
if (Global->t_in_bar[i] > max_bar) {
max_bar = Global->t_in_bar[i];
}
if (Global->t_in_bar[i] < min_bar) {
min_bar = Global->t_in_bar[i];
}
avg_fac += Global->t_in_fac[i];
avg_solve += Global->t_in_solve[i];
avg_mod += Global->t_in_mod[i];
avg_bar += Global->t_in_bar[i];
}
avg_fac = avg_fac/P;
avg_solve = avg_solve/P;
avg_mod = avg_mod/P;
avg_bar = avg_bar/P;
}
printf(" PROCESS STATISTICS\n");
printf(" Total Diagonal Perimeter Interior Barrier\n");
printf(" Proc Time Time Time Time Time\n");
printf(" 0 %10.0f %10.0f %10.0f %10.0f %10.0f\n",
Global->completion[0],Global->t_in_fac[0],
Global->t_in_solve[0],Global->t_in_mod[0],
Global->t_in_bar[0]);
if (dostats) {
for (i=1; i<P; i++) {
printf(" %3ld %10.0f %10.0f %10.0f %10.0f %10.0f\n",
i,Global->completion[i],Global->t_in_fac[i],
Global->t_in_solve[i],Global->t_in_mod[i],
Global->t_in_bar[i]);
}
printf(" Avg %10.0f %10.0f %10.0f %10.0f %10.0f\n",
avgt,avg_fac,avg_solve,avg_mod,avg_bar);
printf(" Min %10.0f %10.0f %10.0f %10.0f %10.0f\n",
mint,min_fac,min_solve,min_mod,min_bar);
printf(" Max %10.0f %10.0f %10.0f %10.0f %10.0f\n",
maxt,max_fac,max_solve,max_mod,max_bar);
}
printf("\n");
Global->starttime = start;
printf(" TIMING INFORMATION\n");
printf("Start time : %16lu\n", Global->starttime);
printf("Initialization finish time : %16lu\n", Global->rs);
printf("Overall finish time : %16lu\n", Global->rf);
printf("Total time with initialization : %16lu\n", Global->rf-Global->starttime);
printf("Total time without initialization : %16lu\n", Global->rf-Global->rs);
printf("\n");
if (test_result) {
printf(" TESTING RESULTS\n");
CheckResult(n, a, rhs);
}
// malicious_end();
{exit(0);};
}
void SlaveStart()
{
long MyNum;
{pthread_mutex_lock(&(Global->idlock));}
MyNum = Global->id;
Global->id ++;
{pthread_mutex_unlock(&(Global->idlock));}
/* POSSIBLE ENHANCEMENT: Here is where one might pin processes to
processors to avoid migration */
{;};
OneSolve(n, block_size, MyNum, dostats);
}
void OneSolve(long n, long block_size, long MyNum, long dostats)
{
unsigned long myrs;
unsigned long myrf;
unsigned long mydone;
struct LocalCopies *lc;
lc = (struct LocalCopies *) malloc(sizeof(struct LocalCopies));
if (lc == NULL) {
fprintf(stderr,"Proc %ld could not malloc memory for lc\n",MyNum);
exit(-1);
}
lc->t_in_fac = 0.0;
lc->t_in_solve = 0.0;
lc->t_in_mod = 0.0;
lc->t_in_bar = 0.0;
/* barrier to ensure all initialization is done */
{
#line 441
pthread_barrier_wait(&(Global->start));
#line 441
};
malicious_1();
malicious_2();
malicious_3();
malicious_4();
/* to remove cold-start misses, all processors touch their own data */
TouchA(block_size, MyNum);
{
#line 446
pthread_barrier_wait(&(Global->start));
#line 446
};
/* POSSIBLE ENHANCEMENT: Here is where one might reset the
statistics that one is measuring about the parallel execution */
if ((MyNum == 0) || (dostats)) {
{
#line 452
struct timeval FullTime;
#line 452
#line 452
gettimeofday(&FullTime, NULL);
#line 452
(myrs) = (unsigned long)(FullTime.tv_usec + FullTime.tv_sec * 1000000);
#line 452
};
}
lu(n, block_size, MyNum, lc, dostats);
if ((MyNum == 0) || (dostats)) {
{
#line 458
struct timeval FullTime;
#line 458
#line 458
gettimeofday(&FullTime, NULL);
#line 458
(mydone) = (unsigned long)(FullTime.tv_usec + FullTime.tv_sec * 1000000);
#line 458
};
}
{
#line 461
pthread_barrier_wait(&(Global->start));
#line 461
};
if ((MyNum == 0) || (dostats)) {
Global->t_in_fac[MyNum] = lc->t_in_fac;
Global->t_in_solve[MyNum] = lc->t_in_solve;
Global->t_in_mod[MyNum] = lc->t_in_mod;
Global->t_in_bar[MyNum] = lc->t_in_bar;
Global->completion[MyNum] = mydone-myrs;
}
if (MyNum == 0) {
{
#line 471
struct timeval FullTime;
#line 471
#line 471
gettimeofday(&FullTime, NULL);
#line 471
(myrf) = (unsigned long)(FullTime.tv_usec + FullTime.tv_sec * 1000000);
#line 471
};
Global->rs = myrs;
Global->done = mydone;
Global->rf = myrf;
}
}
void lu0(double *a, long n, long stride)
{
long j;
long k;
long length;
double alpha;
for (k=0; k<n; k++) {
/* modify subsequent columns */
for (j=k+1; j<n; j++) {
a[k+j*stride] /= a[k+k*stride];
alpha = -a[k+j*stride];
length = n-k-1;
daxpy(&a[k+1+j*stride], &a[k+1+k*stride], n-k-1, alpha);
}
}
}
void bdiv(double *a, double *diag, long stride_a, long stride_diag, long dimi, long dimk)
{
long j;
long k;
double alpha;
for (k=0; k<dimk; k++) {
for (j=k+1; j<dimk; j++) {
alpha = -diag[k+j*stride_diag];
daxpy(&a[j*stride_a], &a[k*stride_a], dimi, alpha);
}
}
}
void bmodd(double *a, double *c, long dimi, long dimj, long stride_a, long stride_c)
{
long j;
long k;
long length;
double alpha;
for (k=0; k<dimi; k++) {
for (j=0; j<dimj; j++) {
c[k+j*stride_c] /= a[k+k*stride_a];
alpha = -c[k+j*stride_c];
length = dimi - k - 1;
daxpy(&c[k+1+j*stride_c], &a[k+1+k*stride_a], dimi-k-1, alpha);
}
}
}
void bmod(double *a, double *b, double *c, long dimi, long dimj, long dimk, long stridea, long strideb, long stridec)
{
long j;
long k;
double alpha;
for (k=0; k<dimk; k++) {
for (j=0; j<dimj; j++) {
alpha = -b[k+j*strideb];
daxpy(&c[j*stridec], &a[k*stridea], dimi, alpha);
}
}
}
void daxpy(double *a, double *b, long n, double alpha)
{
long i;
for (i=0; i<n; i++) {
a[i] += alpha*b[i];
}
}
long BlockOwner(long I, long J)
{
// return((J%num_cols) + (I%num_rows)*num_cols);
return((I + J) % P);
}
long BlockOwnerColumn(long I, long J)
{
return(I % P);
}
long BlockOwnerRow(long I, long J)
{
return(((J % P) + (P / 2)) % P);
}
void lu(long n, long bs, long MyNum, struct LocalCopies *lc, long dostats)
{
long i, il, j, jl, k, kl;
long I, J, K;
double *A, *B, *C, *D;
long strI, strJ, strK;
unsigned long t1, t2, t3, t4, t11, t22;
for (k=0, K=0; k<n; k+=bs, K++) {
kl = k + bs;
if (kl > n) {
kl = n;
strK = kl - k;
} else {
strK = bs;
}
if ((MyNum == 0) || (dostats)) {
{
#line 590
struct timeval FullTime;
#line 590
#line 590
gettimeofday(&FullTime, NULL);
#line 590
(t1) = (unsigned long)(FullTime.tv_usec + FullTime.tv_sec * 1000000);
#line 590
};
}
/* factor diagonal block */
if (BlockOwner(K, K) == MyNum) {
A = a[K+K*nblocks];
lu0(A, strK, strK);
}
if ((MyNum == 0) || (dostats)) {
{
#line 600
struct timeval FullTime;
#line 600
#line 600
gettimeofday(&FullTime, NULL);
#line 600
(t11) = (unsigned long)(FullTime.tv_usec + FullTime.tv_sec * 1000000);
#line 600
};
}
{
#line 603
pthread_barrier_wait(&(Global->start));
#line 603
};
if ((MyNum == 0) || (dostats)) {
{
#line 606
struct timeval FullTime;
#line 606
#line 606
gettimeofday(&FullTime, NULL);
#line 606
(t2) = (unsigned long)(FullTime.tv_usec + FullTime.tv_sec * 1000000);
#line 606
};
}
/* divide column k by diagonal block */
D = a[K+K*nblocks];
for (i=kl, I=K+1; i<n; i+=bs, I++) {
if (BlockOwnerColumn(I, K) == MyNum) { /* parcel out blocks */
il = i + bs;
if (il > n) {
il = n;
strI = il - i;
} else {
strI = bs;
}
A = a[I+K*nblocks];
bdiv(A, D, strI, strK, strI, strK);
}
}
/* modify row k by diagonal block */
for (j=kl, J=K+1; j<n; j+=bs, J++) {
if (BlockOwnerRow(K, J) == MyNum) { /* parcel out blocks */
jl = j+bs;
if (jl > n) {
jl = n;
strJ = jl - j;
} else {
strJ = bs;
}
A = a[K+J*nblocks];
bmodd(D, A, strK, strJ, strK, strK);
}
}
if ((MyNum == 0) || (dostats)) {
{
#line 640
struct timeval FullTime;
#line 640
#line 640
gettimeofday(&FullTime, NULL);
#line 640
(t22) = (unsigned long)(FullTime.tv_usec + FullTime.tv_sec * 1000000);
#line 640
};
}
{
#line 643
pthread_barrier_wait(&(Global->start));
#line 643
};
if ((MyNum == 0) || (dostats)) {
{
#line 646
struct timeval FullTime;
#line 646
#line 646
gettimeofday(&FullTime, NULL);
#line 646
(t3) = (unsigned long)(FullTime.tv_usec + FullTime.tv_sec * 1000000);
#line 646
};
}
/* modify subsequent block columns */
for (i=kl, I=K+1; i<n; i+=bs, I++) {
il = i+bs;
if (il > n) {
il = n;
strI = il - i;
} else {
strI = bs;
}
A = a[I+K*nblocks];
for (j=kl, J=K+1; j<n; j+=bs, J++) {
jl = j + bs;
if (jl > n) {
jl = n;
strJ= jl - j;
} else {
strJ = bs;
}
if (BlockOwner(I, J) == MyNum) { /* parcel out blocks */
B = a[K+J*nblocks];
C = a[I+J*nblocks];
bmod(A, B, C, strI, strJ, strK, strI, strK, strI);
}
}
}
if ((MyNum == 0) || (dostats)) {
{
#line 676
struct timeval FullTime;
#line 676
#line 676
gettimeofday(&FullTime, NULL);
#line 676
(t4) = (unsigned long)(FullTime.tv_usec + FullTime.tv_sec * 1000000);
#line 676
};
lc->t_in_fac += (t11-t1);
lc->t_in_solve += (t22-t2);
lc->t_in_mod += (t4-t3);
lc->t_in_bar += (t2-t11) + (t3-t22);
}
}
}
void InitA(double *rhs)
{
long i, j;
long ii, jj;
long edge;
long ibs;
long jbs, skip;
srand48((long) 1);
edge = n%block_size;
for (j=0; j<n; j++) {
for (i=0; i<n; i++) {
if ((n - i) <= edge) {
ibs = edge;
ibs = n-edge;
skip = edge;
} else {
ibs = block_size;
skip = block_size;
}
if ((n - j) <= edge) {
jbs = edge;
jbs = n-edge;
} else {
jbs = block_size;
}
ii = (i/block_size) + (j/block_size)*nblocks;
jj = (i%ibs)+(j%jbs)*skip;
a[ii][jj] = ((double) drand48())/MAXRAND;
if (i == j) {
a[ii][jj] *= 10;
}
}
}
for (j=0; j<n; j++) {
rhs[j] = 0.0;
}
for (j=0; j<n; j++) {
for (i=0; i<n; i++) {
if ((n - i) <= edge) {
ibs = edge;
ibs = n-edge;
skip = edge;
} else {
ibs = block_size;
skip = block_size;
}
if ((n - j) <= edge) {
jbs = edge;
jbs = n-edge;
} else {
jbs = block_size;
}
ii = (i/block_size) + (j/block_size)*nblocks;
jj = (i%ibs)+(j%jbs)*skip;
rhs[i] += a[ii][jj];
}
}
}
double TouchA(long bs, long MyNum)
{
long i, j, I, J;
double tot = 0.0;
long ibs;
long jbs;
/* touch my portion of A[] */
for (J=0; J<nblocks; J++) {
for (I=0; I<nblocks; I++) {
if (BlockOwner(I, J) == MyNum) {
if (J == nblocks-1) {
jbs = n%bs;
if (jbs == 0) {
jbs = bs;
}
} else {
jbs = bs;
}
if (I == nblocks-1) {
ibs = n%bs;
if (ibs == 0) {
ibs = bs;
}
} else {
ibs = bs;
}
for (j=0; j<jbs; j++) {
for (i=0; i<ibs; i++) {
tot += a[I+J*nblocks][i+j*ibs];
}
}
}
}
}
return(tot);
}
void PrintA()
{
long i, j;
long ii, jj;
long edge;
long ibs, jbs, skip;
edge = n%block_size;
for (i=0; i<n; i++) {
for (j=0; j<n; j++) {
if ((n - i) <= edge) {
ibs = edge;
ibs = n-edge;
skip = edge;
} else {
ibs = block_size;
skip = block_size;
}
if ((n - j) <= edge) {
jbs = edge;
jbs = n-edge;
} else {
jbs = block_size;
}
ii = (i/block_size) + (j/block_size)*nblocks;
jj = (i%ibs)+(j%jbs)*skip;
printf("%8.1f ", a[ii][jj]);
}
printf("\n");
}
fflush(stdout);
}
void CheckResult(long n, double **a, double *rhs)
{
long i, j, bogus = 0;
double *y, diff, max_diff;
long ii, jj;
long edge;
long ibs, jbs, skip;
edge = n%block_size;
y = (double *) malloc(n*sizeof(double));
if (y == NULL) {
printerr("Could not malloc memory for y\n");
exit(-1);
}
for (j=0; j<n; j++) {
y[j] = rhs[j];
}
for (j=0; j<n; j++) {
if ((n - j) <= edge) {
jbs = edge;
jbs = n-edge;
skip = edge;
} else {
jbs = block_size;
skip = block_size;
}
ii = (j/block_size) + (j/block_size)*nblocks;
jj = (j%jbs)+(j%jbs)*skip;
y[j] = y[j]/a[ii][jj];
for (i=j+1; i<n; i++) {
if ((n - i) <= edge) {
ibs = edge;
ibs = n-edge;
skip = edge;
} else {
ibs = block_size;
skip = block_size;
}
ii = (i/block_size) + (j/block_size)*nblocks;
jj = (i%ibs)+(j%jbs)*skip;
y[i] -= a[ii][jj]*y[j];
}
}
for (j=n-1; j>=0; j--) {
for (i=0; i<j; i++) {
if ((n - i) <= edge) {
ibs = edge;
ibs = n-edge;
skip = edge;
} else {
ibs = block_size;
skip = block_size;
}
if ((n - j) <= edge) {
jbs = edge;
jbs = n-edge;
} else {
jbs = block_size;
}
ii = (i/block_size) + (j/block_size)*nblocks;
jj = (i%ibs)+(j%jbs)*skip;
y[i] -= a[ii][jj]*y[j];
}
}
max_diff = 0.0;
for (j=0; j<n; j++) {
diff = y[j] - 1.0;
if (fabs(diff) > 0.00001) {
bogus = 1;
max_diff = diff;
}
}
if (bogus) {
printf("TEST FAILED: (%.5f diff)\n", max_diff);
} else {
printf("TEST PASSED\n");
}
free(y);
}
void printerr(char *s)
{
fprintf(stderr,"ERROR: %s\n",s);
}
|
the_stack_data/52577.c | #include<stdio.h>
int ary[10][10], completed[10], n, cost = 0;
void takeInput(){
int i, j;
printf("Enter the number of villages: ");
scanf("%d", &n);
printf("\nEnter the Cost Matrix\n");
for(i = 0; i < n; i++){
printf("\nEnter Elements of Row: %d\n",i+1);
for(j = 0; j < n; j++)
scanf("%d", &ary[i][j]);
completed[i] = 0;
}
printf("\n\nThe cost list is:");
for(i = 0; i < n; i++){
printf("\n");
for(j = 0; j < n; j++)
printf("\t%d", ary[i][j]);
}
}
int least(int c){
int i, nc = 999;
int min = 999, kmin;
for(i = 0; i < n; i++){
if((ary[c][i] != 0) && (completed[i] == 0))
if(ary[c][i] + ary[i][c] < min){
min = ary[i][0] + ary[c][i];
kmin = ary[c][i];
nc = i;
}
}
if(min != 999)
cost += kmin;
return nc;
}
void mincost(int city){
int i, ncity;
completed[city] = 1;
printf("%d --->",city+1);
ncity = least(city);
if(ncity == 999){
ncity = 0;
printf("%d",ncity+1);
cost += ary[city][ncity];
return;
}
mincost(ncity);
}
int main(){
takeInput();
printf("\n\nThe Path is:\n");
mincost(0);
printf("\n\nMinimum cost is %d\n ", cost);
return 0;
}
|
the_stack_data/6388920.c | #include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
int main (int argc, char *argv[])
{
struct stat sb;
int ret;
if (argc < 2) {
fprintf(stderr, "usage: %s <file>\n", argv[0]);
return 1;
}
ret = stat (argv[1], &sb);
if (ret) {
perror ("stat");
return 1;
}
printf ("File type: ");
switch (sb.st_mode & S_IFMT) {
case S_IFBLK:
printf ("block device node\n");
break;
case S_IFCHR:
printf ("character device node\n");
break;
case S_IFDIR:
printf ("directory\n");
break;
case S_IFIFO:
printf ("FIFO\n");
break;
case S_IFLNK:
printf ("symbolic link\n");
break;
case S_IFREG:
printf ("regular file\n");
break;
case S_IFSOCK:
printf ("socket\n");
break;
default:
printf ("unknown\n");
break;
}
return 0;
}
|
the_stack_data/179831374.c | //
// Created by adamzeng on 2019-06-10.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define BUF_SIZE 1024
#define TRUE 1
#define FALSE 1
void error_handling(char *message);
int main(int argc, char **argv) {
int serv_sock, clnt_sock;
char message[BUF_SIZE];
int str_len, i;
int option,optlen;
struct sockaddr_in serv_adr, clnt_adr;
socklen_t clnt_adr_sz;
if (argc != 2) {
printf("Usage: %s <port>\n", argv[0]);
exit(1);
}
serv_sock = socket(PF_INET, SOCK_STREAM, 0);
if (serv_sock == -1) {
error_handling("socket() error");
}
optlen = sizeof(option);
option = TRUE;
// 防止端口占用
setsockopt(serv_sock, SOL_SOCKET, SO_REUSEADDR, (void *) &option, optlen);
memset(&serv_adr, 0, sizeof(serv_adr));
serv_adr.sin_family = AF_INET;
serv_adr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_adr.sin_port = htons(atoi(argv[1]));
if (bind(serv_sock, (struct sockaddr *) &serv_adr, sizeof(serv_adr)) == -1) {
error_handling("bind() error");
}
if (listen(serv_sock, 5) == -1) {
error_handling("listen() error");
}
clnt_adr_sz = sizeof(clnt_adr);
for (int i = 0; i < 5; ++i) {
clnt_sock = accept(serv_sock, (struct sockaddr *) &clnt_adr, &clnt_adr_sz);
if (clnt_sock == -1) {
error_handling("accept() error");
} else {
printf("Connected client %d \n", i + 1);
}
while ((str_len = read(clnt_sock, message, BUF_SIZE)) != 0) {
printf("%s",message);
write(clnt_sock, message, str_len);
}
close(clnt_sock);
}
close(serv_sock);
return 0;
}
void error_handling(char *message) {
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
} |
the_stack_data/218894025.c | #include <stdio.h>
#include <string.h>
#include <limits.h>
int main(int argc, char **argv) {
char * utf_8_str = "touch 😸.txt";
printf("UTF-8 string: %s\n", utf_8_str);
printf("len(utf_8_str) = %ld\n", strlen(utf_8_str));
for (int i = 0; i < strlen(utf_8_str) + 1; i++) {
printf("utf_8_str[%d] = %d\n", i, (unsigned char)(utf_8_str[i]));
}
return 0;
}
|
the_stack_data/75231.c | #include <stdio.h>
int main()
{
int number, digit, dummy, counter;
number = 0;
digit = 0;
counter = 0;
printf("Введите число: ");
scanf("%d", &number);
dummy = number;
while (dummy != 0){
digit = dummy % 10;
dummy = dummy / 10;
if (digit % 2 == 1){
counter += 1;
}
}
printf("В числе %d %d нечетных цифр\n", number, counter);
return 0;
}
|
the_stack_data/161080335.c | // RUN: %llvmgcc %s -S -o /dev/null
// GCC allows variable sized arrays in structures, crazy!
// This is PR360.
int sub1(int i, char *pi) {
typedef int foo[i];
struct bar {foo f1; int f2;} *p = (struct bar *) pi;
return p->f2;
}
|
the_stack_data/220455156.c | #include <stdio.h>
int main()
{
double s;
printf("Enter Frahrenheit temperatur:\n");
scanf("%lf",&s);
printf("Celsius equivalent: %.1lf\n",5.0*(s-32)/9.0);
return 0;
}
|
the_stack_data/95777.c | #include <stdio.h>
#include <string.h>
const int open = 0, opening = 1, stopped_open = 2, stopped_close = 3, closing = 4, closed = 5;
int main() {
char word[14][1000];
int time = 0, i;
int current = closed;
while(!feof(stdin)) {
scanf("%s", word[time++]);
}
printf("Door: CLOSED\n");
for(i = 0; i < time-1; i++) {
if(strcmp(word[i], "button_clicked") == 0) {
if(current == closed) {
printf("Door: OPENING\n");
current = opening;
continue;
}
if(current == open) {
printf("Door: CLOSING\n");
current = closing;
continue;
}
if(current == opening) {
printf("Door: STOPPED_WHILE_OPENING\n");
current = stopped_open;
continue;
}
if(current == closing) {
printf("Door: STOPPED_WHILE_CLOSING\n");
current = stopped_close;
continue;
}
if(current == stopped_open) {
printf("Door: CLOSING\n");
current = closing;
continue;
}
if(current == stopped_close) {
printf("Door: OPENING\n");
current = opening;
continue;
}
}
else if(strcmp(word[i], "cycle_complete") == 0){
if(current == opening) {
printf("Door: OPEN\n");
current = open;
}
if(current == closing) {
printf("Door: CLOSED\n");
current = closed;
}
}
}
return 0;
}
|
the_stack_data/68888273.c | /*
* Copyright (c) 2017, 2018, Oracle and/or its affiliates.
*
* 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. Neither the name of the copyright holder nor the names of its contributors may be used to
* endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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
* COPYRIGHT HOLDER OR CONTRIBUTORS 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.
*/
extern void abort(void);
struct test {
char a : 2;
};
int main() {
struct test t = { 0 };
if (t.a++ != 0) {
abort();
}
if (t.a != 1) {
abort();
}
if (++t.a != -2) {
abort();
}
if (t.a != -2) {
abort();
}
if (t.a-- != -2) {
abort();
}
if (t.a != 1) {
abort();
}
if (--t.a != 0) {
abort();
}
if (t.a != 0) {
abort();
}
return 0;
}
|
the_stack_data/150140997.c | // general protection fault in bd_set_size
// https://syzkaller.appspot.com/bug?id=6046790aa728b7b1ef97f95eb8e1d20801bc9423
// status:open
// autogenerated by syzkaller (https://github.com/google/syzkaller)
#define _GNU_SOURCE
#include <arpa/inet.h>
#include <dirent.h>
#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <net/if_arp.h>
#include <netinet/in.h>
#include <pthread.h>
#include <sched.h>
#include <setjmp.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/prctl.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include <linux/futex.h>
#include <linux/genetlink.h>
#include <linux/if.h>
#include <linux/if_ether.h>
#include <linux/if_tun.h>
#include <linux/ip.h>
#include <linux/loop.h>
#include <linux/net.h>
#include <linux/netlink.h>
#include <linux/tcp.h>
unsigned long long procid;
static __thread int skip_segv;
static __thread jmp_buf segv_env;
static void segv_handler(int sig, siginfo_t* info, void* ctx)
{
uintptr_t addr = (uintptr_t)info->si_addr;
const uintptr_t prog_start = 1 << 20;
const uintptr_t prog_end = 100 << 20;
if (__atomic_load_n(&skip_segv, __ATOMIC_RELAXED) &&
(addr < prog_start || addr > prog_end)) {
_longjmp(segv_env, 1);
}
exit(sig);
}
static void install_segv_handler(void)
{
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = SIG_IGN;
syscall(SYS_rt_sigaction, 0x20, &sa, NULL, 8);
syscall(SYS_rt_sigaction, 0x21, &sa, NULL, 8);
memset(&sa, 0, sizeof(sa));
sa.sa_sigaction = segv_handler;
sa.sa_flags = SA_NODEFER | SA_SIGINFO;
sigaction(SIGSEGV, &sa, NULL);
sigaction(SIGBUS, &sa, NULL);
}
#define NONFAILING(...) \
{ \
__atomic_fetch_add(&skip_segv, 1, __ATOMIC_SEQ_CST); \
if (_setjmp(segv_env) == 0) { \
__VA_ARGS__; \
} \
__atomic_fetch_sub(&skip_segv, 1, __ATOMIC_SEQ_CST); \
}
static void sleep_ms(uint64_t ms)
{
usleep(ms * 1000);
}
static uint64_t current_time_ms(void)
{
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts))
exit(1);
return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
}
static void use_temporary_dir(void)
{
char tmpdir_template[] = "./syzkaller.XXXXXX";
char* tmpdir = mkdtemp(tmpdir_template);
if (!tmpdir)
exit(1);
if (chmod(tmpdir, 0777))
exit(1);
if (chdir(tmpdir))
exit(1);
}
static void thread_start(void* (*fn)(void*), void* arg)
{
pthread_t th;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, 128 << 10);
if (pthread_create(&th, &attr, fn, arg))
exit(1);
pthread_attr_destroy(&attr);
}
typedef struct {
int state;
} event_t;
static void event_init(event_t* ev)
{
ev->state = 0;
}
static void event_reset(event_t* ev)
{
ev->state = 0;
}
static void event_set(event_t* ev)
{
if (ev->state)
exit(1);
__atomic_store_n(&ev->state, 1, __ATOMIC_RELEASE);
syscall(SYS_futex, &ev->state, FUTEX_WAKE | FUTEX_PRIVATE_FLAG);
}
static void event_wait(event_t* ev)
{
while (!__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE))
syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, 0);
}
static int event_isset(event_t* ev)
{
return __atomic_load_n(&ev->state, __ATOMIC_ACQUIRE);
}
static int event_timedwait(event_t* ev, uint64_t timeout)
{
uint64_t start = current_time_ms();
uint64_t now = start;
for (;;) {
uint64_t remain = timeout - (now - start);
struct timespec ts;
ts.tv_sec = remain / 1000;
ts.tv_nsec = (remain % 1000) * 1000 * 1000;
syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, &ts);
if (__atomic_load_n(&ev->state, __ATOMIC_RELAXED))
return 1;
now = current_time_ms();
if (now - start > timeout)
return 0;
}
}
static void vsnprintf_check(char* str, size_t size, const char* format,
va_list args)
{
int rv;
rv = vsnprintf(str, size, format, args);
if (rv < 0)
exit(1);
if ((size_t)rv >= size)
exit(1);
}
#define COMMAND_MAX_LEN 128
#define PATH_PREFIX \
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin "
#define PATH_PREFIX_LEN (sizeof(PATH_PREFIX) - 1)
static void execute_command(bool panic, const char* format, ...)
{
va_list args;
char command[PATH_PREFIX_LEN + COMMAND_MAX_LEN];
int rv;
va_start(args, format);
memcpy(command, PATH_PREFIX, PATH_PREFIX_LEN);
vsnprintf_check(command + PATH_PREFIX_LEN, COMMAND_MAX_LEN, format, args);
va_end(args);
rv = system(command);
if (rv) {
if (panic)
exit(1);
}
}
static int tunfd = -1;
static int tun_frags_enabled;
#define SYZ_TUN_MAX_PACKET_SIZE 1000
#define TUN_IFACE "syz_tun"
#define LOCAL_MAC "aa:aa:aa:aa:aa:aa"
#define REMOTE_MAC "aa:aa:aa:aa:aa:bb"
#define LOCAL_IPV4 "172.20.20.170"
#define REMOTE_IPV4 "172.20.20.187"
#define LOCAL_IPV6 "fe80::aa"
#define REMOTE_IPV6 "fe80::bb"
#define IFF_NAPI 0x0010
#define IFF_NAPI_FRAGS 0x0020
static void initialize_tun(void)
{
tunfd = open("/dev/net/tun", O_RDWR | O_NONBLOCK);
if (tunfd == -1) {
printf("tun: can't open /dev/net/tun: please enable CONFIG_TUN=y\n");
printf("otherwise fuzzing or reproducing might not work as intended\n");
return;
}
const int kTunFd = 240;
if (dup2(tunfd, kTunFd) < 0)
exit(1);
close(tunfd);
tunfd = kTunFd;
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, TUN_IFACE, IFNAMSIZ);
ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_NAPI | IFF_NAPI_FRAGS;
if (ioctl(tunfd, TUNSETIFF, (void*)&ifr) < 0) {
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
if (ioctl(tunfd, TUNSETIFF, (void*)&ifr) < 0)
exit(1);
}
if (ioctl(tunfd, TUNGETIFF, (void*)&ifr) < 0)
exit(1);
tun_frags_enabled = (ifr.ifr_flags & IFF_NAPI_FRAGS) != 0;
execute_command(0, "sysctl -w net.ipv6.conf.%s.accept_dad=0", TUN_IFACE);
execute_command(0, "sysctl -w net.ipv6.conf.%s.router_solicitations=0",
TUN_IFACE);
execute_command(1, "ip link set dev %s address %s", TUN_IFACE, LOCAL_MAC);
execute_command(1, "ip addr add %s/24 dev %s", LOCAL_IPV4, TUN_IFACE);
execute_command(1, "ip neigh add %s lladdr %s dev %s nud permanent",
REMOTE_IPV4, REMOTE_MAC, TUN_IFACE);
execute_command(0, "ip -6 addr add %s/120 dev %s", LOCAL_IPV6, TUN_IFACE);
execute_command(0, "ip -6 neigh add %s lladdr %s dev %s nud permanent",
REMOTE_IPV6, REMOTE_MAC, TUN_IFACE);
execute_command(1, "ip link set dev %s up", TUN_IFACE);
}
#define DEV_IPV4 "172.20.20.%d"
#define DEV_IPV6 "fe80::%02hx"
#define DEV_MAC "aa:aa:aa:aa:aa:%02hx"
static void snprintf_check(char* str, size_t size, const char* format, ...)
{
va_list args;
va_start(args, format);
vsnprintf_check(str, size, format, args);
va_end(args);
}
static void initialize_netdevices(void)
{
unsigned i;
const char* devtypes[] = {"ip6gretap", "bridge", "vcan", "bond", "team"};
const char* devnames[] = {"lo",
"sit0",
"bridge0",
"vcan0",
"tunl0",
"gre0",
"gretap0",
"ip_vti0",
"ip6_vti0",
"ip6tnl0",
"ip6gre0",
"ip6gretap0",
"erspan0",
"bond0",
"veth0",
"veth1",
"team0",
"veth0_to_bridge",
"veth1_to_bridge",
"veth0_to_bond",
"veth1_to_bond",
"veth0_to_team",
"veth1_to_team"};
const char* devmasters[] = {"bridge", "bond", "team"};
for (i = 0; i < sizeof(devtypes) / (sizeof(devtypes[0])); i++)
execute_command(0, "ip link add dev %s0 type %s", devtypes[i], devtypes[i]);
execute_command(0, "ip link add type veth");
for (i = 0; i < sizeof(devmasters) / (sizeof(devmasters[0])); i++) {
execute_command(
0, "ip link add name %s_slave_0 type veth peer name veth0_to_%s",
devmasters[i], devmasters[i]);
execute_command(
0, "ip link add name %s_slave_1 type veth peer name veth1_to_%s",
devmasters[i], devmasters[i]);
execute_command(0, "ip link set %s_slave_0 master %s0", devmasters[i],
devmasters[i]);
execute_command(0, "ip link set %s_slave_1 master %s0", devmasters[i],
devmasters[i]);
execute_command(0, "ip link set veth0_to_%s up", devmasters[i]);
execute_command(0, "ip link set veth1_to_%s up", devmasters[i]);
}
execute_command(0, "ip link set bridge_slave_0 up");
execute_command(0, "ip link set bridge_slave_1 up");
for (i = 0; i < sizeof(devnames) / (sizeof(devnames[0])); i++) {
char addr[32];
snprintf_check(addr, sizeof(addr), DEV_IPV4, i + 10);
execute_command(0, "ip -4 addr add %s/24 dev %s", addr, devnames[i]);
snprintf_check(addr, sizeof(addr), DEV_IPV6, i + 10);
execute_command(0, "ip -6 addr add %s/120 dev %s", addr, devnames[i]);
snprintf_check(addr, sizeof(addr), DEV_MAC, i + 10);
execute_command(0, "ip link set dev %s address %s", devnames[i], addr);
execute_command(0, "ip link set dev %s up", devnames[i]);
}
}
static int read_tun(char* data, int size)
{
if (tunfd < 0)
return -1;
int rv = read(tunfd, data, size);
if (rv < 0) {
if (errno == EAGAIN)
return -1;
if (errno == EBADFD)
return -1;
exit(1);
}
return rv;
}
static void flush_tun()
{
char data[SYZ_TUN_MAX_PACKET_SIZE];
while (read_tun(&data[0], sizeof(data)) != -1) {
}
}
static long syz_genetlink_get_family_id(long name)
{
char buf[512] = {0};
struct nlmsghdr* hdr = (struct nlmsghdr*)buf;
struct genlmsghdr* genlhdr = (struct genlmsghdr*)NLMSG_DATA(hdr);
struct nlattr* attr = (struct nlattr*)(genlhdr + 1);
hdr->nlmsg_len =
sizeof(*hdr) + sizeof(*genlhdr) + sizeof(*attr) + GENL_NAMSIZ;
hdr->nlmsg_type = GENL_ID_CTRL;
hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
genlhdr->cmd = CTRL_CMD_GETFAMILY;
attr->nla_type = CTRL_ATTR_FAMILY_NAME;
attr->nla_len = sizeof(*attr) + GENL_NAMSIZ;
NONFAILING(strncpy((char*)(attr + 1), (char*)name, GENL_NAMSIZ));
struct iovec iov = {hdr, hdr->nlmsg_len};
struct sockaddr_nl addr = {0};
addr.nl_family = AF_NETLINK;
int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
if (fd == -1) {
return -1;
}
struct msghdr msg = {&addr, sizeof(addr), &iov, 1, NULL, 0, 0};
if (sendmsg(fd, &msg, 0) == -1) {
close(fd);
return -1;
}
ssize_t n = recv(fd, buf, sizeof(buf), 0);
close(fd);
if (n <= 0) {
return -1;
}
if (hdr->nlmsg_type != GENL_ID_CTRL) {
return -1;
}
for (; (char*)attr < buf + n;
attr = (struct nlattr*)((char*)attr + NLMSG_ALIGN(attr->nla_len))) {
if (attr->nla_type == CTRL_ATTR_FAMILY_ID)
return *(uint16_t*)(attr + 1);
}
return -1;
}
struct fs_image_segment {
void* data;
uintptr_t size;
uintptr_t offset;
};
#define IMAGE_MAX_SEGMENTS 4096
#define IMAGE_MAX_SIZE (129 << 20)
#define SYZ_memfd_create 319
static long syz_mount_image(long fsarg, long dir, unsigned long size,
unsigned long nsegs, long segments, long flags,
long optsarg)
{
char loopname[64], fs[32], opts[256];
int loopfd, err = 0, res = -1;
unsigned long i;
struct fs_image_segment* segs = (struct fs_image_segment*)segments;
if (nsegs > IMAGE_MAX_SEGMENTS)
nsegs = IMAGE_MAX_SEGMENTS;
for (i = 0; i < nsegs; i++) {
if (segs[i].size > IMAGE_MAX_SIZE)
segs[i].size = IMAGE_MAX_SIZE;
segs[i].offset %= IMAGE_MAX_SIZE;
if (segs[i].offset > IMAGE_MAX_SIZE - segs[i].size)
segs[i].offset = IMAGE_MAX_SIZE - segs[i].size;
if (size < segs[i].offset + segs[i].offset)
size = segs[i].offset + segs[i].offset;
}
if (size > IMAGE_MAX_SIZE)
size = IMAGE_MAX_SIZE;
int memfd = syscall(SYZ_memfd_create, "syz_mount_image", 0);
if (memfd == -1) {
err = errno;
goto error;
}
if (ftruncate(memfd, size)) {
err = errno;
goto error_close_memfd;
}
for (i = 0; i < nsegs; i++) {
if (pwrite(memfd, segs[i].data, segs[i].size, segs[i].offset) < 0) {
}
}
snprintf(loopname, sizeof(loopname), "/dev/loop%llu", procid);
loopfd = open(loopname, O_RDWR);
if (loopfd == -1) {
err = errno;
goto error_close_memfd;
}
if (ioctl(loopfd, LOOP_SET_FD, memfd)) {
if (errno != EBUSY) {
err = errno;
goto error_close_loop;
}
ioctl(loopfd, LOOP_CLR_FD, 0);
usleep(1000);
if (ioctl(loopfd, LOOP_SET_FD, memfd)) {
err = errno;
goto error_close_loop;
}
}
mkdir((char*)dir, 0777);
memset(fs, 0, sizeof(fs));
NONFAILING(strncpy(fs, (char*)fsarg, sizeof(fs) - 1));
memset(opts, 0, sizeof(opts));
NONFAILING(strncpy(opts, (char*)optsarg, sizeof(opts) - 32));
if (strcmp(fs, "iso9660") == 0) {
flags |= MS_RDONLY;
} else if (strncmp(fs, "ext", 3) == 0) {
if (strstr(opts, "errors=panic") || strstr(opts, "errors=remount-ro") == 0)
strcat(opts, ",errors=continue");
} else if (strcmp(fs, "xfs") == 0) {
strcat(opts, ",nouuid");
}
if (mount(loopname, (char*)dir, fs, flags, opts)) {
err = errno;
goto error_clear_loop;
}
res = 0;
error_clear_loop:
ioctl(loopfd, LOOP_CLR_FD, 0);
error_close_loop:
close(loopfd);
error_close_memfd:
close(memfd);
error:
errno = err;
return res;
}
static bool write_file(const char* file, const char* what, ...)
{
char buf[1024];
va_list args;
va_start(args, what);
vsnprintf(buf, sizeof(buf), what, args);
va_end(args);
buf[sizeof(buf) - 1] = 0;
int len = strlen(buf);
int fd = open(file, O_WRONLY | O_CLOEXEC);
if (fd == -1)
return false;
if (write(fd, buf, len) != len) {
int err = errno;
close(fd);
errno = err;
return false;
}
close(fd);
return true;
}
#define XT_TABLE_SIZE 1536
#define XT_MAX_ENTRIES 10
struct xt_counters {
uint64_t pcnt, bcnt;
};
struct ipt_getinfo {
char name[32];
unsigned int valid_hooks;
unsigned int hook_entry[5];
unsigned int underflow[5];
unsigned int num_entries;
unsigned int size;
};
struct ipt_get_entries {
char name[32];
unsigned int size;
void* entrytable[XT_TABLE_SIZE / sizeof(void*)];
};
struct ipt_replace {
char name[32];
unsigned int valid_hooks;
unsigned int num_entries;
unsigned int size;
unsigned int hook_entry[5];
unsigned int underflow[5];
unsigned int num_counters;
struct xt_counters* counters;
char entrytable[XT_TABLE_SIZE];
};
struct ipt_table_desc {
const char* name;
struct ipt_getinfo info;
struct ipt_replace replace;
};
static struct ipt_table_desc ipv4_tables[] = {
{.name = "filter"}, {.name = "nat"}, {.name = "mangle"},
{.name = "raw"}, {.name = "security"},
};
static struct ipt_table_desc ipv6_tables[] = {
{.name = "filter"}, {.name = "nat"}, {.name = "mangle"},
{.name = "raw"}, {.name = "security"},
};
#define IPT_BASE_CTL 64
#define IPT_SO_SET_REPLACE (IPT_BASE_CTL)
#define IPT_SO_GET_INFO (IPT_BASE_CTL)
#define IPT_SO_GET_ENTRIES (IPT_BASE_CTL + 1)
struct arpt_getinfo {
char name[32];
unsigned int valid_hooks;
unsigned int hook_entry[3];
unsigned int underflow[3];
unsigned int num_entries;
unsigned int size;
};
struct arpt_get_entries {
char name[32];
unsigned int size;
void* entrytable[XT_TABLE_SIZE / sizeof(void*)];
};
struct arpt_replace {
char name[32];
unsigned int valid_hooks;
unsigned int num_entries;
unsigned int size;
unsigned int hook_entry[3];
unsigned int underflow[3];
unsigned int num_counters;
struct xt_counters* counters;
char entrytable[XT_TABLE_SIZE];
};
struct arpt_table_desc {
const char* name;
struct arpt_getinfo info;
struct arpt_replace replace;
};
static struct arpt_table_desc arpt_tables[] = {
{.name = "filter"},
};
#define ARPT_BASE_CTL 96
#define ARPT_SO_SET_REPLACE (ARPT_BASE_CTL)
#define ARPT_SO_GET_INFO (ARPT_BASE_CTL)
#define ARPT_SO_GET_ENTRIES (ARPT_BASE_CTL + 1)
static void checkpoint_iptables(struct ipt_table_desc* tables, int num_tables,
int family, int level)
{
struct ipt_get_entries entries;
socklen_t optlen;
int fd, i;
fd = socket(family, SOCK_STREAM, IPPROTO_TCP);
if (fd == -1) {
switch (errno) {
case EAFNOSUPPORT:
case ENOPROTOOPT:
return;
}
exit(1);
}
for (i = 0; i < num_tables; i++) {
struct ipt_table_desc* table = &tables[i];
strcpy(table->info.name, table->name);
strcpy(table->replace.name, table->name);
optlen = sizeof(table->info);
if (getsockopt(fd, level, IPT_SO_GET_INFO, &table->info, &optlen)) {
switch (errno) {
case EPERM:
case ENOENT:
case ENOPROTOOPT:
continue;
}
exit(1);
}
if (table->info.size > sizeof(table->replace.entrytable))
exit(1);
if (table->info.num_entries > XT_MAX_ENTRIES)
exit(1);
memset(&entries, 0, sizeof(entries));
strcpy(entries.name, table->name);
entries.size = table->info.size;
optlen = sizeof(entries) - sizeof(entries.entrytable) + table->info.size;
if (getsockopt(fd, level, IPT_SO_GET_ENTRIES, &entries, &optlen))
exit(1);
table->replace.valid_hooks = table->info.valid_hooks;
table->replace.num_entries = table->info.num_entries;
table->replace.size = table->info.size;
memcpy(table->replace.hook_entry, table->info.hook_entry,
sizeof(table->replace.hook_entry));
memcpy(table->replace.underflow, table->info.underflow,
sizeof(table->replace.underflow));
memcpy(table->replace.entrytable, entries.entrytable, table->info.size);
}
close(fd);
}
static void reset_iptables(struct ipt_table_desc* tables, int num_tables,
int family, int level)
{
struct xt_counters counters[XT_MAX_ENTRIES];
struct ipt_get_entries entries;
struct ipt_getinfo info;
socklen_t optlen;
int fd, i;
fd = socket(family, SOCK_STREAM, IPPROTO_TCP);
if (fd == -1) {
switch (errno) {
case EAFNOSUPPORT:
case ENOPROTOOPT:
return;
}
exit(1);
}
for (i = 0; i < num_tables; i++) {
struct ipt_table_desc* table = &tables[i];
if (table->info.valid_hooks == 0)
continue;
memset(&info, 0, sizeof(info));
strcpy(info.name, table->name);
optlen = sizeof(info);
if (getsockopt(fd, level, IPT_SO_GET_INFO, &info, &optlen))
exit(1);
if (memcmp(&table->info, &info, sizeof(table->info)) == 0) {
memset(&entries, 0, sizeof(entries));
strcpy(entries.name, table->name);
entries.size = table->info.size;
optlen = sizeof(entries) - sizeof(entries.entrytable) + entries.size;
if (getsockopt(fd, level, IPT_SO_GET_ENTRIES, &entries, &optlen))
exit(1);
if (memcmp(table->replace.entrytable, entries.entrytable,
table->info.size) == 0)
continue;
}
table->replace.num_counters = info.num_entries;
table->replace.counters = counters;
optlen = sizeof(table->replace) - sizeof(table->replace.entrytable) +
table->replace.size;
if (setsockopt(fd, level, IPT_SO_SET_REPLACE, &table->replace, optlen))
exit(1);
}
close(fd);
}
static void checkpoint_arptables(void)
{
struct arpt_get_entries entries;
socklen_t optlen;
unsigned i;
int fd;
fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fd == -1) {
switch (errno) {
case EAFNOSUPPORT:
case ENOPROTOOPT:
return;
}
exit(1);
}
for (i = 0; i < sizeof(arpt_tables) / sizeof(arpt_tables[0]); i++) {
struct arpt_table_desc* table = &arpt_tables[i];
strcpy(table->info.name, table->name);
strcpy(table->replace.name, table->name);
optlen = sizeof(table->info);
if (getsockopt(fd, SOL_IP, ARPT_SO_GET_INFO, &table->info, &optlen)) {
switch (errno) {
case EPERM:
case ENOENT:
case ENOPROTOOPT:
continue;
}
exit(1);
}
if (table->info.size > sizeof(table->replace.entrytable))
exit(1);
if (table->info.num_entries > XT_MAX_ENTRIES)
exit(1);
memset(&entries, 0, sizeof(entries));
strcpy(entries.name, table->name);
entries.size = table->info.size;
optlen = sizeof(entries) - sizeof(entries.entrytable) + table->info.size;
if (getsockopt(fd, SOL_IP, ARPT_SO_GET_ENTRIES, &entries, &optlen))
exit(1);
table->replace.valid_hooks = table->info.valid_hooks;
table->replace.num_entries = table->info.num_entries;
table->replace.size = table->info.size;
memcpy(table->replace.hook_entry, table->info.hook_entry,
sizeof(table->replace.hook_entry));
memcpy(table->replace.underflow, table->info.underflow,
sizeof(table->replace.underflow));
memcpy(table->replace.entrytable, entries.entrytable, table->info.size);
}
close(fd);
}
static void reset_arptables()
{
struct xt_counters counters[XT_MAX_ENTRIES];
struct arpt_get_entries entries;
struct arpt_getinfo info;
socklen_t optlen;
unsigned i;
int fd;
fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fd == -1) {
switch (errno) {
case EAFNOSUPPORT:
case ENOPROTOOPT:
return;
}
exit(1);
}
for (i = 0; i < sizeof(arpt_tables) / sizeof(arpt_tables[0]); i++) {
struct arpt_table_desc* table = &arpt_tables[i];
if (table->info.valid_hooks == 0)
continue;
memset(&info, 0, sizeof(info));
strcpy(info.name, table->name);
optlen = sizeof(info);
if (getsockopt(fd, SOL_IP, ARPT_SO_GET_INFO, &info, &optlen))
exit(1);
if (memcmp(&table->info, &info, sizeof(table->info)) == 0) {
memset(&entries, 0, sizeof(entries));
strcpy(entries.name, table->name);
entries.size = table->info.size;
optlen = sizeof(entries) - sizeof(entries.entrytable) + entries.size;
if (getsockopt(fd, SOL_IP, ARPT_SO_GET_ENTRIES, &entries, &optlen))
exit(1);
if (memcmp(table->replace.entrytable, entries.entrytable,
table->info.size) == 0)
continue;
} else {
}
table->replace.num_counters = info.num_entries;
table->replace.counters = counters;
optlen = sizeof(table->replace) - sizeof(table->replace.entrytable) +
table->replace.size;
if (setsockopt(fd, SOL_IP, ARPT_SO_SET_REPLACE, &table->replace, optlen))
exit(1);
}
close(fd);
}
#define NF_BR_NUMHOOKS 6
#define EBT_TABLE_MAXNAMELEN 32
#define EBT_CHAIN_MAXNAMELEN 32
#define EBT_BASE_CTL 128
#define EBT_SO_SET_ENTRIES (EBT_BASE_CTL)
#define EBT_SO_GET_INFO (EBT_BASE_CTL)
#define EBT_SO_GET_ENTRIES (EBT_SO_GET_INFO + 1)
#define EBT_SO_GET_INIT_INFO (EBT_SO_GET_ENTRIES + 1)
#define EBT_SO_GET_INIT_ENTRIES (EBT_SO_GET_INIT_INFO + 1)
struct ebt_replace {
char name[EBT_TABLE_MAXNAMELEN];
unsigned int valid_hooks;
unsigned int nentries;
unsigned int entries_size;
struct ebt_entries* hook_entry[NF_BR_NUMHOOKS];
unsigned int num_counters;
struct ebt_counter* counters;
char* entries;
};
struct ebt_entries {
unsigned int distinguisher;
char name[EBT_CHAIN_MAXNAMELEN];
unsigned int counter_offset;
int policy;
unsigned int nentries;
char data[0] __attribute__((aligned(__alignof__(struct ebt_replace))));
};
struct ebt_table_desc {
const char* name;
struct ebt_replace replace;
char entrytable[XT_TABLE_SIZE];
};
static struct ebt_table_desc ebt_tables[] = {
{.name = "filter"},
{.name = "nat"},
{.name = "broute"},
};
static void checkpoint_ebtables(void)
{
socklen_t optlen;
unsigned i;
int fd;
fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fd == -1) {
switch (errno) {
case EAFNOSUPPORT:
case ENOPROTOOPT:
return;
}
exit(1);
}
for (i = 0; i < sizeof(ebt_tables) / sizeof(ebt_tables[0]); i++) {
struct ebt_table_desc* table = &ebt_tables[i];
strcpy(table->replace.name, table->name);
optlen = sizeof(table->replace);
if (getsockopt(fd, SOL_IP, EBT_SO_GET_INIT_INFO, &table->replace,
&optlen)) {
switch (errno) {
case EPERM:
case ENOENT:
case ENOPROTOOPT:
continue;
}
exit(1);
}
if (table->replace.entries_size > sizeof(table->entrytable))
exit(1);
table->replace.num_counters = 0;
table->replace.entries = table->entrytable;
optlen = sizeof(table->replace) + table->replace.entries_size;
if (getsockopt(fd, SOL_IP, EBT_SO_GET_INIT_ENTRIES, &table->replace,
&optlen))
exit(1);
}
close(fd);
}
static void reset_ebtables()
{
struct ebt_replace replace;
char entrytable[XT_TABLE_SIZE];
socklen_t optlen;
unsigned i, j, h;
int fd;
fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fd == -1) {
switch (errno) {
case EAFNOSUPPORT:
case ENOPROTOOPT:
return;
}
exit(1);
}
for (i = 0; i < sizeof(ebt_tables) / sizeof(ebt_tables[0]); i++) {
struct ebt_table_desc* table = &ebt_tables[i];
if (table->replace.valid_hooks == 0)
continue;
memset(&replace, 0, sizeof(replace));
strcpy(replace.name, table->name);
optlen = sizeof(replace);
if (getsockopt(fd, SOL_IP, EBT_SO_GET_INFO, &replace, &optlen))
exit(1);
replace.num_counters = 0;
table->replace.entries = 0;
for (h = 0; h < NF_BR_NUMHOOKS; h++)
table->replace.hook_entry[h] = 0;
if (memcmp(&table->replace, &replace, sizeof(table->replace)) == 0) {
memset(&entrytable, 0, sizeof(entrytable));
replace.entries = entrytable;
optlen = sizeof(replace) + replace.entries_size;
if (getsockopt(fd, SOL_IP, EBT_SO_GET_ENTRIES, &replace, &optlen))
exit(1);
if (memcmp(table->entrytable, entrytable, replace.entries_size) == 0)
continue;
}
for (j = 0, h = 0; h < NF_BR_NUMHOOKS; h++) {
if (table->replace.valid_hooks & (1 << h)) {
table->replace.hook_entry[h] =
(struct ebt_entries*)table->entrytable + j;
j++;
}
}
table->replace.entries = table->entrytable;
optlen = sizeof(table->replace) + table->replace.entries_size;
if (setsockopt(fd, SOL_IP, EBT_SO_SET_ENTRIES, &table->replace, optlen))
exit(1);
}
close(fd);
}
static void checkpoint_net_namespace(void)
{
checkpoint_ebtables();
checkpoint_arptables();
checkpoint_iptables(ipv4_tables, sizeof(ipv4_tables) / sizeof(ipv4_tables[0]),
AF_INET, SOL_IP);
checkpoint_iptables(ipv6_tables, sizeof(ipv6_tables) / sizeof(ipv6_tables[0]),
AF_INET6, SOL_IPV6);
}
static void reset_net_namespace(void)
{
reset_ebtables();
reset_arptables();
reset_iptables(ipv4_tables, sizeof(ipv4_tables) / sizeof(ipv4_tables[0]),
AF_INET, SOL_IP);
reset_iptables(ipv6_tables, sizeof(ipv6_tables) / sizeof(ipv6_tables[0]),
AF_INET6, SOL_IPV6);
}
static void setup_cgroups()
{
if (mkdir("/syzcgroup", 0777)) {
}
if (mkdir("/syzcgroup/unified", 0777)) {
}
if (mount("none", "/syzcgroup/unified", "cgroup2", 0, NULL)) {
}
if (chmod("/syzcgroup/unified", 0777)) {
}
if (!write_file("/syzcgroup/unified/cgroup.subtree_control",
"+cpu +memory +io +pids +rdma")) {
}
if (mkdir("/syzcgroup/cpu", 0777)) {
}
if (mount("none", "/syzcgroup/cpu", "cgroup", 0,
"cpuset,cpuacct,perf_event,hugetlb")) {
}
if (!write_file("/syzcgroup/cpu/cgroup.clone_children", "1")) {
}
if (chmod("/syzcgroup/cpu", 0777)) {
}
if (mkdir("/syzcgroup/net", 0777)) {
}
if (mount("none", "/syzcgroup/net", "cgroup", 0,
"net_cls,net_prio,devices,freezer")) {
}
if (chmod("/syzcgroup/net", 0777)) {
}
if (!write_file("/proc/self/oom_score_adj", "-1000")) {
}
}
static void setup_binfmt_misc()
{
if (mount(0, "/proc/sys/fs/binfmt_misc", "binfmt_misc", 0, 0)) {
}
if (!write_file("/proc/sys/fs/binfmt_misc/register",
":syz0:M:0:\x01::./file0:")) {
}
if (!write_file("/proc/sys/fs/binfmt_misc/register",
":syz1:M:1:\x02::./file0:POC")) {
}
}
static void setup_common()
{
if (mount(0, "/sys/fs/fuse/connections", "fusectl", 0, 0)) {
}
setup_cgroups();
setup_binfmt_misc();
}
static void loop();
static void sandbox_common()
{
prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
setpgrp();
setsid();
struct rlimit rlim;
rlim.rlim_cur = rlim.rlim_max = 160 << 20;
setrlimit(RLIMIT_AS, &rlim);
rlim.rlim_cur = rlim.rlim_max = 8 << 20;
setrlimit(RLIMIT_MEMLOCK, &rlim);
rlim.rlim_cur = rlim.rlim_max = 136 << 20;
setrlimit(RLIMIT_FSIZE, &rlim);
rlim.rlim_cur = rlim.rlim_max = 1 << 20;
setrlimit(RLIMIT_STACK, &rlim);
rlim.rlim_cur = rlim.rlim_max = 0;
setrlimit(RLIMIT_CORE, &rlim);
rlim.rlim_cur = rlim.rlim_max = 256;
setrlimit(RLIMIT_NOFILE, &rlim);
if (unshare(CLONE_NEWNS)) {
}
if (unshare(CLONE_NEWIPC)) {
}
if (unshare(0x02000000)) {
}
if (unshare(CLONE_NEWUTS)) {
}
if (unshare(CLONE_SYSVSEM)) {
}
}
int wait_for_loop(int pid)
{
if (pid < 0)
exit(1);
int status = 0;
while (waitpid(-1, &status, __WALL) != pid) {
}
return WEXITSTATUS(status);
}
static int do_sandbox_none(void)
{
if (unshare(CLONE_NEWPID)) {
}
int pid = fork();
if (pid != 0)
return wait_for_loop(pid);
setup_common();
sandbox_common();
if (unshare(CLONE_NEWNET)) {
}
initialize_tun();
initialize_netdevices();
loop();
exit(1);
}
#define FS_IOC_SETFLAGS _IOW('f', 2, long)
static void remove_dir(const char* dir)
{
DIR* dp;
struct dirent* ep;
int iter = 0;
retry:
while (umount2(dir, MNT_DETACH) == 0) {
}
dp = opendir(dir);
if (dp == NULL) {
if (errno == EMFILE) {
exit(1);
}
exit(1);
}
while ((ep = readdir(dp))) {
if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0)
continue;
char filename[FILENAME_MAX];
snprintf(filename, sizeof(filename), "%s/%s", dir, ep->d_name);
while (umount2(filename, MNT_DETACH) == 0) {
}
struct stat st;
if (lstat(filename, &st))
exit(1);
if (S_ISDIR(st.st_mode)) {
remove_dir(filename);
continue;
}
int i;
for (i = 0;; i++) {
if (unlink(filename) == 0)
break;
if (errno == EPERM) {
int fd = open(filename, O_RDONLY);
if (fd != -1) {
long flags = 0;
if (ioctl(fd, FS_IOC_SETFLAGS, &flags) == 0)
close(fd);
continue;
}
}
if (errno == EROFS) {
break;
}
if (errno != EBUSY || i > 100)
exit(1);
if (umount2(filename, MNT_DETACH))
exit(1);
}
}
closedir(dp);
int i;
for (i = 0;; i++) {
if (rmdir(dir) == 0)
break;
if (i < 100) {
if (errno == EPERM) {
int fd = open(dir, O_RDONLY);
if (fd != -1) {
long flags = 0;
if (ioctl(fd, FS_IOC_SETFLAGS, &flags) == 0)
close(fd);
continue;
}
}
if (errno == EROFS) {
break;
}
if (errno == EBUSY) {
if (umount2(dir, MNT_DETACH))
exit(1);
continue;
}
if (errno == ENOTEMPTY) {
if (iter < 100) {
iter++;
goto retry;
}
}
}
exit(1);
}
}
static void kill_and_wait(int pid, int* status)
{
kill(-pid, SIGKILL);
kill(pid, SIGKILL);
int i;
for (i = 0; i < 100; i++) {
if (waitpid(-1, status, WNOHANG | __WALL) == pid)
return;
usleep(1000);
}
DIR* dir = opendir("/sys/fs/fuse/connections");
if (dir) {
for (;;) {
struct dirent* ent = readdir(dir);
if (!ent)
break;
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
continue;
char abort[300];
snprintf(abort, sizeof(abort), "/sys/fs/fuse/connections/%s/abort",
ent->d_name);
int fd = open(abort, O_WRONLY);
if (fd == -1) {
continue;
}
if (write(fd, abort, 1) < 0) {
}
close(fd);
}
closedir(dir);
} else {
}
while (waitpid(-1, status, __WALL) != pid) {
}
}
#define SYZ_HAVE_SETUP_LOOP 1
static void setup_loop()
{
int pid = getpid();
char cgroupdir[64];
char file[128];
snprintf(cgroupdir, sizeof(cgroupdir), "/syzcgroup/unified/syz%llu", procid);
if (mkdir(cgroupdir, 0777)) {
}
snprintf(file, sizeof(file), "%s/pids.max", cgroupdir);
if (!write_file(file, "32")) {
}
snprintf(file, sizeof(file), "%s/memory.low", cgroupdir);
if (!write_file(file, "%d", 198 << 20)) {
}
snprintf(file, sizeof(file), "%s/memory.high", cgroupdir);
if (!write_file(file, "%d", 199 << 20)) {
}
snprintf(file, sizeof(file), "%s/memory.max", cgroupdir);
if (!write_file(file, "%d", 200 << 20)) {
}
if (!write_file("/proc/self/oom_score_adj", "-1000")) {
}
snprintf(file, sizeof(file), "%s/cgroup.procs", cgroupdir);
if (!write_file(file, "%d", pid)) {
}
snprintf(cgroupdir, sizeof(cgroupdir), "/syzcgroup/cpu/syz%llu", procid);
if (mkdir(cgroupdir, 0777)) {
}
snprintf(file, sizeof(file), "%s/cgroup.procs", cgroupdir);
if (!write_file(file, "%d", pid)) {
}
snprintf(cgroupdir, sizeof(cgroupdir), "/syzcgroup/net/syz%llu", procid);
if (mkdir(cgroupdir, 0777)) {
}
snprintf(file, sizeof(file), "%s/cgroup.procs", cgroupdir);
if (!write_file(file, "%d", pid)) {
}
checkpoint_net_namespace();
}
#define SYZ_HAVE_RESET_LOOP 1
static void reset_loop()
{
char buf[64];
snprintf(buf, sizeof(buf), "/dev/loop%llu", procid);
int loopfd = open(buf, O_RDWR);
if (loopfd != -1) {
ioctl(loopfd, LOOP_CLR_FD, 0);
close(loopfd);
}
reset_net_namespace();
}
#define SYZ_HAVE_SETUP_TEST 1
static void setup_test()
{
prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
setpgrp();
char cgroupdir[64];
snprintf(cgroupdir, sizeof(cgroupdir), "/syzcgroup/unified/syz%llu", procid);
if (symlink(cgroupdir, "./cgroup")) {
}
snprintf(cgroupdir, sizeof(cgroupdir), "/syzcgroup/cpu/syz%llu", procid);
if (symlink(cgroupdir, "./cgroup.cpu")) {
}
snprintf(cgroupdir, sizeof(cgroupdir), "/syzcgroup/net/syz%llu", procid);
if (symlink(cgroupdir, "./cgroup.net")) {
}
if (!write_file("/proc/self/oom_score_adj", "0")) {
}
flush_tun();
}
#define SYZ_HAVE_RESET_TEST 1
static void reset_test()
{
int fd;
for (fd = 3; fd < 30; fd++)
close(fd);
}
struct thread_t {
int created, call;
event_t ready, done;
};
static struct thread_t threads[16];
static void execute_call(int call);
static int running;
static void* thr(void* arg)
{
struct thread_t* th = (struct thread_t*)arg;
for (;;) {
event_wait(&th->ready);
event_reset(&th->ready);
execute_call(th->call);
__atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED);
event_set(&th->done);
}
return 0;
}
static void execute_one(void)
{
int i, call, thread;
for (call = 0; call < 6; call++) {
for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0]));
thread++) {
struct thread_t* th = &threads[thread];
if (!th->created) {
th->created = 1;
event_init(&th->ready);
event_init(&th->done);
event_set(&th->done);
thread_start(thr, th);
}
if (!event_isset(&th->done))
continue;
event_reset(&th->done);
th->call = call;
__atomic_fetch_add(&running, 1, __ATOMIC_RELAXED);
event_set(&th->ready);
event_timedwait(&th->done, 45);
break;
}
}
for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++)
sleep_ms(1);
}
static void execute_one(void);
#define WAIT_FLAGS __WALL
static void loop(void)
{
setup_loop();
int iter;
for (iter = 0;; iter++) {
char cwdbuf[32];
sprintf(cwdbuf, "./%d", iter);
if (mkdir(cwdbuf, 0777))
exit(1);
reset_loop();
int pid = fork();
if (pid < 0)
exit(1);
if (pid == 0) {
if (chdir(cwdbuf))
exit(1);
setup_test();
execute_one();
reset_test();
exit(0);
}
int status = 0;
uint64_t start = current_time_ms();
for (;;) {
if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
break;
sleep_ms(1);
if (current_time_ms() - start < 5 * 1000)
continue;
kill_and_wait(pid, &status);
break;
}
remove_dir(cwdbuf);
}
}
uint64_t r[1] = {0xffffffffffffffff};
void execute_call(int call)
{
long res;
switch (call) {
case 0:
NONFAILING(memcpy((void*)0x20000100, "/dev/loop-control", 18));
res = syscall(__NR_openat, 0xffffffffffffff9c, 0x20000100, 0x40, 0);
if (res != -1)
r[0] = res;
break;
case 1:
syscall(__NR_ioctl, r[0], 0x4c81, 0);
break;
case 2:
NONFAILING(memcpy((void*)0x20000000, "f2fs", 5));
NONFAILING(memcpy((void*)0x20000100, "./file0", 8));
NONFAILING(*(uint64_t*)0x20000200 = 0x20010400);
NONFAILING(*(uint64_t*)0x20000208 = 0);
NONFAILING(*(uint64_t*)0x20000210 = 0x1400);
NONFAILING(*(uint8_t*)0x20016600 = 0);
syz_mount_image(0x20000000, 0x20000100, 0, 1, 0x20000200, 0, 0x20016600);
break;
case 3:
syscall(__NR_prctl, 0x3f);
break;
case 4:
syscall(__NR_ioctl, r[0], 0x4c80, 0);
break;
case 5:
NONFAILING(memcpy((void*)0x20000500, "IPVS", 5));
syz_genetlink_get_family_id(0x20000500);
break;
}
}
int main(void)
{
syscall(__NR_mmap, 0x20000000, 0x1000000, 3, 0x32, -1, 0);
install_segv_handler();
for (procid = 0; procid < 6; procid++) {
if (fork() == 0) {
use_temporary_dir();
do_sandbox_none();
}
}
sleep(1000000);
return 0;
}
|
the_stack_data/40763126.c | int BOB_MCBOB();
int main(int argc, char **argv) {
return BOB_MCBOB();
}
|
the_stack_data/1220167.c | #include<stdio.h>
int main() {
int InputNumber,count,flag,CountTwo;
printf ("Enter number:\n ") ;
scanf ("%d",&InputNumber);
printf ("Prime numbers from 2 to %d:\n",InputNumber);
for (count=2;count<=InputNumber;count++) {
flag=1;
for(CountTwo=2;CountTwo<count;CountTwo++) {
if (count==CountTwo) continue;
if (count%CountTwo==0) {flag=0; break;
}
}if(flag) printf("%d\n",count);
}
}
|
the_stack_data/50824.c | #define _FILE_OFFSET_BITS 64
#include <stdio.h> /* printf */
#include <unistd.h> /* usleep */
#include <string.h> /* strerror */
#include <getopt.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/mman.h>
#include <stdio.h>
#include <inttypes.h>
#include <unistd.h>
typedef char FOURCC[4];
typedef unsigned long int DWORD;
typedef unsigned int WORD;
typedef unsigned int UINT;
typedef unsigned long int DWORDLONG;
typedef unsigned long int LONG;
typedef unsigned char BYTE;
#define DIEonERR(value) if ((value)<0) { \
fprintf(stderr,"%i DIED in %s line %i with error %i\n",getpid(),__FILE__,__LINE__,-(value));exit(1);}
#define DIEif(cond,msg) if (cond) { \
fprintf(stderr,"%i DIED in %s line %i: %s (%i)\n",getpid(),__FILE__,__LINE__,msg,cond);exit(1);}
typedef struct
{
char header_fcc[4];
uint32_t file_cb;
char file_fcc[4];
} riffheader_t;
typedef struct
{
char fcc[4];
uint32_t cb;
} riffchunk_t;
typedef struct
{
char list_fcc[4];
uint32_t list_cb;
char type_fcc[4];
} rifflist_t;
typedef struct
{
char fcc[4];
uint32_t flags;
uint32_t off;
uint32_t cb;
} aviidxentry_t;
int
main (int argc, char **argv)
{
riffheader_t riffheader;
aviidxentry_t *idxe;
off_t pos;
union {
char buf[80];
riffchunk_t riffchunk;
rifflist_t rifflist;
} buf;
int err;
pos = ftello(stdin);
err = fread(&riffheader,sizeof(riffheader),1,stdin);
DIEif(err!=1,"cannot read riff header");
printf("RIFF-ID: '%c%c%c%c', fileType: '%c%c%c%c', fileSize: %u bytes (%lli .. %lli)\n",
riffheader.header_fcc[0], riffheader.header_fcc[1], riffheader.header_fcc[2], riffheader.header_fcc[3],
riffheader.file_fcc[0], riffheader.file_fcc[1], riffheader.file_fcc[2], riffheader.file_fcc[3],
riffheader.file_cb, (long long int)pos, (long long int)pos+8+riffheader.file_cb-1);
//DIEif(memcmp(riffheader.header_fcc,"RIFF",4),"no RIFF file");
if(memcmp(riffheader.header_fcc,"RIFF",4)!=0)
{
printf("WARNING: no RIFF file!\n");
err = fseek(stdin,0,SEEK_SET);
DIEif(err!=0,"cannot seek to begin of file");
}
while (!feof(stdin))
{
pos = ftello(stdin);
err = fread(&buf,sizeof(riffchunk_t),1,stdin);
DIEif(err!=1,"cannot read chunk header");
if( memcmp(buf.riffchunk.fcc,"LIST",4)==0 )
{
err = fread(buf.rifflist.type_fcc,sizeof(buf.rifflist.type_fcc),1,stdin);
DIEif(err!=1,"cannot read list type");
printf("- '%c%c%c%c', listType: '%c%c%c%c', listSize: %u bytes (%lli .. %lli)\n",
buf.rifflist.list_fcc[0], buf.rifflist.list_fcc[1], buf.rifflist.list_fcc[2], buf.rifflist.list_fcc[3],
buf.rifflist.type_fcc[0], buf.rifflist.type_fcc[1], buf.rifflist.type_fcc[2], buf.rifflist.type_fcc[3],
buf.rifflist.list_cb, (long long int)pos, (long long int)pos+8+buf.rifflist.list_cb-1);
}
else
{
#define BUFSZ 256
unsigned char cbuf[BUFSZ];
long buflen;
int i,l;
printf("- '%c%c%c%c', chunkSize: %u bytes (%llu .. %llu = 0x%llX .. 0x%llX)\n",
buf.riffchunk.fcc[0], buf.riffchunk.fcc[1], buf.riffchunk.fcc[2], buf.riffchunk.fcc[3],
buf.riffchunk.cb, (unsigned long long int)pos, (unsigned long long int)pos+8+buf.riffchunk.cb-1,
(unsigned long long int)pos, (unsigned long long int)pos+8+buf.riffchunk.cb-1 );
//pos = ftell(stdin);
buflen = BUFSZ<buf.riffchunk.cb?BUFSZ:buf.riffchunk.cb;
err = fread(cbuf,buflen,1,stdin);
DIEif(err!=1,"cannot read chunk");
printf(" ");
for(i=0;i<buflen;i++) {
printf("%c",cbuf[i]>=32 && cbuf[i]<127 ? cbuf[i] : '.');
if(i%4==3) printf(" ");
if(i%64==63) printf("\n ");
}
printf("\n ");
for(i=0;i<buflen;i++) {
printf("%02X ",cbuf[i]);
if(i%4==3) printf(" ");
if(i%16==15) printf("\n ");
}
printf("\n");
if ( memcmp(buf.riffchunk.fcc, "idx1", 4) == 0 ) {
l = buf.riffchunk.cb;
for(i=0;i< (buf.riffchunk.cb<BUFSZ?buf.riffchunk.cb:BUFSZ)/sizeof(aviidxentry_t);i++) {
idxe = (aviidxentry_t*) &cbuf[i * sizeof(aviidxentry_t)];
printf(" - '%c%c%c%c', chunkSize: %u bytes at %llu = 0x%llX\n",
idxe->fcc[0], idxe->fcc[1], idxe->fcc[2], idxe->fcc[3],
idxe->cb, (unsigned long long int)idxe->off, (unsigned long long int)idxe->off);
}
}
if( (buf.riffchunk.cb & 1) != 0) buf.riffchunk.cb++; // must be even
buf.riffchunk.cb-=buflen;
err = fseek(stdin,buf.riffchunk.cb,SEEK_CUR);
DIEif(err!=0,"cannot seek to end of chunk");
}
}
return 0;
}
|
the_stack_data/116319.c | /*
* Exercise 3-5:
* Write function that converts integer n into base b char representation in string s.
* In particular, itob(n,s,16) formats n as hex integer in s.
*/
#include <stdio.h>
#include <string.h>
void reverse(char s[]);
void itob(int n,char s[], int b);
int main()
{
int n1 = 32;
char s1[50];
itob(n1, s1, 16);
printf("%d, %s\n\n", n1, s1);
return 0;
}
void reverse(char s[])
{
int i = 0, j = 0;
char c;
for (i = 0, j = strlen(s)-1; i < j; i++, j--)
c = s[i], s[i] = s[j], s[j] = c;
}
void itob(int n,char s[], int b)
{
int i, sign;
if ((sign = n) < 0) // record sign
n = -n; // make n positive
i = 0;
do { // generate digits in reverse order
s[i++] = n % b + '0'; // get next digit
} while (n /= b); // delete it
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}
|
the_stack_data/8923.c | #include <stdio.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/un.h>
#include <stdlib.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define LISTEN_BACKLOG 50
#define BUFFER_SIZE 7
int main(int argc, char *argv[])
{
char *ip_address = argv[1];
// asci to int
int port = atoi(argv[2]);
// tcp socket for server & client
int sock;
struct sockaddr_in server_addr;
printf("Starting alrien simple key value store...\n");
printf("IP ADDRESS: %s\n", ip_address);
printf("PORT: %d\n", port);
// setup socket
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
printf("Error opening socket: -1\n");
exit(1);
}
server_addr.sin_port = htons(port);
inet_aton(ip_address, &server_addr.sin_addr);
server_addr.sin_family = AF_INET;
// bind socket to port & ip_address
if (bind(sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
printf("Error binding socket to port & ip_address: %d, %s\n", port, ip_address);
exit(1);
}
// list on port & ip_address
if (listen(sock, LISTEN_BACKLOG) == -1) {
printf("Error listing on port & ip_address: %d, %s\n", port, ip_address);
}
while(1) {
printf("Checking if connections are coming in ...\n");
int session, datasize, r, w;
char buffer[BUFFER_SIZE];
struct sockaddr_in client_addr;
socklen_t client_addr_size;
//put buffer full of zero's
bzero(buffer,BUFFER_SIZE);
client_addr_size = sizeof(struct sockaddr_in);
session = accept(sock, (struct sockaddr*)&client_addr, &client_addr_size);
if (session == -1) {
printf("Connection failed or no connections!");
continue;
}
printf("Accepting connection from %s:%d\n", inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
//read first 7 chars from string
read(session,&datasize,sizeof(datasize));
r = read(session,buffer,BUFFER_SIZE);
if (r < 0) {
printf("Failed to read from socket!");
continue;
}
//display the message from client
printf("Here is the message: %s, %d\n",buffer,ntohs(datasize));
//write to client
w = write(session,"I got your message",18);
if (w < 0) {
printf("Failed to write to socket!");
continue;
}
//close session socket
close(session);
}
//close server socket
close(sock);
return 0;
}
|
the_stack_data/38266.c | /* -----------------------------------------------------------------------------
*
* (c) The GHC Team, 2001
* Author: Sungwoo Park
*
* Retainer profiling.
*
* ---------------------------------------------------------------------------*/
#ifdef PROFILING
// Turn off inlining when debugging - it obfuscates things
#ifdef DEBUG
#define INLINE
#else
#define INLINE inline
#endif
#include "PosixSource.h"
#include "Rts.h"
#include "RtsUtils.h"
#include "RetainerProfile.h"
#include "RetainerSet.h"
#include "Schedule.h"
#include "Printer.h"
#include "Weak.h"
#include "sm/Sanity.h"
#include "Profiling.h"
#include "Stats.h"
#include "ProfHeap.h"
#include "Apply.h"
#include "Stable.h" /* markStableTables */
#include "sm/Storage.h" // for END_OF_STATIC_LIST
/*
Note: what to change in order to plug-in a new retainer profiling scheme?
(1) type retainer in ../includes/StgRetainerProf.h
(2) retainer function R(), i.e., getRetainerFrom()
(3) the two hashing functions, hashKeySingleton() and hashKeyAddElement(),
in RetainerSet.h, if needed.
(4) printRetainer() and printRetainerSetShort() in RetainerSet.c.
*/
/* -----------------------------------------------------------------------------
* Declarations...
* -------------------------------------------------------------------------- */
static nat retainerGeneration; // generation
static nat numObjectVisited; // total number of objects visited
static nat timesAnyObjectVisited; // number of times any objects are visited
/*
The rs field in the profile header of any object points to its retainer
set in an indirect way: if flip is 0, it points to the retainer set;
if flip is 1, it points to the next byte after the retainer set (even
for NULL pointers). Therefore, with flip 1, (rs ^ 1) is the actual
pointer. See retainerSetOf().
*/
StgWord flip = 0; // flip bit
// must be 0 if DEBUG_RETAINER is on (for static closures)
#define setRetainerSetToNull(c) \
(c)->header.prof.hp.rs = (RetainerSet *)((StgWord)NULL | flip)
static void retainStack(StgClosure *, retainer, StgPtr, StgPtr);
static void retainClosure(StgClosure *, StgClosure *, retainer);
#ifdef DEBUG_RETAINER
static void belongToHeap(StgPtr p);
#endif
#ifdef DEBUG_RETAINER
/*
cStackSize records how many times retainStack() has been invoked recursively,
that is, the number of activation records for retainStack() on the C stack.
maxCStackSize records its max value.
Invariants:
cStackSize <= maxCStackSize
*/
static nat cStackSize, maxCStackSize;
static nat sumOfNewCost; // sum of the cost of each object, computed
// when the object is first visited
static nat sumOfNewCostExtra; // for those objects not visited during
// retainer profiling, e.g., MUT_VAR
static nat costArray[N_CLOSURE_TYPES];
nat sumOfCostLinear; // sum of the costs of all object, computed
// when linearly traversing the heap after
// retainer profiling
nat costArrayLinear[N_CLOSURE_TYPES];
#endif
/* -----------------------------------------------------------------------------
* Retainer stack - header
* Note:
* Although the retainer stack implementation could be separated *
* from the retainer profiling engine, there does not seem to be
* any advantage in doing that; retainer stack is an integral part
* of retainer profiling engine and cannot be use elsewhere at
* all.
* -------------------------------------------------------------------------- */
typedef enum {
posTypeStep,
posTypePtrs,
posTypeSRT,
posTypeLargeSRT,
} nextPosType;
typedef union {
// fixed layout or layout specified by a field in the closure
StgWord step;
// layout.payload
struct {
// See StgClosureInfo in InfoTables.h
#if SIZEOF_VOID_P == 8
StgWord32 pos;
StgWord32 ptrs;
#else
StgWord16 pos;
StgWord16 ptrs;
#endif
StgPtr payload;
} ptrs;
// SRT
struct {
StgClosure **srt;
StgWord srt_bitmap;
} srt;
// Large SRT
struct {
StgLargeSRT *srt;
StgWord offset;
} large_srt;
} nextPos;
typedef struct {
nextPosType type;
nextPos next;
} stackPos;
typedef struct {
StgClosure *c;
retainer c_child_r;
stackPos info;
} stackElement;
/*
Invariants:
firstStack points to the first block group.
currentStack points to the block group currently being used.
currentStack->free == stackLimit.
stackTop points to the topmost byte in the stack of currentStack.
Unless the whole stack is empty, stackTop must point to the topmost
object (or byte) in the whole stack. Thus, it is only when the whole stack
is empty that stackTop == stackLimit (not during the execution of push()
and pop()).
stackBottom == currentStack->start.
stackLimit == currentStack->start + BLOCK_SIZE_W * currentStack->blocks.
Note:
When a current stack becomes empty, stackTop is set to point to
the topmost element on the previous block group so as to satisfy
the invariants described above.
*/
static bdescr *firstStack = NULL;
static bdescr *currentStack;
static stackElement *stackBottom, *stackTop, *stackLimit;
/*
currentStackBoundary is used to mark the current stack chunk.
If stackTop == currentStackBoundary, it means that the current stack chunk
is empty. It is the responsibility of the user to keep currentStackBoundary
valid all the time if it is to be employed.
*/
static stackElement *currentStackBoundary;
/*
stackSize records the current size of the stack.
maxStackSize records its high water mark.
Invariants:
stackSize <= maxStackSize
Note:
stackSize is just an estimate measure of the depth of the graph. The reason
is that some heap objects have only a single child and may not result
in a new element being pushed onto the stack. Therefore, at the end of
retainer profiling, maxStackSize + maxCStackSize is some value no greater
than the actual depth of the graph.
*/
#ifdef DEBUG_RETAINER
static int stackSize, maxStackSize;
#endif
// number of blocks allocated for one stack
#define BLOCKS_IN_STACK 1
/* -----------------------------------------------------------------------------
* Add a new block group to the stack.
* Invariants:
* currentStack->link == s.
* -------------------------------------------------------------------------- */
static INLINE void
newStackBlock( bdescr *bd )
{
currentStack = bd;
stackTop = (stackElement *)(bd->start + BLOCK_SIZE_W * bd->blocks);
stackBottom = (stackElement *)bd->start;
stackLimit = (stackElement *)stackTop;
bd->free = (StgPtr)stackLimit;
}
/* -----------------------------------------------------------------------------
* Return to the previous block group.
* Invariants:
* s->link == currentStack.
* -------------------------------------------------------------------------- */
static INLINE void
returnToOldStack( bdescr *bd )
{
currentStack = bd;
stackTop = (stackElement *)bd->free;
stackBottom = (stackElement *)bd->start;
stackLimit = (stackElement *)(bd->start + BLOCK_SIZE_W * bd->blocks);
bd->free = (StgPtr)stackLimit;
}
/* -----------------------------------------------------------------------------
* Initializes the traverse stack.
* -------------------------------------------------------------------------- */
static void
initializeTraverseStack( void )
{
if (firstStack != NULL) {
freeChain(firstStack);
}
firstStack = allocGroup(BLOCKS_IN_STACK);
firstStack->link = NULL;
firstStack->u.back = NULL;
newStackBlock(firstStack);
}
/* -----------------------------------------------------------------------------
* Frees all the block groups in the traverse stack.
* Invariants:
* firstStack != NULL
* -------------------------------------------------------------------------- */
static void
closeTraverseStack( void )
{
freeChain(firstStack);
firstStack = NULL;
}
/* -----------------------------------------------------------------------------
* Returns rtsTrue if the whole stack is empty.
* -------------------------------------------------------------------------- */
static INLINE rtsBool
isEmptyRetainerStack( void )
{
return (firstStack == currentStack) && stackTop == stackLimit;
}
/* -----------------------------------------------------------------------------
* Returns size of stack
* -------------------------------------------------------------------------- */
#ifdef DEBUG
W_
retainerStackBlocks( void )
{
bdescr* bd;
W_ res = 0;
for (bd = firstStack; bd != NULL; bd = bd->link)
res += bd->blocks;
return res;
}
#endif
/* -----------------------------------------------------------------------------
* Returns rtsTrue if stackTop is at the stack boundary of the current stack,
* i.e., if the current stack chunk is empty.
* -------------------------------------------------------------------------- */
static INLINE rtsBool
isOnBoundary( void )
{
return stackTop == currentStackBoundary;
}
/* -----------------------------------------------------------------------------
* Initializes *info from ptrs and payload.
* Invariants:
* payload[] begins with ptrs pointers followed by non-pointers.
* -------------------------------------------------------------------------- */
static INLINE void
init_ptrs( stackPos *info, nat ptrs, StgPtr payload )
{
info->type = posTypePtrs;
info->next.ptrs.pos = 0;
info->next.ptrs.ptrs = ptrs;
info->next.ptrs.payload = payload;
}
/* -----------------------------------------------------------------------------
* Find the next object from *info.
* -------------------------------------------------------------------------- */
static INLINE StgClosure *
find_ptrs( stackPos *info )
{
if (info->next.ptrs.pos < info->next.ptrs.ptrs) {
return (StgClosure *)info->next.ptrs.payload[info->next.ptrs.pos++];
} else {
return NULL;
}
}
/* -----------------------------------------------------------------------------
* Initializes *info from SRT information stored in *infoTable.
* -------------------------------------------------------------------------- */
static INLINE void
init_srt_fun( stackPos *info, StgFunInfoTable *infoTable )
{
if (infoTable->i.srt_bitmap == (StgHalfWord)(-1)) {
info->type = posTypeLargeSRT;
info->next.large_srt.srt = (StgLargeSRT *)GET_FUN_SRT(infoTable);
info->next.large_srt.offset = 0;
} else {
info->type = posTypeSRT;
info->next.srt.srt = (StgClosure **)GET_FUN_SRT(infoTable);
info->next.srt.srt_bitmap = infoTable->i.srt_bitmap;
}
}
static INLINE void
init_srt_thunk( stackPos *info, StgThunkInfoTable *infoTable )
{
if (infoTable->i.srt_bitmap == (StgHalfWord)(-1)) {
info->type = posTypeLargeSRT;
info->next.large_srt.srt = (StgLargeSRT *)GET_SRT(infoTable);
info->next.large_srt.offset = 0;
} else {
info->type = posTypeSRT;
info->next.srt.srt = (StgClosure **)GET_SRT(infoTable);
info->next.srt.srt_bitmap = infoTable->i.srt_bitmap;
}
}
/* -----------------------------------------------------------------------------
* Find the next object from *info.
* -------------------------------------------------------------------------- */
static INLINE StgClosure *
find_srt( stackPos *info )
{
StgClosure *c;
StgWord bitmap;
if (info->type == posTypeSRT) {
// Small SRT bitmap
bitmap = info->next.srt.srt_bitmap;
while (bitmap != 0) {
if ((bitmap & 1) != 0) {
#if defined(COMPILING_WINDOWS_DLL)
if ((unsigned long)(*(info->next.srt.srt)) & 0x1)
c = (* (StgClosure **)((unsigned long)*(info->next.srt.srt)) & ~0x1);
else
c = *(info->next.srt.srt);
#else
c = *(info->next.srt.srt);
#endif
bitmap = bitmap >> 1;
info->next.srt.srt++;
info->next.srt.srt_bitmap = bitmap;
return c;
}
bitmap = bitmap >> 1;
info->next.srt.srt++;
}
// bitmap is now zero...
return NULL;
}
else {
// Large SRT bitmap
nat i = info->next.large_srt.offset;
StgWord bitmap;
// Follow the pattern from GC.c:scavenge_large_srt_bitmap().
bitmap = info->next.large_srt.srt->l.bitmap[i / BITS_IN(W_)];
bitmap = bitmap >> (i % BITS_IN(StgWord));
while (i < info->next.large_srt.srt->l.size) {
if ((bitmap & 1) != 0) {
c = ((StgClosure **)info->next.large_srt.srt->srt)[i];
i++;
info->next.large_srt.offset = i;
return c;
}
i++;
if (i % BITS_IN(W_) == 0) {
bitmap = info->next.large_srt.srt->l.bitmap[i / BITS_IN(W_)];
} else {
bitmap = bitmap >> 1;
}
}
// reached the end of this bitmap.
info->next.large_srt.offset = i;
return NULL;
}
}
/* -----------------------------------------------------------------------------
* push() pushes a stackElement representing the next child of *c
* onto the traverse stack. If *c has no child, *first_child is set
* to NULL and nothing is pushed onto the stack. If *c has only one
* child, *c_chlid is set to that child and nothing is pushed onto
* the stack. If *c has more than two children, *first_child is set
* to the first child and a stackElement representing the second
* child is pushed onto the stack.
* Invariants:
* *c_child_r is the most recent retainer of *c's children.
* *c is not any of TSO, AP, PAP, AP_STACK, which means that
* there cannot be any stack objects.
* Note: SRTs are considered to be children as well.
* -------------------------------------------------------------------------- */
static INLINE void
push( StgClosure *c, retainer c_child_r, StgClosure **first_child )
{
stackElement se;
bdescr *nbd; // Next Block Descriptor
#ifdef DEBUG_RETAINER
// debugBelch("push(): stackTop = 0x%x, currentStackBoundary = 0x%x\n", stackTop, currentStackBoundary);
#endif
ASSERT(get_itbl(c)->type != TSO);
ASSERT(get_itbl(c)->type != AP_STACK);
//
// fill in se
//
se.c = c;
se.c_child_r = c_child_r;
// fill in se.info
switch (get_itbl(c)->type) {
// no child, no SRT
case CONSTR_0_1:
case CONSTR_0_2:
case ARR_WORDS:
*first_child = NULL;
return;
// one child (fixed), no SRT
case MUT_VAR_CLEAN:
case MUT_VAR_DIRTY:
*first_child = ((StgMutVar *)c)->var;
return;
case THUNK_SELECTOR:
*first_child = ((StgSelector *)c)->selectee;
return;
case IND_PERM:
case BLACKHOLE:
*first_child = ((StgInd *)c)->indirectee;
return;
case CONSTR_1_0:
case CONSTR_1_1:
*first_child = c->payload[0];
return;
// For CONSTR_2_0 and MVAR, we use se.info.step to record the position
// of the next child. We do not write a separate initialization code.
// Also we do not have to initialize info.type;
// two children (fixed), no SRT
// need to push a stackElement, but nothing to store in se.info
case CONSTR_2_0:
*first_child = c->payload[0]; // return the first pointer
// se.info.type = posTypeStep;
// se.info.next.step = 2; // 2 = second
break;
// three children (fixed), no SRT
// need to push a stackElement
case MVAR_CLEAN:
case MVAR_DIRTY:
// head must be TSO and the head of a linked list of TSOs.
// Shoule it be a child? Seems to be yes.
*first_child = (StgClosure *)((StgMVar *)c)->head;
// se.info.type = posTypeStep;
se.info.next.step = 2; // 2 = second
break;
// three children (fixed), no SRT
case WEAK:
*first_child = ((StgWeak *)c)->key;
// se.info.type = posTypeStep;
se.info.next.step = 2;
break;
// layout.payload.ptrs, no SRT
case TVAR:
case CONSTR:
case PRIM:
case MUT_PRIM:
case BCO:
case CONSTR_STATIC:
init_ptrs(&se.info, get_itbl(c)->layout.payload.ptrs,
(StgPtr)c->payload);
*first_child = find_ptrs(&se.info);
if (*first_child == NULL)
return; // no child
break;
// StgMutArrPtr.ptrs, no SRT
case MUT_ARR_PTRS_CLEAN:
case MUT_ARR_PTRS_DIRTY:
case MUT_ARR_PTRS_FROZEN:
case MUT_ARR_PTRS_FROZEN0:
init_ptrs(&se.info, ((StgMutArrPtrs *)c)->ptrs,
(StgPtr)(((StgMutArrPtrs *)c)->payload));
*first_child = find_ptrs(&se.info);
if (*first_child == NULL)
return;
break;
// StgMutArrPtr.ptrs, no SRT
case SMALL_MUT_ARR_PTRS_CLEAN:
case SMALL_MUT_ARR_PTRS_DIRTY:
case SMALL_MUT_ARR_PTRS_FROZEN:
case SMALL_MUT_ARR_PTRS_FROZEN0:
init_ptrs(&se.info, ((StgSmallMutArrPtrs *)c)->ptrs,
(StgPtr)(((StgSmallMutArrPtrs *)c)->payload));
*first_child = find_ptrs(&se.info);
if (*first_child == NULL)
return;
break;
// layout.payload.ptrs, SRT
case FUN: // *c is a heap object.
case FUN_2_0:
init_ptrs(&se.info, get_itbl(c)->layout.payload.ptrs, (StgPtr)c->payload);
*first_child = find_ptrs(&se.info);
if (*first_child == NULL)
// no child from ptrs, so check SRT
goto fun_srt_only;
break;
case THUNK:
case THUNK_2_0:
init_ptrs(&se.info, get_itbl(c)->layout.payload.ptrs,
(StgPtr)((StgThunk *)c)->payload);
*first_child = find_ptrs(&se.info);
if (*first_child == NULL)
// no child from ptrs, so check SRT
goto thunk_srt_only;
break;
// 1 fixed child, SRT
case FUN_1_0:
case FUN_1_1:
*first_child = c->payload[0];
ASSERT(*first_child != NULL);
init_srt_fun(&se.info, get_fun_itbl(c));
break;
case THUNK_1_0:
case THUNK_1_1:
*first_child = ((StgThunk *)c)->payload[0];
ASSERT(*first_child != NULL);
init_srt_thunk(&se.info, get_thunk_itbl(c));
break;
case FUN_STATIC: // *c is a heap object.
ASSERT(get_itbl(c)->srt_bitmap != 0);
case FUN_0_1:
case FUN_0_2:
fun_srt_only:
init_srt_fun(&se.info, get_fun_itbl(c));
*first_child = find_srt(&se.info);
if (*first_child == NULL)
return; // no child
break;
// SRT only
case THUNK_STATIC:
ASSERT(get_itbl(c)->srt_bitmap != 0);
case THUNK_0_1:
case THUNK_0_2:
thunk_srt_only:
init_srt_thunk(&se.info, get_thunk_itbl(c));
*first_child = find_srt(&se.info);
if (*first_child == NULL)
return; // no child
break;
case TREC_CHUNK:
*first_child = (StgClosure *)((StgTRecChunk *)c)->prev_chunk;
se.info.next.step = 0; // entry no.
break;
// cannot appear
case PAP:
case AP:
case AP_STACK:
case TSO:
case STACK:
case IND_STATIC:
case CONSTR_NOCAF_STATIC:
// stack objects
case UPDATE_FRAME:
case CATCH_FRAME:
case UNDERFLOW_FRAME:
case STOP_FRAME:
case RET_BCO:
case RET_SMALL:
case RET_BIG:
// invalid objects
case IND:
case INVALID_OBJECT:
default:
barf("Invalid object *c in push()");
return;
}
if (stackTop - 1 < stackBottom) {
#ifdef DEBUG_RETAINER
// debugBelch("push() to the next stack.\n");
#endif
// currentStack->free is updated when the active stack is switched
// to the next stack.
currentStack->free = (StgPtr)stackTop;
if (currentStack->link == NULL) {
nbd = allocGroup(BLOCKS_IN_STACK);
nbd->link = NULL;
nbd->u.back = currentStack;
currentStack->link = nbd;
} else
nbd = currentStack->link;
newStackBlock(nbd);
}
// adjust stackTop (acutal push)
stackTop--;
// If the size of stackElement was huge, we would better replace the
// following statement by either a memcpy() call or a switch statement
// on the type of the element. Currently, the size of stackElement is
// small enough (5 words) that this direct assignment seems to be enough.
// ToDo: The line below leads to the warning:
// warning: 'se.info.type' may be used uninitialized in this function
// This is caused by the fact that there are execution paths through the
// large switch statement above where some cases do not initialize this
// field. Is this really harmless? Can we avoid the warning?
*stackTop = se;
#ifdef DEBUG_RETAINER
stackSize++;
if (stackSize > maxStackSize) maxStackSize = stackSize;
// ASSERT(stackSize >= 0);
// debugBelch("stackSize = %d\n", stackSize);
#endif
}
/* -----------------------------------------------------------------------------
* popOff() and popOffReal(): Pop a stackElement off the traverse stack.
* Invariants:
* stackTop cannot be equal to stackLimit unless the whole stack is
* empty, in which case popOff() is not allowed.
* Note:
* You can think of popOffReal() as a part of popOff() which is
* executed at the end of popOff() in necessary. Since popOff() is
* likely to be executed quite often while popOffReal() is not, we
* separate popOffReal() from popOff(), which is declared as an
* INLINE function (for the sake of execution speed). popOffReal()
* is called only within popOff() and nowhere else.
* -------------------------------------------------------------------------- */
static void
popOffReal(void)
{
bdescr *pbd; // Previous Block Descriptor
#ifdef DEBUG_RETAINER
// debugBelch("pop() to the previous stack.\n");
#endif
ASSERT(stackTop + 1 == stackLimit);
ASSERT(stackBottom == (stackElement *)currentStack->start);
if (firstStack == currentStack) {
// The stack is completely empty.
stackTop++;
ASSERT(stackTop == stackLimit);
#ifdef DEBUG_RETAINER
stackSize--;
if (stackSize > maxStackSize) maxStackSize = stackSize;
/*
ASSERT(stackSize >= 0);
debugBelch("stackSize = %d\n", stackSize);
*/
#endif
return;
}
// currentStack->free is updated when the active stack is switched back
// to the previous stack.
currentStack->free = (StgPtr)stackLimit;
// find the previous block descriptor
pbd = currentStack->u.back;
ASSERT(pbd != NULL);
returnToOldStack(pbd);
#ifdef DEBUG_RETAINER
stackSize--;
if (stackSize > maxStackSize) maxStackSize = stackSize;
/*
ASSERT(stackSize >= 0);
debugBelch("stackSize = %d\n", stackSize);
*/
#endif
}
static INLINE void
popOff(void) {
#ifdef DEBUG_RETAINER
// debugBelch("\tpopOff(): stackTop = 0x%x, currentStackBoundary = 0x%x\n", stackTop, currentStackBoundary);
#endif
ASSERT(stackTop != stackLimit);
ASSERT(!isEmptyRetainerStack());
// <= (instead of <) is wrong!
if (stackTop + 1 < stackLimit) {
stackTop++;
#ifdef DEBUG_RETAINER
stackSize--;
if (stackSize > maxStackSize) maxStackSize = stackSize;
/*
ASSERT(stackSize >= 0);
debugBelch("stackSize = %d\n", stackSize);
*/
#endif
return;
}
popOffReal();
}
/* -----------------------------------------------------------------------------
* Finds the next object to be considered for retainer profiling and store
* its pointer to *c.
* Test if the topmost stack element indicates that more objects are left,
* and if so, retrieve the first object and store its pointer to *c. Also,
* set *cp and *r appropriately, both of which are stored in the stack element.
* The topmost stack element then is overwritten so as for it to now denote
* the next object.
* If the topmost stack element indicates no more objects are left, pop
* off the stack element until either an object can be retrieved or
* the current stack chunk becomes empty, indicated by rtsTrue returned by
* isOnBoundary(), in which case *c is set to NULL.
* Note:
* It is okay to call this function even when the current stack chunk
* is empty.
* -------------------------------------------------------------------------- */
static INLINE void
pop( StgClosure **c, StgClosure **cp, retainer *r )
{
stackElement *se;
#ifdef DEBUG_RETAINER
// debugBelch("pop(): stackTop = 0x%x, currentStackBoundary = 0x%x\n", stackTop, currentStackBoundary);
#endif
do {
if (isOnBoundary()) { // if the current stack chunk is depleted
*c = NULL;
return;
}
se = stackTop;
switch (get_itbl(se->c)->type) {
// two children (fixed), no SRT
// nothing in se.info
case CONSTR_2_0:
*c = se->c->payload[1];
*cp = se->c;
*r = se->c_child_r;
popOff();
return;
// three children (fixed), no SRT
// need to push a stackElement
case MVAR_CLEAN:
case MVAR_DIRTY:
if (se->info.next.step == 2) {
*c = (StgClosure *)((StgMVar *)se->c)->tail;
se->info.next.step++; // move to the next step
// no popOff
} else {
*c = ((StgMVar *)se->c)->value;
popOff();
}
*cp = se->c;
*r = se->c_child_r;
return;
// three children (fixed), no SRT
case WEAK:
if (se->info.next.step == 2) {
*c = ((StgWeak *)se->c)->value;
se->info.next.step++;
// no popOff
} else {
*c = ((StgWeak *)se->c)->finalizer;
popOff();
}
*cp = se->c;
*r = se->c_child_r;
return;
case TREC_CHUNK: {
// These are pretty complicated: we have N entries, each
// of which contains 3 fields that we want to follow. So
// we divide the step counter: the 2 low bits indicate
// which field, and the rest of the bits indicate the
// entry number (starting from zero).
TRecEntry *entry;
nat entry_no = se->info.next.step >> 2;
nat field_no = se->info.next.step & 3;
if (entry_no == ((StgTRecChunk *)se->c)->next_entry_idx) {
*c = NULL;
popOff();
return;
}
entry = &((StgTRecChunk *)se->c)->entries[entry_no];
if (field_no == 0) {
*c = (StgClosure *)entry->tvar;
} else if (field_no == 1) {
*c = entry->expected_value;
} else {
*c = entry->new_value;
}
*cp = se->c;
*r = se->c_child_r;
se->info.next.step++;
return;
}
case TVAR:
case CONSTR:
case PRIM:
case MUT_PRIM:
case BCO:
case CONSTR_STATIC:
// StgMutArrPtr.ptrs, no SRT
case MUT_ARR_PTRS_CLEAN:
case MUT_ARR_PTRS_DIRTY:
case MUT_ARR_PTRS_FROZEN:
case MUT_ARR_PTRS_FROZEN0:
*c = find_ptrs(&se->info);
if (*c == NULL) {
popOff();
break;
}
*cp = se->c;
*r = se->c_child_r;
return;
// layout.payload.ptrs, SRT
case FUN: // always a heap object
case FUN_2_0:
if (se->info.type == posTypePtrs) {
*c = find_ptrs(&se->info);
if (*c != NULL) {
*cp = se->c;
*r = se->c_child_r;
return;
}
init_srt_fun(&se->info, get_fun_itbl(se->c));
}
goto do_srt;
case THUNK:
case THUNK_2_0:
if (se->info.type == posTypePtrs) {
*c = find_ptrs(&se->info);
if (*c != NULL) {
*cp = se->c;
*r = se->c_child_r;
return;
}
init_srt_thunk(&se->info, get_thunk_itbl(se->c));
}
goto do_srt;
// SRT
do_srt:
case THUNK_STATIC:
case FUN_STATIC:
case FUN_0_1:
case FUN_0_2:
case THUNK_0_1:
case THUNK_0_2:
case FUN_1_0:
case FUN_1_1:
case THUNK_1_0:
case THUNK_1_1:
*c = find_srt(&se->info);
if (*c != NULL) {
*cp = se->c;
*r = se->c_child_r;
return;
}
popOff();
break;
// no child (fixed), no SRT
case CONSTR_0_1:
case CONSTR_0_2:
case ARR_WORDS:
// one child (fixed), no SRT
case MUT_VAR_CLEAN:
case MUT_VAR_DIRTY:
case THUNK_SELECTOR:
case IND_PERM:
case CONSTR_1_1:
// cannot appear
case PAP:
case AP:
case AP_STACK:
case TSO:
case STACK:
case IND_STATIC:
case CONSTR_NOCAF_STATIC:
// stack objects
case UPDATE_FRAME:
case CATCH_FRAME:
case UNDERFLOW_FRAME:
case STOP_FRAME:
case RET_BCO:
case RET_SMALL:
case RET_BIG:
// invalid objects
case IND:
case INVALID_OBJECT:
default:
barf("Invalid object *c in pop()");
return;
}
} while (rtsTrue);
}
/* -----------------------------------------------------------------------------
* RETAINER PROFILING ENGINE
* -------------------------------------------------------------------------- */
void
initRetainerProfiling( void )
{
initializeAllRetainerSet();
retainerGeneration = 0;
}
/* -----------------------------------------------------------------------------
* This function must be called before f-closing prof_file.
* -------------------------------------------------------------------------- */
void
endRetainerProfiling( void )
{
#ifdef SECOND_APPROACH
outputAllRetainerSet(prof_file);
#endif
}
/* -----------------------------------------------------------------------------
* Returns the actual pointer to the retainer set of the closure *c.
* It may adjust RSET(c) subject to flip.
* Side effects:
* RSET(c) is initialized to NULL if its current value does not
* conform to flip.
* Note:
* Even though this function has side effects, they CAN be ignored because
* subsequent calls to retainerSetOf() always result in the same return value
* and retainerSetOf() is the only way to retrieve retainerSet of a given
* closure.
* We have to perform an XOR (^) operation each time a closure is examined.
* The reason is that we do not know when a closure is visited last.
* -------------------------------------------------------------------------- */
static INLINE void
maybeInitRetainerSet( StgClosure *c )
{
if (!isRetainerSetFieldValid(c)) {
setRetainerSetToNull(c);
}
}
/* -----------------------------------------------------------------------------
* Returns rtsTrue if *c is a retainer.
* -------------------------------------------------------------------------- */
static INLINE rtsBool
isRetainer( StgClosure *c )
{
switch (get_itbl(c)->type) {
//
// True case
//
// TSOs MUST be retainers: they constitute the set of roots.
case TSO:
case STACK:
// mutable objects
case MUT_PRIM:
case MVAR_CLEAN:
case MVAR_DIRTY:
case TVAR:
case MUT_VAR_CLEAN:
case MUT_VAR_DIRTY:
case MUT_ARR_PTRS_CLEAN:
case MUT_ARR_PTRS_DIRTY:
// thunks are retainers.
case THUNK:
case THUNK_1_0:
case THUNK_0_1:
case THUNK_2_0:
case THUNK_1_1:
case THUNK_0_2:
case THUNK_SELECTOR:
case AP:
case AP_STACK:
// Static thunks, or CAFS, are obviously retainers.
case THUNK_STATIC:
// WEAK objects are roots; there is separate code in which traversing
// begins from WEAK objects.
case WEAK:
return rtsTrue;
//
// False case
//
// constructors
case CONSTR:
case CONSTR_1_0:
case CONSTR_0_1:
case CONSTR_2_0:
case CONSTR_1_1:
case CONSTR_0_2:
// functions
case FUN:
case FUN_1_0:
case FUN_0_1:
case FUN_2_0:
case FUN_1_1:
case FUN_0_2:
// partial applications
case PAP:
// indirection
case IND_PERM:
// IND_STATIC used to be an error, but at the moment it can happen
// as isAlive doesn't look through IND_STATIC as it ignores static
// closures. See trac #3956 for a program that hit this error.
case IND_STATIC:
case BLACKHOLE:
// static objects
case CONSTR_STATIC:
case FUN_STATIC:
// misc
case PRIM:
case BCO:
case ARR_WORDS:
// STM
case TREC_CHUNK:
// immutable arrays
case MUT_ARR_PTRS_FROZEN:
case MUT_ARR_PTRS_FROZEN0:
return rtsFalse;
//
// Error case
//
// CONSTR_NOCAF_STATIC
// cannot be *c, *cp, *r in the retainer profiling loop.
case CONSTR_NOCAF_STATIC:
// Stack objects are invalid because they are never treated as
// legal objects during retainer profiling.
case UPDATE_FRAME:
case CATCH_FRAME:
case UNDERFLOW_FRAME:
case STOP_FRAME:
case RET_BCO:
case RET_SMALL:
case RET_BIG:
// other cases
case IND:
case INVALID_OBJECT:
default:
barf("Invalid object in isRetainer(): %d", get_itbl(c)->type);
return rtsFalse;
}
}
/* -----------------------------------------------------------------------------
* Returns the retainer function value for the closure *c, i.e., R(*c).
* This function does NOT return the retainer(s) of *c.
* Invariants:
* *c must be a retainer.
* Note:
* Depending on the definition of this function, the maintenance of retainer
* sets can be made easier. If most retainer sets are likely to be created
* again across garbage collections, refreshAllRetainerSet() in
* RetainerSet.c can simply do nothing.
* If this is not the case, we can free all the retainer sets and
* re-initialize the hash table.
* See refreshAllRetainerSet() in RetainerSet.c.
* -------------------------------------------------------------------------- */
static INLINE retainer
getRetainerFrom( StgClosure *c )
{
ASSERT(isRetainer(c));
#if defined(RETAINER_SCHEME_INFO)
// Retainer scheme 1: retainer = info table
return get_itbl(c);
#elif defined(RETAINER_SCHEME_CCS)
// Retainer scheme 2: retainer = cost centre stack
return c->header.prof.ccs;
#elif defined(RETAINER_SCHEME_CC)
// Retainer scheme 3: retainer = cost centre
return c->header.prof.ccs->cc;
#endif
}
/* -----------------------------------------------------------------------------
* Associates the retainer set *s with the closure *c, that is, *s becomes
* the retainer set of *c.
* Invariants:
* c != NULL
* s != NULL
* -------------------------------------------------------------------------- */
static INLINE void
associate( StgClosure *c, RetainerSet *s )
{
// StgWord has the same size as pointers, so the following type
// casting is okay.
RSET(c) = (RetainerSet *)((StgWord)s | flip);
}
/* -----------------------------------------------------------------------------
Call retainClosure for each of the closures covered by a large bitmap.
-------------------------------------------------------------------------- */
static void
retain_large_bitmap (StgPtr p, StgLargeBitmap *large_bitmap, nat size,
StgClosure *c, retainer c_child_r)
{
nat i, b;
StgWord bitmap;
b = 0;
bitmap = large_bitmap->bitmap[b];
for (i = 0; i < size; ) {
if ((bitmap & 1) == 0) {
retainClosure((StgClosure *)*p, c, c_child_r);
}
i++;
p++;
if (i % BITS_IN(W_) == 0) {
b++;
bitmap = large_bitmap->bitmap[b];
} else {
bitmap = bitmap >> 1;
}
}
}
static INLINE StgPtr
retain_small_bitmap (StgPtr p, nat size, StgWord bitmap,
StgClosure *c, retainer c_child_r)
{
while (size > 0) {
if ((bitmap & 1) == 0) {
retainClosure((StgClosure *)*p, c, c_child_r);
}
p++;
bitmap = bitmap >> 1;
size--;
}
return p;
}
/* -----------------------------------------------------------------------------
* Call retainClosure for each of the closures in an SRT.
* ------------------------------------------------------------------------- */
static void
retain_large_srt_bitmap (StgLargeSRT *srt, StgClosure *c, retainer c_child_r)
{
nat i, b, size;
StgWord bitmap;
StgClosure **p;
b = 0;
p = (StgClosure **)srt->srt;
size = srt->l.size;
bitmap = srt->l.bitmap[b];
for (i = 0; i < size; ) {
if ((bitmap & 1) != 0) {
retainClosure((StgClosure *)*p, c, c_child_r);
}
i++;
p++;
if (i % BITS_IN(W_) == 0) {
b++;
bitmap = srt->l.bitmap[b];
} else {
bitmap = bitmap >> 1;
}
}
}
static INLINE void
retainSRT (StgClosure **srt, nat srt_bitmap, StgClosure *c, retainer c_child_r)
{
nat bitmap;
StgClosure **p;
bitmap = srt_bitmap;
p = srt;
if (bitmap == (StgHalfWord)(-1)) {
retain_large_srt_bitmap( (StgLargeSRT *)srt, c, c_child_r );
return;
}
while (bitmap != 0) {
if ((bitmap & 1) != 0) {
#if defined(COMPILING_WINDOWS_DLL)
if ( (unsigned long)(*srt) & 0x1 ) {
retainClosure(* (StgClosure**) ((unsigned long) (*srt) & ~0x1),
c, c_child_r);
} else {
retainClosure(*srt,c,c_child_r);
}
#else
retainClosure(*srt,c,c_child_r);
#endif
}
p++;
bitmap = bitmap >> 1;
}
}
/* -----------------------------------------------------------------------------
* Process all the objects in the stack chunk from stackStart to stackEnd
* with *c and *c_child_r being their parent and their most recent retainer,
* respectively. Treat stackOptionalFun as another child of *c if it is
* not NULL.
* Invariants:
* *c is one of the following: TSO, AP_STACK.
* If *c is TSO, c == c_child_r.
* stackStart < stackEnd.
* RSET(c) and RSET(c_child_r) are valid, i.e., their
* interpretation conforms to the current value of flip (even when they
* are interpreted to be NULL).
* If *c is TSO, its state is not ThreadComplete,or ThreadKilled,
* which means that its stack is ready to process.
* Note:
* This code was almost plagiarzied from GC.c! For each pointer,
* retainClosure() is invoked instead of evacuate().
* -------------------------------------------------------------------------- */
static void
retainStack( StgClosure *c, retainer c_child_r,
StgPtr stackStart, StgPtr stackEnd )
{
stackElement *oldStackBoundary;
StgPtr p;
StgRetInfoTable *info;
StgWord bitmap;
nat size;
#ifdef DEBUG_RETAINER
cStackSize++;
if (cStackSize > maxCStackSize) maxCStackSize = cStackSize;
#endif
/*
Each invocation of retainStack() creates a new virtual
stack. Since all such stacks share a single common stack, we
record the current currentStackBoundary, which will be restored
at the exit.
*/
oldStackBoundary = currentStackBoundary;
currentStackBoundary = stackTop;
#ifdef DEBUG_RETAINER
// debugBelch("retainStack() called: oldStackBoundary = 0x%x, currentStackBoundary = 0x%x\n", oldStackBoundary, currentStackBoundary);
#endif
ASSERT(get_itbl(c)->type == STACK);
p = stackStart;
while (p < stackEnd) {
info = get_ret_itbl((StgClosure *)p);
switch(info->i.type) {
case UPDATE_FRAME:
retainClosure(((StgUpdateFrame *)p)->updatee, c, c_child_r);
p += sizeofW(StgUpdateFrame);
continue;
case UNDERFLOW_FRAME:
case STOP_FRAME:
case CATCH_FRAME:
case CATCH_STM_FRAME:
case CATCH_RETRY_FRAME:
case ATOMICALLY_FRAME:
case RET_SMALL:
bitmap = BITMAP_BITS(info->i.layout.bitmap);
size = BITMAP_SIZE(info->i.layout.bitmap);
p++;
p = retain_small_bitmap(p, size, bitmap, c, c_child_r);
follow_srt:
retainSRT((StgClosure **)GET_SRT(info), info->i.srt_bitmap, c, c_child_r);
continue;
case RET_BCO: {
StgBCO *bco;
p++;
retainClosure((StgClosure *)*p, c, c_child_r);
bco = (StgBCO *)*p;
p++;
size = BCO_BITMAP_SIZE(bco);
retain_large_bitmap(p, BCO_BITMAP(bco), size, c, c_child_r);
p += size;
continue;
}
// large bitmap (> 32 entries, or > 64 on a 64-bit machine)
case RET_BIG:
size = GET_LARGE_BITMAP(&info->i)->size;
p++;
retain_large_bitmap(p, GET_LARGE_BITMAP(&info->i),
size, c, c_child_r);
p += size;
// and don't forget to follow the SRT
goto follow_srt;
case RET_FUN: {
StgRetFun *ret_fun = (StgRetFun *)p;
StgFunInfoTable *fun_info;
retainClosure(ret_fun->fun, c, c_child_r);
fun_info = get_fun_itbl(UNTAG_CLOSURE(ret_fun->fun));
p = (P_)&ret_fun->payload;
switch (fun_info->f.fun_type) {
case ARG_GEN:
bitmap = BITMAP_BITS(fun_info->f.b.bitmap);
size = BITMAP_SIZE(fun_info->f.b.bitmap);
p = retain_small_bitmap(p, size, bitmap, c, c_child_r);
break;
case ARG_GEN_BIG:
size = GET_FUN_LARGE_BITMAP(fun_info)->size;
retain_large_bitmap(p, GET_FUN_LARGE_BITMAP(fun_info),
size, c, c_child_r);
p += size;
break;
default:
bitmap = BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]);
size = BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]);
p = retain_small_bitmap(p, size, bitmap, c, c_child_r);
break;
}
goto follow_srt;
}
default:
barf("Invalid object found in retainStack(): %d",
(int)(info->i.type));
}
}
// restore currentStackBoundary
currentStackBoundary = oldStackBoundary;
#ifdef DEBUG_RETAINER
// debugBelch("retainStack() finished: currentStackBoundary = 0x%x\n", currentStackBoundary);
#endif
#ifdef DEBUG_RETAINER
cStackSize--;
#endif
}
/* ----------------------------------------------------------------------------
* Call retainClosure for each of the children of a PAP/AP
* ------------------------------------------------------------------------- */
static INLINE StgPtr
retain_PAP_payload (StgClosure *pap, /* NOT tagged */
retainer c_child_r, /* NOT tagged */
StgClosure *fun, /* tagged */
StgClosure** payload, StgWord n_args)
{
StgPtr p;
StgWord bitmap;
StgFunInfoTable *fun_info;
retainClosure(fun, pap, c_child_r);
fun = UNTAG_CLOSURE(fun);
fun_info = get_fun_itbl(fun);
ASSERT(fun_info->i.type != PAP);
p = (StgPtr)payload;
switch (fun_info->f.fun_type) {
case ARG_GEN:
bitmap = BITMAP_BITS(fun_info->f.b.bitmap);
p = retain_small_bitmap(p, n_args, bitmap,
pap, c_child_r);
break;
case ARG_GEN_BIG:
retain_large_bitmap(p, GET_FUN_LARGE_BITMAP(fun_info),
n_args, pap, c_child_r);
p += n_args;
break;
case ARG_BCO:
retain_large_bitmap((StgPtr)payload, BCO_BITMAP(fun),
n_args, pap, c_child_r);
p += n_args;
break;
default:
bitmap = BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]);
p = retain_small_bitmap(p, n_args, bitmap, pap, c_child_r);
break;
}
return p;
}
/* -----------------------------------------------------------------------------
* Compute the retainer set of *c0 and all its desecents by traversing.
* *cp0 is the parent of *c0, and *r0 is the most recent retainer of *c0.
* Invariants:
* c0 = cp0 = r0 holds only for root objects.
* RSET(cp0) and RSET(r0) are valid, i.e., their
* interpretation conforms to the current value of flip (even when they
* are interpreted to be NULL).
* However, RSET(c0) may be corrupt, i.e., it may not conform to
* the current value of flip. If it does not, during the execution
* of this function, RSET(c0) must be initialized as well as all
* its descendants.
* Note:
* stackTop must be the same at the beginning and the exit of this function.
* *c0 can be TSO (as well as AP_STACK).
* -------------------------------------------------------------------------- */
static void
retainClosure( StgClosure *c0, StgClosure *cp0, retainer r0 )
{
// c = Current closure (possibly tagged)
// cp = Current closure's Parent (NOT tagged)
// r = current closures' most recent Retainer (NOT tagged)
// c_child_r = current closure's children's most recent retainer
// first_child = first child of c
StgClosure *c, *cp, *first_child;
RetainerSet *s, *retainerSetOfc;
retainer r, c_child_r;
StgWord typeOfc;
#ifdef DEBUG_RETAINER
// StgPtr oldStackTop;
#endif
#ifdef DEBUG_RETAINER
// oldStackTop = stackTop;
// debugBelch("retainClosure() called: c0 = 0x%x, cp0 = 0x%x, r0 = 0x%x\n", c0, cp0, r0);
#endif
// (c, cp, r) = (c0, cp0, r0)
c = c0;
cp = cp0;
r = r0;
goto inner_loop;
loop:
//debugBelch("loop");
// pop to (c, cp, r);
pop(&c, &cp, &r);
if (c == NULL) {
#ifdef DEBUG_RETAINER
// debugBelch("retainClosure() ends: oldStackTop = 0x%x, stackTop = 0x%x\n", oldStackTop, stackTop);
#endif
return;
}
//debugBelch("inner_loop");
inner_loop:
c = UNTAG_CLOSURE(c);
// c = current closure under consideration,
// cp = current closure's parent,
// r = current closure's most recent retainer
//
// Loop invariants (on the meaning of c, cp, r, and their retainer sets):
// RSET(cp) and RSET(r) are valid.
// RSET(c) is valid only if c has been visited before.
//
// Loop invariants (on the relation between c, cp, and r)
// if cp is not a retainer, r belongs to RSET(cp).
// if cp is a retainer, r == cp.
typeOfc = get_itbl(c)->type;
#ifdef DEBUG_RETAINER
switch (typeOfc) {
case IND_STATIC:
case CONSTR_NOCAF_STATIC:
case CONSTR_STATIC:
case THUNK_STATIC:
case FUN_STATIC:
break;
default:
if (retainerSetOf(c) == NULL) { // first visit?
costArray[typeOfc] += cost(c);
sumOfNewCost += cost(c);
}
break;
}
#endif
// special cases
switch (typeOfc) {
case TSO:
if (((StgTSO *)c)->what_next == ThreadComplete ||
((StgTSO *)c)->what_next == ThreadKilled) {
#ifdef DEBUG_RETAINER
debugBelch("ThreadComplete or ThreadKilled encountered in retainClosure()\n");
#endif
goto loop;
}
break;
case IND_STATIC:
// We just skip IND_STATIC, so its retainer set is never computed.
c = ((StgIndStatic *)c)->indirectee;
goto inner_loop;
// static objects with no pointers out, so goto loop.
case CONSTR_NOCAF_STATIC:
// It is not just enough not to compute the retainer set for *c; it is
// mandatory because CONSTR_NOCAF_STATIC are not reachable from
// scavenged_static_objects, the list from which is assumed to traverse
// all static objects after major garbage collections.
goto loop;
case THUNK_STATIC:
case FUN_STATIC:
if (get_itbl(c)->srt_bitmap == 0) {
// No need to compute the retainer set; no dynamic objects
// are reachable from *c.
//
// Static objects: if we traverse all the live closures,
// including static closures, during each heap census then
// we will observe that some static closures appear and
// disappear. eg. a closure may contain a pointer to a
// static function 'f' which is not otherwise reachable
// (it doesn't indirectly point to any CAFs, so it doesn't
// appear in any SRTs), so we would find 'f' during
// traversal. However on the next sweep there may be no
// closures pointing to 'f'.
//
// We must therefore ignore static closures whose SRT is
// empty, because these are exactly the closures that may
// "appear". A closure with a non-empty SRT, and which is
// still required, will always be reachable.
//
// But what about CONSTR_STATIC? Surely these may be able
// to appear, and they don't have SRTs, so we can't
// check. So for now, we're calling
// resetStaticObjectForRetainerProfiling() from the
// garbage collector to reset the retainer sets in all the
// reachable static objects.
goto loop;
}
default:
break;
}
// The above objects are ignored in computing the average number of times
// an object is visited.
timesAnyObjectVisited++;
// If this is the first visit to c, initialize its retainer set.
maybeInitRetainerSet(c);
retainerSetOfc = retainerSetOf(c);
// Now compute s:
// isRetainer(cp) == rtsTrue => s == NULL
// isRetainer(cp) == rtsFalse => s == cp.retainer
if (isRetainer(cp))
s = NULL;
else
s = retainerSetOf(cp);
// (c, cp, r, s) is available.
// (c, cp, r, s, R_r) is available, so compute the retainer set for *c.
if (retainerSetOfc == NULL) {
// This is the first visit to *c.
numObjectVisited++;
if (s == NULL)
associate(c, singleton(r));
else
// s is actually the retainer set of *c!
associate(c, s);
// compute c_child_r
c_child_r = isRetainer(c) ? getRetainerFrom(c) : r;
} else {
// This is not the first visit to *c.
if (isMember(r, retainerSetOfc))
goto loop; // no need to process child
if (s == NULL)
associate(c, addElement(r, retainerSetOfc));
else {
// s is not NULL and cp is not a retainer. This means that
// each time *cp is visited, so is *c. Thus, if s has
// exactly one more element in its retainer set than c, s
// is also the new retainer set for *c.
if (s->num == retainerSetOfc->num + 1) {
associate(c, s);
}
// Otherwise, just add R_r to the current retainer set of *c.
else {
associate(c, addElement(r, retainerSetOfc));
}
}
if (isRetainer(c))
goto loop; // no need to process child
// compute c_child_r
c_child_r = r;
}
// now, RSET() of all of *c, *cp, and *r is valid.
// (c, c_child_r) are available.
// process child
// Special case closures: we process these all in one go rather
// than attempting to save the current position, because doing so
// would be hard.
switch (typeOfc) {
case STACK:
retainStack(c, c_child_r,
((StgStack *)c)->sp,
((StgStack *)c)->stack + ((StgStack *)c)->stack_size);
goto loop;
case TSO:
{
StgTSO *tso = (StgTSO *)c;
retainClosure(tso->stackobj, c, c_child_r);
retainClosure(tso->blocked_exceptions, c, c_child_r);
retainClosure(tso->bq, c, c_child_r);
retainClosure(tso->trec, c, c_child_r);
if ( tso->why_blocked == BlockedOnMVar
|| tso->why_blocked == BlockedOnMVarRead
|| tso->why_blocked == BlockedOnBlackHole
|| tso->why_blocked == BlockedOnMsgThrowTo
) {
retainClosure(tso->block_info.closure, c, c_child_r);
}
goto loop;
}
case PAP:
{
StgPAP *pap = (StgPAP *)c;
retain_PAP_payload(c, c_child_r, pap->fun, pap->payload, pap->n_args);
goto loop;
}
case AP:
{
StgAP *ap = (StgAP *)c;
retain_PAP_payload(c, c_child_r, ap->fun, ap->payload, ap->n_args);
goto loop;
}
case AP_STACK:
retainClosure(((StgAP_STACK *)c)->fun, c, c_child_r);
retainStack(c, c_child_r,
(StgPtr)((StgAP_STACK *)c)->payload,
(StgPtr)((StgAP_STACK *)c)->payload +
((StgAP_STACK *)c)->size);
goto loop;
}
push(c, c_child_r, &first_child);
// If first_child is null, c has no child.
// If first_child is not null, the top stack element points to the next
// object. push() may or may not push a stackElement on the stack.
if (first_child == NULL)
goto loop;
// (c, cp, r) = (first_child, c, c_child_r)
r = c_child_r;
cp = c;
c = first_child;
goto inner_loop;
}
/* -----------------------------------------------------------------------------
* Compute the retainer set for every object reachable from *tl.
* -------------------------------------------------------------------------- */
static void
retainRoot(void *user STG_UNUSED, StgClosure **tl)
{
StgClosure *c;
// We no longer assume that only TSOs and WEAKs are roots; any closure can
// be a root.
ASSERT(isEmptyRetainerStack());
currentStackBoundary = stackTop;
c = UNTAG_CLOSURE(*tl);
maybeInitRetainerSet(c);
if (c != &stg_END_TSO_QUEUE_closure && isRetainer(c)) {
retainClosure(c, c, getRetainerFrom(c));
} else {
retainClosure(c, c, CCS_SYSTEM);
}
// NOT TRUE: ASSERT(isMember(getRetainerFrom(*tl), retainerSetOf(*tl)));
// *tl might be a TSO which is ThreadComplete, in which
// case we ignore it for the purposes of retainer profiling.
}
/* -----------------------------------------------------------------------------
* Compute the retainer set for each of the objects in the heap.
* -------------------------------------------------------------------------- */
static void
computeRetainerSet( void )
{
StgWeak *weak;
RetainerSet *rtl;
nat g, n;
StgPtr ml;
bdescr *bd;
#ifdef DEBUG_RETAINER
RetainerSet tmpRetainerSet;
#endif
markCapabilities(retainRoot, NULL); // for scheduler roots
// This function is called after a major GC, when key, value, and finalizer
// all are guaranteed to be valid, or reachable.
//
// The following code assumes that WEAK objects are considered to be roots
// for retainer profilng.
for (n = 0; n < n_capabilities; n++) {
// NB: after a GC, all nursery weak_ptr_lists have been migrated
// to the global lists living in the generations
ASSERT(capabilities[n]->weak_ptr_list_hd == NULL);
ASSERT(capabilities[n]->weak_ptr_list_tl == NULL);
}
for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
for (weak = generations[g].weak_ptr_list; weak != NULL; weak = weak->link) {
// retainRoot((StgClosure *)weak);
retainRoot(NULL, (StgClosure **)&weak);
}
}
// Consider roots from the stable ptr table.
markStableTables(retainRoot, NULL);
// The following code resets the rs field of each unvisited mutable
// object (computing sumOfNewCostExtra and updating costArray[] when
// debugging retainer profiler).
for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
// NOT TRUE: even G0 has a block on its mutable list
// ASSERT(g != 0 || (generations[g].mut_list == NULL));
// Traversing through mut_list is necessary
// because we can find MUT_VAR objects which have not been
// visited during retainer profiling.
for (n = 0; n < n_capabilities; n++) {
for (bd = capabilities[n]->mut_lists[g]; bd != NULL; bd = bd->link) {
for (ml = bd->start; ml < bd->free; ml++) {
maybeInitRetainerSet((StgClosure *)*ml);
rtl = retainerSetOf((StgClosure *)*ml);
#ifdef DEBUG_RETAINER
if (rtl == NULL) {
// first visit to *ml
// This is a violation of the interface rule!
RSET(ml) = (RetainerSet *)((StgWord)(&tmpRetainerSet) | flip);
switch (get_itbl((StgClosure *)ml)->type) {
case IND_STATIC:
// no cost involved
break;
case CONSTR_NOCAF_STATIC:
case CONSTR_STATIC:
case THUNK_STATIC:
case FUN_STATIC:
barf("Invalid object in computeRetainerSet(): %d", get_itbl((StgClosure*)ml)->type);
break;
default:
// dynamic objects
costArray[get_itbl((StgClosure *)ml)->type] += cost((StgClosure *)ml);
sumOfNewCostExtra += cost((StgClosure *)ml);
break;
}
}
#endif
}
}
}
}
}
/* -----------------------------------------------------------------------------
* Traverse all static objects for which we compute retainer sets,
* and reset their rs fields to NULL, which is accomplished by
* invoking maybeInitRetainerSet(). This function must be called
* before zeroing all objects reachable from scavenged_static_objects
* in the case of major gabage collections. See GarbageCollect() in
* GC.c.
* Note:
* The mut_once_list of the oldest generation must also be traversed?
* Why? Because if the evacuation of an object pointed to by a static
* indirection object fails, it is put back to the mut_once_list of
* the oldest generation.
* However, this is not necessary because any static indirection objects
* are just traversed through to reach dynamic objects. In other words,
* they are not taken into consideration in computing retainer sets.
*
* SDM (20/7/2011): I don't think this is doing anything sensible,
* because it happens before retainerProfile() and at the beginning of
* retainerProfil() we change the sense of 'flip'. So all of the
* calls to maybeInitRetainerSet() here are initialising retainer sets
* with the wrong flip. Also, I don't see why this is necessary. I
* added a maybeInitRetainerSet() call to retainRoot(), and that seems
* to have fixed the assertion failure in retainerSetOf() I was
* encountering.
* -------------------------------------------------------------------------- */
void
resetStaticObjectForRetainerProfiling( StgClosure *static_objects )
{
#ifdef DEBUG_RETAINER
nat count;
#endif
StgClosure *p;
#ifdef DEBUG_RETAINER
count = 0;
#endif
p = static_objects;
while (p != END_OF_STATIC_OBJECT_LIST) {
p = UNTAG_STATIC_LIST_PTR(p);
#ifdef DEBUG_RETAINER
count++;
#endif
switch (get_itbl(p)->type) {
case IND_STATIC:
// Since we do not compute the retainer set of any
// IND_STATIC object, we don't have to reset its retainer
// field.
p = (StgClosure*)*IND_STATIC_LINK(p);
break;
case THUNK_STATIC:
maybeInitRetainerSet(p);
p = (StgClosure*)*THUNK_STATIC_LINK(p);
break;
case FUN_STATIC:
maybeInitRetainerSet(p);
p = (StgClosure*)*FUN_STATIC_LINK(p);
break;
case CONSTR_STATIC:
maybeInitRetainerSet(p);
p = (StgClosure*)*STATIC_LINK(get_itbl(p), p);
break;
default:
barf("resetStaticObjectForRetainerProfiling: %p (%s)",
p, get_itbl(p)->type);
break;
}
}
#ifdef DEBUG_RETAINER
// debugBelch("count in scavenged_static_objects = %d\n", count);
#endif
}
/* -----------------------------------------------------------------------------
* Perform retainer profiling.
* N is the oldest generation being profilied, where the generations are
* numbered starting at 0.
* Invariants:
* Note:
* This function should be called only immediately after major garbage
* collection.
* ------------------------------------------------------------------------- */
void
retainerProfile(void)
{
#ifdef DEBUG_RETAINER
nat i;
nat totalHeapSize; // total raw heap size (computed by linear scanning)
#endif
#ifdef DEBUG_RETAINER
debugBelch(" < retainerProfile() invoked : %d>\n", retainerGeneration);
#endif
stat_startRP();
// We haven't flipped the bit yet.
#ifdef DEBUG_RETAINER
debugBelch("Before traversing:\n");
sumOfCostLinear = 0;
for (i = 0;i < N_CLOSURE_TYPES; i++)
costArrayLinear[i] = 0;
totalHeapSize = checkHeapSanityForRetainerProfiling();
debugBelch("\tsumOfCostLinear = %d, totalHeapSize = %d\n", sumOfCostLinear, totalHeapSize);
/*
debugBelch("costArrayLinear[] = ");
for (i = 0;i < N_CLOSURE_TYPES; i++)
debugBelch("[%u:%u] ", i, costArrayLinear[i]);
debugBelch("\n");
*/
ASSERT(sumOfCostLinear == totalHeapSize);
/*
#define pcostArrayLinear(index) \
if (costArrayLinear[index] > 0) \
debugBelch("costArrayLinear[" #index "] = %u\n", costArrayLinear[index])
pcostArrayLinear(THUNK_STATIC);
pcostArrayLinear(FUN_STATIC);
pcostArrayLinear(CONSTR_STATIC);
pcostArrayLinear(CONSTR_NOCAF_STATIC);
*/
#endif
// Now we flips flip.
flip = flip ^ 1;
#ifdef DEBUG_RETAINER
stackSize = 0;
maxStackSize = 0;
cStackSize = 0;
maxCStackSize = 0;
#endif
numObjectVisited = 0;
timesAnyObjectVisited = 0;
#ifdef DEBUG_RETAINER
debugBelch("During traversing:\n");
sumOfNewCost = 0;
sumOfNewCostExtra = 0;
for (i = 0;i < N_CLOSURE_TYPES; i++)
costArray[i] = 0;
#endif
/*
We initialize the traverse stack each time the retainer profiling is
performed (because the traverse stack size varies on each retainer profiling
and this operation is not costly anyhow). However, we just refresh the
retainer sets.
*/
initializeTraverseStack();
#ifdef DEBUG_RETAINER
initializeAllRetainerSet();
#else
refreshAllRetainerSet();
#endif
computeRetainerSet();
#ifdef DEBUG_RETAINER
debugBelch("After traversing:\n");
sumOfCostLinear = 0;
for (i = 0;i < N_CLOSURE_TYPES; i++)
costArrayLinear[i] = 0;
totalHeapSize = checkHeapSanityForRetainerProfiling();
debugBelch("\tsumOfCostLinear = %d, totalHeapSize = %d\n", sumOfCostLinear, totalHeapSize);
ASSERT(sumOfCostLinear == totalHeapSize);
// now, compare the two results
/*
Note:
costArray[] must be exactly the same as costArrayLinear[].
Known exceptions:
1) Dead weak pointers, whose type is CONSTR. These objects are not
reachable from any roots.
*/
debugBelch("Comparison:\n");
debugBelch("\tcostArrayLinear[] (must be empty) = ");
for (i = 0;i < N_CLOSURE_TYPES; i++)
if (costArray[i] != costArrayLinear[i])
// nothing should be printed except MUT_VAR after major GCs
debugBelch("[%u:%u] ", i, costArrayLinear[i]);
debugBelch("\n");
debugBelch("\tsumOfNewCost = %u\n", sumOfNewCost);
debugBelch("\tsumOfNewCostExtra = %u\n", sumOfNewCostExtra);
debugBelch("\tcostArray[] (must be empty) = ");
for (i = 0;i < N_CLOSURE_TYPES; i++)
if (costArray[i] != costArrayLinear[i])
// nothing should be printed except MUT_VAR after major GCs
debugBelch("[%u:%u] ", i, costArray[i]);
debugBelch("\n");
// only for major garbage collection
ASSERT(sumOfNewCost + sumOfNewCostExtra == sumOfCostLinear);
#endif
// post-processing
closeTraverseStack();
#ifdef DEBUG_RETAINER
closeAllRetainerSet();
#else
// Note that there is no post-processing for the retainer sets.
#endif
retainerGeneration++;
stat_endRP(
retainerGeneration - 1, // retainerGeneration has just been incremented!
#ifdef DEBUG_RETAINER
maxCStackSize, maxStackSize,
#endif
(double)timesAnyObjectVisited / numObjectVisited);
}
/* -----------------------------------------------------------------------------
* DEBUGGING CODE
* -------------------------------------------------------------------------- */
#ifdef DEBUG_RETAINER
#define LOOKS_LIKE_PTR(r) ((LOOKS_LIKE_STATIC_CLOSURE(r) || \
((HEAP_ALLOCED(r) && ((Bdescr((P_)r)->flags & BF_FREE) == 0)))) && \
((StgWord)(*(StgPtr)r)!=0xaaaaaaaa))
static nat
sanityCheckHeapClosure( StgClosure *c )
{
StgInfoTable *info;
ASSERT(LOOKS_LIKE_GHC_INFO(c->header.info));
ASSERT(!closure_STATIC(c));
ASSERT(LOOKS_LIKE_PTR(c));
if ((((StgWord)RSET(c) & 1) ^ flip) != 0) {
if (get_itbl(c)->type == CONSTR &&
!strcmp(GET_PROF_TYPE(get_itbl(c)), "DEAD_WEAK") &&
!strcmp(GET_PROF_DESC(get_itbl(c)), "DEAD_WEAK")) {
debugBelch("\tUnvisited dead weak pointer object found: c = %p\n", c);
costArray[get_itbl(c)->type] += cost(c);
sumOfNewCost += cost(c);
} else
debugBelch(
"Unvisited object: flip = %d, c = %p(%d, %s, %s), rs = %p\n",
flip, c, get_itbl(c)->type,
get_itbl(c)->prof.closure_type, GET_PROF_DESC(get_itbl(c)),
RSET(c));
} else {
// debugBelch("sanityCheckHeapClosure) S: flip = %d, c = %p(%d), rs = %p\n", flip, c, get_itbl(c)->type, RSET(c));
}
return closure_sizeW(c);
}
static nat
heapCheck( bdescr *bd )
{
StgPtr p;
static nat costSum, size;
costSum = 0;
while (bd != NULL) {
p = bd->start;
while (p < bd->free) {
size = sanityCheckHeapClosure((StgClosure *)p);
sumOfCostLinear += size;
costArrayLinear[get_itbl((StgClosure *)p)->type] += size;
p += size;
// no need for slop check; I think slops are not used currently.
}
ASSERT(p == bd->free);
costSum += bd->free - bd->start;
bd = bd->link;
}
return costSum;
}
static nat
smallObjectPoolCheck(void)
{
bdescr *bd;
StgPtr p;
static nat costSum, size;
bd = g0s0->blocks;
costSum = 0;
// first block
if (bd == NULL)
return costSum;
p = bd->start;
while (p < alloc_Hp) {
size = sanityCheckHeapClosure((StgClosure *)p);
sumOfCostLinear += size;
costArrayLinear[get_itbl((StgClosure *)p)->type] += size;
p += size;
}
ASSERT(p == alloc_Hp);
costSum += alloc_Hp - bd->start;
bd = bd->link;
while (bd != NULL) {
p = bd->start;
while (p < bd->free) {
size = sanityCheckHeapClosure((StgClosure *)p);
sumOfCostLinear += size;
costArrayLinear[get_itbl((StgClosure *)p)->type] += size;
p += size;
}
ASSERT(p == bd->free);
costSum += bd->free - bd->start;
bd = bd->link;
}
return costSum;
}
static nat
chainCheck(bdescr *bd)
{
nat costSum, size;
costSum = 0;
while (bd != NULL) {
// bd->free - bd->start is not an accurate measurement of the
// object size. Actually it is always zero, so we compute its
// size explicitly.
size = sanityCheckHeapClosure((StgClosure *)bd->start);
sumOfCostLinear += size;
costArrayLinear[get_itbl((StgClosure *)bd->start)->type] += size;
costSum += size;
bd = bd->link;
}
return costSum;
}
static nat
checkHeapSanityForRetainerProfiling( void )
{
nat costSum, g, s;
costSum = 0;
debugBelch("START: sumOfCostLinear = %d, costSum = %d\n", sumOfCostLinear, costSum);
if (RtsFlags.GcFlags.generations == 1) {
costSum += heapCheck(g0s0->to_blocks);
debugBelch("heapCheck: sumOfCostLinear = %d, costSum = %d\n", sumOfCostLinear, costSum);
costSum += chainCheck(g0s0->large_objects);
debugBelch("chainCheck: sumOfCostLinear = %d, costSum = %d\n", sumOfCostLinear, costSum);
} else {
for (g = 0; g < RtsFlags.GcFlags.generations; g++)
for (s = 0; s < generations[g].n_steps; s++) {
/*
After all live objects have been scavenged, the garbage
collector may create some objects in
scheduleFinalizers(). These objects are created through
allocate(), so the small object pool or the large object
pool of the g0s0 may not be empty.
*/
if (g == 0 && s == 0) {
costSum += smallObjectPoolCheck();
debugBelch("smallObjectPoolCheck(): sumOfCostLinear = %d, costSum = %d\n", sumOfCostLinear, costSum);
costSum += chainCheck(generations[g].steps[s].large_objects);
debugBelch("chainCheck(): sumOfCostLinear = %d, costSum = %d\n", sumOfCostLinear, costSum);
} else {
costSum += heapCheck(generations[g].steps[s].blocks);
debugBelch("heapCheck(): sumOfCostLinear = %d, costSum = %d\n", sumOfCostLinear, costSum);
costSum += chainCheck(generations[g].steps[s].large_objects);
debugBelch("chainCheck(): sumOfCostLinear = %d, costSum = %d\n", sumOfCostLinear, costSum);
}
}
}
return costSum;
}
void
findPointer(StgPtr p)
{
StgPtr q, r, e;
bdescr *bd;
nat g, s;
for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
for (s = 0; s < generations[g].n_steps; s++) {
// if (g == 0 && s == 0) continue;
bd = generations[g].steps[s].blocks;
for (; bd; bd = bd->link) {
for (q = bd->start; q < bd->free; q++) {
if (*q == (StgWord)p) {
r = q;
while (!LOOKS_LIKE_GHC_INFO(*r)) r--;
debugBelch("Found in gen[%d], step[%d]: q = %p, r = %p\n", g, s, q, r);
// return;
}
}
}
bd = generations[g].steps[s].large_objects;
for (; bd; bd = bd->link) {
e = bd->start + cost((StgClosure *)bd->start);
for (q = bd->start; q < e; q++) {
if (*q == (StgWord)p) {
r = q;
while (*r == 0 || !LOOKS_LIKE_GHC_INFO(*r)) r--;
debugBelch("Found in gen[%d], large_objects: %p\n", g, r);
// return;
}
}
}
}
}
}
static void
belongToHeap(StgPtr p)
{
bdescr *bd;
nat g, s;
for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
for (s = 0; s < generations[g].n_steps; s++) {
// if (g == 0 && s == 0) continue;
bd = generations[g].steps[s].blocks;
for (; bd; bd = bd->link) {
if (bd->start <= p && p < bd->free) {
debugBelch("Belongs to gen[%d], step[%d]", g, s);
return;
}
}
bd = generations[g].steps[s].large_objects;
for (; bd; bd = bd->link) {
if (bd->start <= p && p < bd->start + getHeapClosureSize((StgClosure *)bd->start)) {
debugBelch("Found in gen[%d], large_objects: %p\n", g, bd->start);
return;
}
}
}
}
}
#endif /* DEBUG_RETAINER */
#endif /* PROFILING */
|
the_stack_data/117091.c | int main()
{
int i, n;
int c[n];
#pragma scop
/* A comment in the scop. */
for (i=1;i<=n;i++)
// A C++ comment.
c[i] = 0;
/* Another comment in the scop. */
#pragma endscop
}
|
the_stack_data/370585.c | //Write a program in C to implement Quick Sorting.
#include<stdio.h>
void quicksort(int arr[25], int f, int l)
{
int i, j, p, t;
if(f < l)
{
p = f;
i = f;
j = l;
while(i < j)
{
while(arr[i] <= arr[p] && i < l)
i++;
while(arr[j] > arr[p])
j--;
if(i < j)
{
t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
t = arr[p];
arr[p] = arr[j];
arr[j]=t;
quicksort(arr,f,j-1);
quicksort(arr,j+1,l);
}
}
int main()
{
int i, n, arr[25];
printf("Enter the size of an array: \n");
scanf("%d",&n);
printf("Enter %d elements: \n", n);
for(i=0;i < n;i++)
scanf("%d",&arr[i]);
quicksort(arr,0,n-1);
printf("Order of Sorted elements: \n");
for(i=0;i<n;i++)
printf(" %d\t",arr[i]);
return 0;
}
|
the_stack_data/86074222.c | #include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
/*
* These are defined for each TIP program in the compiled code.
*/
int64_t _tip_main();
extern int64_t _tip_num_inputs;
extern int64_t _tip_input_array[];
/*
* intrinsic functions for TIP IO expressions and statements
* x = input;
* output y;
* error y;
*/
int64_t _tip_input() {
int64_t x;
printf("Enter input: ");
scanf("%" SCNd64, &x);
return x;
}
void _tip_output(int64_t x) {
printf("Program output: %" PRId64 "\n", x);
}
void _tip_error(int64_t x) {
printf("[error] Error: Execution error, code: %" PRId64 "\n", x);
exit(-1);
}
/*
* If the compiled program has no "main" function then one is created
* that calls this function.
*/
void _tip_main_undefined() {
printf("Error: missing main function\n");
exit(-1);
}
/*
* Set up the arguments to be read by the TIP "main" function.
* The number of arguments is defined by the compiled TIP code
* and read here to perform error checking. All arguments to
* the TIP "main" are passed through the "_tip_input_array".
* Finally, the TIP "main" is renamed during compilation to "_tip_main",
* its arguments are removed, and code to read them from "_tip_input_array"
* is generated.
*/
int main(int argc, char *argv[]) {
// Throw an error if the wrong number of arguments are passed
if (argc != _tip_num_inputs + 1) {
printf("expected %ld integer arguments\n", _tip_num_inputs);
exit(-1);
}
// required by strtoll, but discarded
char *eptr;
for (size_t i=0; i < _tip_num_inputs; i++) {
_tip_input_array[i] = strtoll(argv[i+1], &eptr, 10);
}
printf("Program output: %" PRId64 "\n", _tip_main());
return 0;
}
|
the_stack_data/109255.c | //
// chalice (256 uncompressed bytes)
//
extern const unsigned char chalice_Bitmap[256] = {
0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x00, 0x00, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
0x00, 0x00, 0x00, 0x00, 0x1a, 0x1a, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x17, 0x17, 0x1a, 0x18, 0x00,
0x00, 0x00, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x1a, 0x1a, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x16, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
|
the_stack_data/31387258.c | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <CL/cl.h>
unsigned char *read_buffer(char *file_name, size_t *size_ptr)
{
FILE *f;
unsigned char *buf;
size_t size;
/* Open file */
f = fopen(file_name, "rb");
if (!f)
return NULL;
/* Obtain file size */
fseek(f, 0, SEEK_END);
size = ftell(f);
fseek(f, 0, SEEK_SET);
/* Allocate and read buffer */
buf = malloc(size + 1);
fread(buf, 1, size, f);
buf[size] = '\0';
/* Return size of buffer */
if (size_ptr)
*size_ptr = size;
/* Return buffer */
return buf;
}
void write_buffer(char *file_name, const char *buffer, size_t buffer_size)
{
FILE *f;
/* Open file */
f = fopen(file_name, "w+");
/* Write buffer */
if(buffer)
fwrite(buffer, 1, buffer_size, f);
/* Close file */
fclose(f);
}
int main(int argc, char const *argv[])
{
/* Get platform */
cl_platform_id platform;
cl_uint num_platforms;
cl_int ret = clGetPlatformIDs(1, &platform, &num_platforms);
if (ret != CL_SUCCESS)
{
printf("error: call to 'clGetPlatformIDs' failed\n");
exit(1);
}
printf("Number of platforms: %d\n", num_platforms);
printf("platform=%p\n", platform);
/* Get platform name */
char platform_name[100];
ret = clGetPlatformInfo(platform, CL_PLATFORM_NAME, sizeof(platform_name), platform_name, NULL);
if (ret != CL_SUCCESS)
{
printf("error: call to 'clGetPlatformInfo' failed\n");
exit(1);
}
printf("platform.name='%s'\n\n", platform_name);
/* Get device */
cl_device_id device;
cl_uint num_devices;
ret = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, &num_devices);
if (ret != CL_SUCCESS)
{
printf("error: call to 'clGetDeviceIDs' failed\n");
exit(1);
}
printf("Number of devices: %d\n", num_devices);
printf("device=%p\n", device);
/* Get device name */
char device_name[100];
ret = clGetDeviceInfo(device, CL_DEVICE_NAME, sizeof(device_name),
device_name, NULL);
if (ret != CL_SUCCESS)
{
printf("error: call to 'clGetDeviceInfo' failed\n");
exit(1);
}
printf("device.name='%s'\n", device_name);
printf("\n");
/* Create a Context Object */
cl_context context;
context = clCreateContext(NULL, 1, &device, NULL, NULL, &ret);
if (ret != CL_SUCCESS)
{
printf("error: call to 'clCreateContext' failed\n");
exit(1);
}
printf("context=%p\n", context);
/* Create a Command Queue Object*/
cl_command_queue command_queue;
command_queue = clCreateCommandQueue(context, device, 0, &ret);
if (ret != CL_SUCCESS)
{
printf("error: call to 'clCreateCommandQueue' failed\n");
exit(1);
}
printf("command_queue=%p\n", command_queue);
printf("\n");
/* Program source */
unsigned char *source_code;
size_t source_length;
/* Read program from 'atan_float16.cl' */
source_code = read_buffer("atan_float16.cl", &source_length);
/* Create a program */
cl_program program;
program = clCreateProgramWithSource(context, 1, (const char **)&source_code, &source_length, &ret);
if (ret != CL_SUCCESS)
{
printf("error: call to 'clCreateProgramWithSource' failed\n");
exit(1);
}
printf("program=%p\n", program);
/* Build program */
ret = clBuildProgram(program, 1, &device, NULL, NULL, NULL);
if (ret != CL_SUCCESS )
{
size_t size;
char *log;
/* Get log size */
clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG,0, NULL, &size);
/* Allocate log and print */
log = malloc(size);
clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG,size, log, NULL);
printf("error: call to 'clBuildProgram' failed:\n%s\n", log);
/* Free log and exit */
free(log);
exit(1);
}
printf("program built\n");
printf("\n");
/* Create a Kernel Object */
cl_kernel kernel;
kernel = clCreateKernel(program, "atan_float16", &ret);
if (ret != CL_SUCCESS)
{
printf("error: call to 'clCreateKernel' failed\n");
exit(1);
}
/* Create and allocate host buffers */
size_t num_elem = 10;
/* Create and init host side src buffer 0 */
cl_float16 *src_0_host_buffer;
src_0_host_buffer = malloc(num_elem * sizeof(cl_float16));
for (int i = 0; i < num_elem; i++)
src_0_host_buffer[i] = (cl_float16){{2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0}};
/* Create and init device side src buffer 0 */
cl_mem src_0_device_buffer;
src_0_device_buffer = clCreateBuffer(context, CL_MEM_READ_ONLY, num_elem * sizeof(cl_float16), NULL, &ret);
if (ret != CL_SUCCESS)
{
printf("error: could not create source buffer\n");
exit(1);
}
ret = clEnqueueWriteBuffer(command_queue, src_0_device_buffer, CL_TRUE, 0, num_elem * sizeof(cl_float16), src_0_host_buffer, 0, NULL, NULL);
if (ret != CL_SUCCESS)
{
printf("error: call to 'clEnqueueWriteBuffer' failed\n");
exit(1);
}
/* Create host dst buffer */
cl_float16 *dst_host_buffer;
dst_host_buffer = malloc(num_elem * sizeof(cl_float16));
memset((void *)dst_host_buffer, 1, num_elem * sizeof(cl_float16));
/* Create device dst buffer */
cl_mem dst_device_buffer;
dst_device_buffer = clCreateBuffer(context, CL_MEM_WRITE_ONLY, num_elem *sizeof(cl_float16), NULL, &ret);
if (ret != CL_SUCCESS)
{
printf("error: could not create dst buffer\n");
exit(1);
}
/* Set kernel arguments */
ret = CL_SUCCESS;
ret |= clSetKernelArg(kernel, 0, sizeof(cl_mem), &src_0_device_buffer);
ret |= clSetKernelArg(kernel, 1, sizeof(cl_mem), &dst_device_buffer);
if (ret != CL_SUCCESS)
{
printf("error: call to 'clSetKernelArg' failed\n");
exit(1);
}
/* Launch the kernel */
size_t global_work_size = num_elem;
size_t local_work_size = num_elem;
ret = clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL, &global_work_size, &local_work_size, 0, NULL, NULL);
if (ret != CL_SUCCESS)
{
printf("error: call to 'clEnqueueNDRangeKernel' failed\n");
exit(1);
}
/* Wait for it to finish */
clFinish(command_queue);
/* Read results from GPU */
ret = clEnqueueReadBuffer(command_queue, dst_device_buffer, CL_TRUE,0, num_elem * sizeof(cl_float16), dst_host_buffer, 0, NULL, NULL);
if (ret != CL_SUCCESS)
{
printf("error: call to 'clEnqueueReadBuffer' failed\n");
exit(1);
}
/* Dump dst buffer to file */
char dump_file[100];
sprintf((char *)&dump_file, "%s.result", argv[0]);
write_buffer(dump_file, (const char *)dst_host_buffer, num_elem * sizeof(cl_float16));
printf("Result dumped to %s\n", dump_file);
/* Free host dst buffer */
free(dst_host_buffer);
/* Free device dst buffer */
ret = clReleaseMemObject(dst_device_buffer);
if (ret != CL_SUCCESS)
{
printf("error: call to 'clReleaseMemObject' failed\n");
exit(1);
}
/* Free host side src buffer 0 */
free(src_0_host_buffer);
/* Free device side src buffer 0 */
ret = clReleaseMemObject(src_0_device_buffer);
if (ret != CL_SUCCESS)
{
printf("error: call to 'clReleaseMemObject' failed\n");
exit(1);
}
/* Release kernel */
ret = clReleaseKernel(kernel);
if (ret != CL_SUCCESS)
{
printf("error: call to 'clReleaseKernel' failed\n");
exit(1);
}
/* Release program */
ret = clReleaseProgram(program);
if (ret != CL_SUCCESS)
{
printf("error: call to 'clReleaseProgram' failed\n");
exit(1);
}
/* Release command queue */
ret = clReleaseCommandQueue(command_queue);
if (ret != CL_SUCCESS)
{
printf("error: call to 'clReleaseCommandQueue' failed\n");
exit(1);
}
/* Release context */
ret = clReleaseContext(context);
if (ret != CL_SUCCESS)
{
printf("error: call to 'clReleaseContext' failed\n");
exit(1);
}
return 0;
} |
the_stack_data/248580455.c | char pcap_version[] = "1.7.4";
|
the_stack_data/287272.c | #include<stdio.h>
int main()
{
int a[100000]={},n,k,i=0,j=0,m,l;
scanf("%d",&n);
while(i<n){
scanf("%d",&m);
a[m]++;
i++;
}
i=0;
while(i<100000){
if(a[i]!=0){
l=i;
}
i++;
}
scanf("%d",&k);
i=l,j=0;
while(i>0 && j<k){
//printf("!%d\n",i);
if(a[i]>0) j++;
i--;
}
printf("%d %d",i+1,a[i+1]);
return 0;
} |
the_stack_data/153269122.c | #include<stdio.h>
#include<stdlib.h>
struct listnode {
int data;
struct listnode *next;
};
void add_node(struct listnode **head, int i) {
struct listnode *node = (struct listnode *) malloc(sizeof(struct listnode));
node->next = *head;
node->data = i;
*head = node;
}
void printlist(struct listnode *head) {
while(head) {
printf("%d ", head->data);
head = head->next;
}
return;
}
struct listnode * merge(struct listnode *a, struct listnode *b) {
struct listnode *result = NULL;
if(a == NULL) {
return b;
}
if(b == NULL) {
return a;
}
if(a->data < b->data) {
result = a;
result->next = merge(a->next, b);
}
if(b->data <= a->data) {
result = b;
result->next = merge(a, b->next);
}
return result;
}
void main() {
struct listnode *head1, *head2;
head1 = head2 = NULL;
add_node(&head1, 9);
add_node(&head1, 7);
add_node(&head1, 5);
add_node(&head1, 3);
add_node(&head1, 1);
add_node(&head2, 8);
add_node(&head2, 6);
add_node(&head2, 4);
add_node(&head2, 2);
add_node(&head2, 0);
struct listnode *result = merge(head1, head2);
printlist(result);
}
|
the_stack_data/173577571.c | // Lendo dados do usuário
#include <stdio.h>
int main(void) {
char ip[17]; // Limitando a quantidade de bytes
int porta;
printf("Digite o IP \n");
scanf("%s",&ip);
printf("Digite a Porta \n");
scanf("%i",&porta); // scanf não é seguro! O ideial seria: fgets(ip,17,stdin);
printf("\nVarrendo...\nHost: %s \nPorta: %i",ip,porta);
return 0;
} |
the_stack_data/1090729.c | /*
* C (CeeLanguage) (according to KernighanAndRitchie)
*/
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("Hello, world\n");
return EXIT_SUCCESS;
}
|
the_stack_data/82950442.c | #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define PFIX_CLI "[MTd]"
#define MAX_BUF_SIZE 200
int mtd_server_cmd_run_HOSTNAME(char *msg_cmd, char *message);
/* Get hostname */
int mtd_server_cmd_run_HOSTNAME(char *msg_cmd, char *message)
{
FILE *fd;
char str_cmd[MAX_BUF_SIZE], str_buf[MAX_BUF_SIZE];
memset (str_cmd, 0, MAX_BUF_SIZE);
/* Create a command */
sprintf(str_cmd, "hostname");
/* Run a command */
fd = popen(str_cmd, "r");
if (!fd) {
sprintf(message, "%s_%s ERR%% Error reading hostname.", PFIX_CLI, msg_cmd);
return 1;
}
/* Get command stdout */
while (fgets (str_buf, sizeof (str_buf), fd) <= 0);
pclose (fd);
sprintf(message, "%s_%s OK# Hostname is: %s", PFIX_CLI, msg_cmd, str_buf);
return 0;
}
|
the_stack_data/173578567.c | /* This testcase is part of GDB, the GNU debugger.
Copyright 2005-2015 Free Software Foundation, Inc.
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, see <http://www.gnu.org/licenses/>. */
#include <stdio.h>
int x;
void bar()
{
x--;
}
void foo()
{
x++;
}
int main()
{
foo();
bar();
return 0;
}
|
the_stack_data/347399.c | /*
* =====================================================================================
*
* Filename: 65norma1.c
*
* Description: Norma 1
*
* Version: 1.0
* Created: 06/22/16 21:36:42
* Revision: none
* Compiler: gcc
*
* Author: LTKills
* Organization: USP
*
* =====================================================================================
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main(int argc, char *argv[]){
FILE *fp1, *fp2;
int row, j, k;
double **matA, **matB, sum1 = 0, sum2 = 0, max2 = 0, max1 = 0, a;
char name1[20], name2[20], c = 0;
scanf("%s %s", name1, name2);
fp1 = fopen(name1, "r");
fp2 = fopen(name2, "r");
// Checando as dimensões
for(row = 0; c != '\n'; row++){
fscanf(fp1, "%lf", &a);
fscanf(fp1, "%c", &c);
}
fseek(fp1, 0, SEEK_SET);
// Criando as matrizes A e B
matA = (double**) calloc(row, sizeof(double*));
matB = (double**) calloc(row, sizeof(double*));
for(j = 0; j < row; j++){
matA[j] = (double*) calloc(row, sizeof(double));
matB[j] = (double*) calloc(row, sizeof(double));
}
// Lendo as matrizes
for(j = 0; j < row; j++){
for(k = 0; k < row; k++){
fscanf(fp1, "%lf", &matA[j][k]);
fscanf(fp2, "%lf", &matB[j][k]);
}
}
// Norma 1
for(k = 0; k < row; k++){
for(j = 0; j < row; j++){
sum1 += fabs(matA[j][k]);
sum2 += fabs(matB[j][k]);
}
if(max1 < sum1) max1 = sum1;
if(max2 < sum2) max2 = sum2;
sum1 = 0;
sum2 = 0;
}
printf("%.4lf\n%.4lf\n", max1, max2);
for(j = 0; j < row; j++){
for(k = 0; k < row; k++){
printf("%.4lf ", (matA[j][k]+matB[j][k]));
matB[j][k] += matA[j][k];
}
printf("\n");
}
// Norma 1 da matriz soma
for(k = 0; k < row; k++){
for(j = 0; j < row; j++){
sum2 += fabs(matB[j][k]);
}
if(max2 < sum2) max2 = sum2;
sum2 = 0;
}
printf("%.4lf\n", max2);
fclose(fp1);
fclose(fp2);
for(j = 0; j < row; j++){
free(matA[j]);
free(matB[j]);
}
free(matA);
free(matB);
return 0;
}
|
the_stack_data/95449685.c | #include <stdio.h>
int count[10005][10];
int main(){
int i, j, k, T, n;
int *r;
for (i=1; i<10005; i++) {
for (j=0; j<10; j++)
count[i][j] = count[i-1][j];
for (k=i; k; k/=10)
count[i][k%10]++;
}
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
r = count[n];
printf("%d %d %d %d %d %d %d %d %d %d\n", r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8], r[9]);
}
}
|
the_stack_data/381211.c | #include <stdio.h>
int main(){
int vlr;
scanf("%d", &vlr);
switch (vlr)
{
case 1:
printf("January\n");
break;
case 2:
printf("February\n");
break;
case 3:
printf("March\n");
break;
case 4:
printf("April\n");
break;
case 5:
printf("May\n");
break;
case 6:
printf("June\n");
break;
case 7:
printf("July\n");
break;
case 8:
printf("August\n");
break;
case 9:
printf("September\n");
break;
case 10:
printf("October\n");
break;
case 11:
printf("November\n");
break;
case 12:
printf("December\n");
break;
default:
printf("Valor invalido!\n");
}
return 0;
}
|
the_stack_data/76699333.c | // WARNING in ieee80211_check_rate_mask
// https://syzkaller.appspot.com/bug?id=366ba7ce5ed51ee32a19
// status:0
// autogenerated by syzkaller (https://github.com/google/syzkaller)
#define _GNU_SOURCE
#include <arpa/inet.h>
#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <sched.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/prctl.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <linux/capability.h>
#include <linux/genetlink.h>
#include <linux/if_addr.h>
#include <linux/if_ether.h>
#include <linux/if_link.h>
#include <linux/in6.h>
#include <linux/neighbour.h>
#include <linux/net.h>
#include <linux/netlink.h>
#include <linux/nl80211.h>
#include <linux/rtnetlink.h>
#include <linux/veth.h>
static bool write_file(const char* file, const char* what, ...)
{
char buf[1024];
va_list args;
va_start(args, what);
vsnprintf(buf, sizeof(buf), what, args);
va_end(args);
buf[sizeof(buf) - 1] = 0;
int len = strlen(buf);
int fd = open(file, O_WRONLY | O_CLOEXEC);
if (fd == -1)
return false;
if (write(fd, buf, len) != len) {
int err = errno;
close(fd);
errno = err;
return false;
}
close(fd);
return true;
}
struct nlmsg {
char* pos;
int nesting;
struct nlattr* nested[8];
char buf[4096];
};
static void netlink_init(struct nlmsg* nlmsg, int typ, int flags,
const void* data, int size)
{
memset(nlmsg, 0, sizeof(*nlmsg));
struct nlmsghdr* hdr = (struct nlmsghdr*)nlmsg->buf;
hdr->nlmsg_type = typ;
hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
memcpy(hdr + 1, data, size);
nlmsg->pos = (char*)(hdr + 1) + NLMSG_ALIGN(size);
}
static void netlink_attr(struct nlmsg* nlmsg, int typ, const void* data,
int size)
{
struct nlattr* attr = (struct nlattr*)nlmsg->pos;
attr->nla_len = sizeof(*attr) + size;
attr->nla_type = typ;
if (size > 0)
memcpy(attr + 1, data, size);
nlmsg->pos += NLMSG_ALIGN(attr->nla_len);
}
static int netlink_send_ext(struct nlmsg* nlmsg, int sock, uint16_t reply_type,
int* reply_len)
{
if (nlmsg->pos > nlmsg->buf + sizeof(nlmsg->buf) || nlmsg->nesting)
exit(1);
struct nlmsghdr* hdr = (struct nlmsghdr*)nlmsg->buf;
hdr->nlmsg_len = nlmsg->pos - nlmsg->buf;
struct sockaddr_nl addr;
memset(&addr, 0, sizeof(addr));
addr.nl_family = AF_NETLINK;
unsigned n = sendto(sock, nlmsg->buf, hdr->nlmsg_len, 0,
(struct sockaddr*)&addr, sizeof(addr));
if (n != hdr->nlmsg_len)
exit(1);
n = recv(sock, nlmsg->buf, sizeof(nlmsg->buf), 0);
if (reply_len)
*reply_len = 0;
if (hdr->nlmsg_type == NLMSG_DONE)
return 0;
if (n < sizeof(struct nlmsghdr))
exit(1);
if (reply_len && hdr->nlmsg_type == reply_type) {
*reply_len = n;
return 0;
}
if (n < sizeof(struct nlmsghdr) + sizeof(struct nlmsgerr))
exit(1);
if (hdr->nlmsg_type != NLMSG_ERROR)
exit(1);
return ((struct nlmsgerr*)(hdr + 1))->error;
}
static int netlink_send(struct nlmsg* nlmsg, int sock)
{
return netlink_send_ext(nlmsg, sock, 0, NULL);
}
static int netlink_query_family_id(struct nlmsg* nlmsg, int sock,
const char* family_name)
{
struct genlmsghdr genlhdr;
memset(&genlhdr, 0, sizeof(genlhdr));
genlhdr.cmd = CTRL_CMD_GETFAMILY;
netlink_init(nlmsg, GENL_ID_CTRL, 0, &genlhdr, sizeof(genlhdr));
netlink_attr(nlmsg, CTRL_ATTR_FAMILY_NAME, family_name,
strnlen(family_name, GENL_NAMSIZ - 1) + 1);
int n = 0;
int err = netlink_send_ext(nlmsg, sock, GENL_ID_CTRL, &n);
if (err < 0) {
return -1;
}
uint16_t id = 0;
struct nlattr* attr = (struct nlattr*)(nlmsg->buf + NLMSG_HDRLEN +
NLMSG_ALIGN(sizeof(genlhdr)));
for (; (char*)attr < nlmsg->buf + n;
attr = (struct nlattr*)((char*)attr + NLMSG_ALIGN(attr->nla_len))) {
if (attr->nla_type == CTRL_ATTR_FAMILY_ID) {
id = *(uint16_t*)(attr + 1);
break;
}
}
if (!id) {
return -1;
}
recv(sock, nlmsg->buf, sizeof(nlmsg->buf), 0);
return id;
}
static struct nlmsg nlmsg;
#define WIFI_INITIAL_DEVICE_COUNT 2
#define WIFI_MAC_BASE \
{ \
0x08, 0x02, 0x11, 0x00, 0x00, 0x00 \
}
#define WIFI_IBSS_BSSID \
{ \
0x50, 0x50, 0x50, 0x50, 0x50, 0x50 \
}
#define WIFI_IBSS_SSID \
{ \
0x10, 0x10, 0x10, 0x10, 0x10, 0x10 \
}
#define WIFI_DEFAULT_FREQUENCY 2412
#define WIFI_DEFAULT_SIGNAL 0
#define WIFI_DEFAULT_RX_RATE 1
#define HWSIM_CMD_REGISTER 1
#define HWSIM_CMD_FRAME 2
#define HWSIM_CMD_NEW_RADIO 4
#define HWSIM_ATTR_SUPPORT_P2P_DEVICE 14
#define HWSIM_ATTR_PERM_ADDR 22
#define IF_OPER_UP 6
struct join_ibss_props {
int wiphy_freq;
bool wiphy_freq_fixed;
uint8_t* mac;
uint8_t* ssid;
int ssid_len;
};
static int set_interface_state(const char* interface_name, int on)
{
struct ifreq ifr;
int sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
return -1;
}
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, interface_name);
int ret = ioctl(sock, SIOCGIFFLAGS, &ifr);
if (ret < 0) {
close(sock);
return -1;
}
if (on)
ifr.ifr_flags |= IFF_UP;
else
ifr.ifr_flags &= ~IFF_UP;
ret = ioctl(sock, SIOCSIFFLAGS, &ifr);
close(sock);
if (ret < 0) {
return -1;
}
return 0;
}
static int nl80211_set_interface(struct nlmsg* nlmsg, int sock,
int nl80211_family, uint32_t ifindex,
uint32_t iftype)
{
struct genlmsghdr genlhdr;
memset(&genlhdr, 0, sizeof(genlhdr));
genlhdr.cmd = NL80211_CMD_SET_INTERFACE;
netlink_init(nlmsg, nl80211_family, 0, &genlhdr, sizeof(genlhdr));
netlink_attr(nlmsg, NL80211_ATTR_IFINDEX, &ifindex, sizeof(ifindex));
netlink_attr(nlmsg, NL80211_ATTR_IFTYPE, &iftype, sizeof(iftype));
int err = netlink_send(nlmsg, sock);
if (err < 0) {
return -1;
}
return 0;
}
static int nl80211_join_ibss(struct nlmsg* nlmsg, int sock, int nl80211_family,
uint32_t ifindex, struct join_ibss_props* props)
{
struct genlmsghdr genlhdr;
memset(&genlhdr, 0, sizeof(genlhdr));
genlhdr.cmd = NL80211_CMD_JOIN_IBSS;
netlink_init(nlmsg, nl80211_family, 0, &genlhdr, sizeof(genlhdr));
netlink_attr(nlmsg, NL80211_ATTR_IFINDEX, &ifindex, sizeof(ifindex));
netlink_attr(nlmsg, NL80211_ATTR_SSID, props->ssid, props->ssid_len);
netlink_attr(nlmsg, NL80211_ATTR_WIPHY_FREQ, &(props->wiphy_freq),
sizeof(props->wiphy_freq));
if (props->mac)
netlink_attr(nlmsg, NL80211_ATTR_MAC, props->mac, ETH_ALEN);
if (props->wiphy_freq_fixed)
netlink_attr(nlmsg, NL80211_ATTR_FREQ_FIXED, NULL, 0);
int err = netlink_send(nlmsg, sock);
if (err < 0) {
return -1;
}
return 0;
}
static int get_ifla_operstate(struct nlmsg* nlmsg, int ifindex)
{
struct ifinfomsg info;
memset(&info, 0, sizeof(info));
info.ifi_family = AF_UNSPEC;
info.ifi_index = ifindex;
int sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if (sock == -1) {
return -1;
}
netlink_init(nlmsg, RTM_GETLINK, 0, &info, sizeof(info));
int n;
int err = netlink_send_ext(nlmsg, sock, RTM_NEWLINK, &n);
close(sock);
if (err) {
return -1;
}
struct rtattr* attr = IFLA_RTA(NLMSG_DATA(nlmsg->buf));
for (; RTA_OK(attr, n); attr = RTA_NEXT(attr, n)) {
if (attr->rta_type == IFLA_OPERSTATE)
return *((int32_t*)RTA_DATA(attr));
}
return -1;
}
static int await_ifla_operstate(struct nlmsg* nlmsg, char* interface,
int operstate)
{
int ifindex = if_nametoindex(interface);
while (true) {
usleep(1000);
int ret = get_ifla_operstate(nlmsg, ifindex);
if (ret < 0)
return ret;
if (ret == operstate)
return 0;
}
return 0;
}
static int nl80211_setup_ibss_interface(struct nlmsg* nlmsg, int sock,
int nl80211_family_id, char* interface,
struct join_ibss_props* ibss_props)
{
int ifindex = if_nametoindex(interface);
if (ifindex == 0) {
return -1;
}
int ret = nl80211_set_interface(nlmsg, sock, nl80211_family_id, ifindex,
NL80211_IFTYPE_ADHOC);
if (ret < 0) {
return -1;
}
ret = set_interface_state(interface, 1);
if (ret < 0) {
return -1;
}
ret = nl80211_join_ibss(nlmsg, sock, nl80211_family_id, ifindex, ibss_props);
if (ret < 0) {
return -1;
}
return 0;
}
static int hwsim80211_create_device(struct nlmsg* nlmsg, int sock,
int hwsim_family,
uint8_t mac_addr[ETH_ALEN])
{
struct genlmsghdr genlhdr;
memset(&genlhdr, 0, sizeof(genlhdr));
genlhdr.cmd = HWSIM_CMD_NEW_RADIO;
netlink_init(nlmsg, hwsim_family, 0, &genlhdr, sizeof(genlhdr));
netlink_attr(nlmsg, HWSIM_ATTR_SUPPORT_P2P_DEVICE, NULL, 0);
netlink_attr(nlmsg, HWSIM_ATTR_PERM_ADDR, mac_addr, ETH_ALEN);
int err = netlink_send(nlmsg, sock);
if (err < 0) {
return -1;
}
return 0;
}
static void initialize_wifi_devices(void)
{
uint8_t mac_addr[6] = WIFI_MAC_BASE;
int sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
if (sock < 0) {
return;
}
int hwsim_family_id = netlink_query_family_id(&nlmsg, sock, "MAC80211_HWSIM");
int nl80211_family_id = netlink_query_family_id(&nlmsg, sock, "nl80211");
uint8_t ssid[] = WIFI_IBSS_SSID;
uint8_t bssid[] = WIFI_IBSS_BSSID;
struct join_ibss_props ibss_props = {.wiphy_freq = WIFI_DEFAULT_FREQUENCY,
.wiphy_freq_fixed = true,
.mac = bssid,
.ssid = ssid,
.ssid_len = sizeof(ssid)};
for (int device_id = 0; device_id < WIFI_INITIAL_DEVICE_COUNT; device_id++) {
mac_addr[5] = device_id;
int ret = hwsim80211_create_device(&nlmsg, sock, hwsim_family_id, mac_addr);
if (ret < 0)
exit(1);
char interface[6] = "wlan0";
interface[4] += device_id;
if (nl80211_setup_ibss_interface(&nlmsg, sock, nl80211_family_id, interface,
&ibss_props) < 0)
exit(1);
}
for (int device_id = 0; device_id < WIFI_INITIAL_DEVICE_COUNT; device_id++) {
char interface[6] = "wlan0";
interface[4] += device_id;
int ret = await_ifla_operstate(&nlmsg, interface, IF_OPER_UP);
if (ret < 0)
exit(1);
}
close(sock);
}
static long syz_genetlink_get_family_id(volatile long name)
{
struct nlmsg nlmsg_tmp;
int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
if (fd == -1) {
return -1;
}
int ret = netlink_query_family_id(&nlmsg_tmp, fd, (char*)name);
close(fd);
if (ret < 0) {
return -1;
}
return ret;
}
static void setup_common()
{
if (mount(0, "/sys/fs/fuse/connections", "fusectl", 0, 0)) {
}
}
static void loop();
static void sandbox_common()
{
prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
setsid();
struct rlimit rlim;
rlim.rlim_cur = rlim.rlim_max = (200 << 20);
setrlimit(RLIMIT_AS, &rlim);
rlim.rlim_cur = rlim.rlim_max = 32 << 20;
setrlimit(RLIMIT_MEMLOCK, &rlim);
rlim.rlim_cur = rlim.rlim_max = 136 << 20;
setrlimit(RLIMIT_FSIZE, &rlim);
rlim.rlim_cur = rlim.rlim_max = 1 << 20;
setrlimit(RLIMIT_STACK, &rlim);
rlim.rlim_cur = rlim.rlim_max = 0;
setrlimit(RLIMIT_CORE, &rlim);
rlim.rlim_cur = rlim.rlim_max = 256;
setrlimit(RLIMIT_NOFILE, &rlim);
if (unshare(CLONE_NEWNS)) {
}
if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL)) {
}
if (unshare(CLONE_NEWIPC)) {
}
if (unshare(0x02000000)) {
}
if (unshare(CLONE_NEWUTS)) {
}
if (unshare(CLONE_SYSVSEM)) {
}
typedef struct {
const char* name;
const char* value;
} sysctl_t;
static const sysctl_t sysctls[] = {
{"/proc/sys/kernel/shmmax", "16777216"},
{"/proc/sys/kernel/shmall", "536870912"},
{"/proc/sys/kernel/shmmni", "1024"},
{"/proc/sys/kernel/msgmax", "8192"},
{"/proc/sys/kernel/msgmni", "1024"},
{"/proc/sys/kernel/msgmnb", "1024"},
{"/proc/sys/kernel/sem", "1024 1048576 500 1024"},
};
unsigned i;
for (i = 0; i < sizeof(sysctls) / sizeof(sysctls[0]); i++)
write_file(sysctls[i].name, sysctls[i].value);
}
static int wait_for_loop(int pid)
{
if (pid < 0)
exit(1);
int status = 0;
while (waitpid(-1, &status, __WALL) != pid) {
}
return WEXITSTATUS(status);
}
static void drop_caps(void)
{
struct __user_cap_header_struct cap_hdr = {};
struct __user_cap_data_struct cap_data[2] = {};
cap_hdr.version = _LINUX_CAPABILITY_VERSION_3;
cap_hdr.pid = getpid();
if (syscall(SYS_capget, &cap_hdr, &cap_data))
exit(1);
const int drop = (1 << CAP_SYS_PTRACE) | (1 << CAP_SYS_NICE);
cap_data[0].effective &= ~drop;
cap_data[0].permitted &= ~drop;
cap_data[0].inheritable &= ~drop;
if (syscall(SYS_capset, &cap_hdr, &cap_data))
exit(1);
}
static int do_sandbox_none(void)
{
if (unshare(CLONE_NEWPID)) {
}
int pid = fork();
if (pid != 0)
return wait_for_loop(pid);
setup_common();
sandbox_common();
drop_caps();
if (unshare(CLONE_NEWNET)) {
}
initialize_wifi_devices();
loop();
exit(1);
}
uint64_t r[7] = {0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff,
0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff,
0xffffffffffffffff};
void loop(void)
{
intptr_t res = 0;
res = syscall(__NR_socket, 0x10ul, 3ul, 0x10);
if (res != -1)
r[0] = res;
memcpy((void*)0x20000200, "nl80211\000", 8);
res = -1;
res = syz_genetlink_get_family_id(0x20000200);
if (res != -1)
r[1] = res;
memcpy((void*)0x20000700, "wlan0\000\000\000\000\000\000\000\000\000\000\000",
16);
res = syscall(__NR_ioctl, r[0], 0x8933, 0x20000700ul);
if (res != -1)
r[2] = *(uint32_t*)0x20000710;
*(uint64_t*)0x20000340 = 0;
*(uint32_t*)0x20000348 = 0;
*(uint64_t*)0x20000350 = 0x20000300;
*(uint64_t*)0x20000300 = 0x20000240;
*(uint32_t*)0x20000240 = 0x24;
*(uint16_t*)0x20000244 = r[1];
*(uint16_t*)0x20000246 = 5;
*(uint32_t*)0x20000248 = 0;
*(uint32_t*)0x2000024c = 0;
*(uint8_t*)0x20000250 = 6;
*(uint8_t*)0x20000251 = 0;
*(uint16_t*)0x20000252 = 0;
*(uint16_t*)0x20000254 = 8;
*(uint16_t*)0x20000256 = 3;
*(uint32_t*)0x20000258 = r[2];
*(uint16_t*)0x2000025c = 8;
*(uint16_t*)0x2000025e = 5;
*(uint32_t*)0x20000260 = 3;
*(uint64_t*)0x20000308 = 0x24;
*(uint64_t*)0x20000358 = 1;
*(uint64_t*)0x20000360 = 0;
*(uint64_t*)0x20000368 = 0;
*(uint32_t*)0x20000370 = 0;
syscall(__NR_sendmsg, r[0], 0x20000340ul, 0ul);
*(uint64_t*)0x20000340 = 0;
*(uint32_t*)0x20000348 = 0;
*(uint64_t*)0x20000350 = 0x20000300;
*(uint64_t*)0x20000300 = 0x20000380;
memcpy((void*)0x20000380, "\x00\x03\x00\x00", 4);
*(uint16_t*)0x20000384 = r[1];
memcpy((void*)0x20000386, "\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0f\x00"
"\x00\x00\x08\x00\x03\x00",
18);
*(uint32_t*)0x20000398 = r[2];
memcpy(
(void*)0x2000039c,
"\x28\x00\x0e\x00\x80\x00\x00\x00\xff\xff\xff\xff\xff\xff\x08\x02\x11\x00"
"\x00\x01\x4c\x88\xe8\xeb\x7e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
"\x64\x00\xff\xff\x08\x00\x26\x00\x6c\x09\x00\x00\x08\x00\x0c\x00\x64\x00"
"\x00\x00\x08\x00\x0d\x00\x00\x00\x00\x00\x0a\x00\x34\x00\x02\x02\x02\x02"
"\x02\x02\x00\x00\x08\x00\x35\x00\x00\x00\x00\x00\x90\x02\x0f\x00\x04\x4e"
"\x79\xc1\x99\xe8\x3b\xed\x43\x6d\x9b\x18\xd6\x35\xd7\xd6\xe8\x00\x08\x00"
"\x00\x88\x54\x41\x3f\x64\xd1\x7b\x8d\x7a\x90\x41\xea\x1a\xa6\xcb\x32\x8e"
"\x4e\xae\x74\xed\xc1\xe0\xad\x5b\x56\x00\x31\x9a\x0c\xf9\x6d\x64\xa3\x28"
"\xee\xca\xfa\xc1\x12\xed\x3e\xc8\x84\xa1\x53\x1d\xfd\xf1\x7a\x1c\xbe\x07"
"\xe1\x3a\xd5\x79\x11\xd1\x2d\x26\x9b\x02\x2c\xcf\x8f\xf9\xaf\x66\xb4\x1d"
"\x84\xa1\xf4\x7c\xd1\x6e\xce\xac\xcf\x5e\xf9\x15\x9b\x14\xc6\xd0\x25\xae"
"\xa1\x2d\xba\xe5\x03\xb9\x48\x22\xfd\x6a\x1c\x7b\xbb\x4c\x9d\x1e\xff\xa4"
"\x6a\x99\xc6\x02\x5a\xba\x12\x6a\xb4\xd7\x43\x3b\x72\x2b\x8c\xf4\x0f\xaa"
"\xb9\x1f\x78\x6c\x32\x81\x3e\xc3\x1c\xbb\xec\xee\xeb\xe4\x4f\xb3\x52\x7d"
"\x98\x35\x6b\x7c\xef\x74\x53\xa6\xd0\xbc\x7c\x65\x73\xe1\xe9\x31\x55\x7e"
"\x85\x94\x57\x2f\xe5\x20\xd0\xb3\x90\x3b\xd7\xe8\x5e\xc4\xab\xc6\x4e\x96"
"\x4b\x56\x55\xb2\x40\xa4\xe6\x02\x0c\x7a\x9f\x72\xb1\x6a\xf0\xfb\xcd\xcb"
"\x35\x4e\x91\xc6\x9c\x1a\x6b\xa7\x57\x64\x92\x63\xf8\xb0\xbc\x97\xce\x3f"
"\xfc\x1f\xff\xa6\x04\x7b\xb4\xa1\x8a\xad\x45\xa0\xfa\x6f\x1c\xf4\x40\x8c"
"\x8d\xf7\x9b\xce\xdd\xae\xff\xb0\x50\x1f\x61\x5f\xeb\x70\x80\x1a\xac\x79"
"\xa1\xb3\xb7\x40\x01\x06\x6b\xfe\x7b\x65\x2c\x5b\xa9\xe6\xdf\x56\x06\x10"
"\xaf\xa7\xb2\xb0\xae\x89\xd8\x16\x7e\xf9\x5c\xab\x5f\x20\x73\x20\x67\x98"
"\x08\x9f\x72\x5a\x5d\x9f\x20\x7a\x14\x09\x90\x61\x12\xda\x7e\x52\xb9\x0d"
"\x5e\xa3\x45\x37\xd6\xd6\xce\x24\xa2\xdb\x63\x14\x41\xbe\x1e\x6d\x63\x23"
"\xd6\x16\x55\xd4\x15\x6b\x50\x6f\xdb\xe3\xf6\xc9\x81\x35\x28\xa9\x50\xdf"
"\x8a\x4c\x2a\x6a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
467);
*(uint64_t*)0x20000308 = 0x300;
*(uint64_t*)0x20000358 = 1;
*(uint64_t*)0x20000360 = 0;
*(uint64_t*)0x20000368 = 0;
*(uint32_t*)0x20000370 = 0;
syscall(__NR_sendmsg, r[0], 0x20000340ul, 0ul);
res = syscall(__NR_socket, 0x10ul, 3ul, 0x10);
if (res != -1)
r[3] = res;
res = syscall(__NR_socket, 0x10ul, 3ul, 0x10);
if (res != -1)
r[4] = res;
memcpy((void*)0x20000700, "wlan0\000\000\000\000\000\000\000\000\000\000\000",
16);
res = syscall(__NR_ioctl, r[4], 0x8933, 0x20000700ul);
if (res != -1)
r[5] = *(uint32_t*)0x20000710;
memcpy((void*)0x20000200, "nl80211\000", 8);
res = -1;
res = syz_genetlink_get_family_id(0x20000200);
if (res != -1)
r[6] = res;
*(uint64_t*)0x20000140 = 0;
*(uint32_t*)0x20000148 = 0;
*(uint64_t*)0x20000150 = 0x20000100;
*(uint64_t*)0x20000100 = 0x20000740;
memcpy((void*)0x20000740, "8\000\000\000", 4);
*(uint16_t*)0x20000744 = r[6];
memcpy((void*)0x20000746, "\x01\x00\xb3\x00\x00\x00\x08\x00\x00\x00\x19\x8f"
"\x00\x00\x08\x00\x03\x00",
18);
*(uint32_t*)0x20000758 = r[5];
memcpy((void*)0x2000075c, "\x05\x00\x24", 3);
*(uint64_t*)0x20000108 = 0x38;
*(uint64_t*)0x20000158 = 1;
*(uint64_t*)0x20000160 = 0;
*(uint64_t*)0x20000168 = 0;
*(uint32_t*)0x20000170 = 0;
syscall(__NR_sendmsg, r[3], 0x20000140ul, 0ul);
}
int main(void)
{
syscall(__NR_mmap, 0x1ffff000ul, 0x1000ul, 0ul, 0x32ul, -1, 0ul);
syscall(__NR_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x32ul, -1, 0ul);
syscall(__NR_mmap, 0x21000000ul, 0x1000ul, 0ul, 0x32ul, -1, 0ul);
do_sandbox_none();
return 0;
}
|
the_stack_data/567029.c | #ifndef EM_PORT_API
# if defined(__EMSCRIPTEN__)
# include <emscripten.h>
# if defined(__cplusplus)
# define EM_PORT_API(rettype) extern "C" rettype EMSCRIPTEN_KEEPALIVE
# else
# define EM_PORT_API(rettype) rettype EMSCRIPTEN_KEEPALIVE
# endif
# else
# if defined(__cplusplus)
# define EM_PORT_API(rettype) extern "C" rettype
# else
# define EM_PORT_API(rettype) rettype
# endif
# endif
#endif
EM_PORT_API(double) sumDouble(double *array, int n) {
double s = 0.0;
for (int i = 0; i < n; i++) {
s += array[i];
}
return s;
} |
the_stack_data/380199.c | /* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalpha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abiri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/10/04 11:29:37 by abiri #+# #+# */
/* Updated: 2018/10/09 23:30:29 by abiri ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isalpha(int c)
{
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
return (1);
return (0);
}
|
the_stack_data/48576318.c | // https://codecast.france-ioi.org/v6/player?stepperControls=_undo,_redo,_expr,_out,_over&base=https%3A%2F%2Ffioi-recordings.s3.amazonaws.com%2Fdartmouth%2F1519072828031
#include <stdio.h>
void myFunction(int *, double *, char *);
int main(void) {
//! showMemory(start=65520)
int i = 42;
int *iAdr = &i;
double a = 3.14;
double *aAdr = &a;
char c = 'p';
char *cAdr;
cAdr = &c;
printf("i = %d and its address is %p.\n", i, iAdr);
printf("a = %lf and its address is %p.\n", a, aAdr);
printf("c = %c and its address is %p.\n", c, cAdr);
myFunction(iAdr, aAdr, cAdr);
return (0);
}
void myFunction(int *iptr, double *aptr, char *cptr) {
printf("Function receied addresses %p, %p and %p.\n", iptr, aptr, cptr);
}
|
the_stack_data/121505.c | /* Frugal Search */
#include <stdio.h>
#include <string.h>
char dic[200][50];
int check[200][50];
char query[200];
char q[50][200];
int nq;
int n;
int str_cmp(char *a, char *b) {
return (strcmp(a, b));
}
int satisfy(int num, char *str) {
int i;
char v;
v = 0;
for (i = 0; str[i] != '\0'; ) {
if (str[i] == '+') {
if (check[num][str[i+1]-'a'] == 0) return (0);
i += 2;
}
else if (str[i] == '-') {
if (check[num][str[i+1]-'a'] != 0) return (0);
i += 2;
}
else {
if (check[num][str[i]-'a'] != 0) v = 1;
i++;
}
}
if (v) return (1);
else return (0);
}
int main() {
int i, j;
char result[50];
int str_cmp();
int get;
while (1) {
n = 0;
while (1) {
gets(dic[n]);
if ((dic[n][0] == '*') || (dic[n][0] == '#')) break;
n++;
}
if (dic[0][0] == '#') break;
qsort(dic, n, sizeof(dic[0]), str_cmp);
for (i = 0; i < n; i++) {
for (j = 0; j < 50; j++) check[i][j] = 0;
for (j = 0; dic[i][j] != '\0'; j++) check[i][dic[i][j]-'a'] = 1;
}
while (1) {
gets(query);
if (query[0] == '*') break;
nq = 0; j = 0;
for (i = 0; query[i] != '\0'; i++) {
if (query[i] == '|') {
q[nq][j] = '\0';
j = 0;
nq++;
}
else q[nq][j++] = query[i];
}
q[nq++][j] = '\0';
get = 0;
for (i = 0; (i < n) && (!get); i++)
for (j = 0; j < nq; j++) {
if (satisfy(i, q[j])) {
get = 1;
strcpy(result, dic[i]);
break;
}
}
if (get) printf("%s\n", result);
else printf("NONE\n");
}
printf("$\n");
}
return 0;
}
|
the_stack_data/184518139.c | // a is on the stack
int fn(short rdi, short rsi, short rdx, short rcx, short r8, short r9, short a) {
return rdi + rsi + rdx + rcx + r8 + r9 + a;
}
int main() {
fn(0x123, 0x456, 0x789, 0xabc, 0xdef, 0xfed, 0xcba);
return 0;
}
|
the_stack_data/106243.c | #include <stdio.h>
void heapify(int *unsorted, int index, int heap_size);
void heap_sort(int *unsorted, int n);
int main() {
int n = 0;
int i = 0;
char oper;
int* unsorted;
printf("Enter the size of the array you want\n");
scanf("%d", &n);
unsorted = (int*)malloc(sizeof(int) * n);
while (getchar() != '\n');
printf("Enter numbers separated by a comma:\n");
while (i != n) {
scanf("%d,", (unsorted + i));
i++;
}
heap_sort(unsorted, n);
printf("[");
printf("%d", *(unsorted));
for (int i = 1; i < n; i++) {
printf(", %d", *(unsorted + i));
}
printf("]");
}
void heapify(int *unsorted, int index, int heap_size) {
int temp;
int largest = index;
int left_index = 2 * index;
int right_index = 2 * index + 1;
if (left_index < heap_size && *(unsorted + left_index) > *(unsorted + largest)) {
largest = left_index;
}
if (right_index < heap_size && *(unsorted + right_index) > *(unsorted + largest)) {
largest = right_index;
}
if (largest != index) {
temp = *(unsorted + largest);
*(unsorted + largest) = *(unsorted + index);
*(unsorted + index) = temp;
heapify(unsorted, largest, heap_size);
}
}
void heap_sort(int *unsorted, int n) {
int temp;
for (int i = n / 2 - 1; i > -1; i--) {
heapify(unsorted, i, n);
}
for (int i = n - 1; i > 0; i--) {
temp = *(unsorted);
*(unsorted) = *(unsorted + i);
*(unsorted + i) = temp;
heapify(unsorted, 0, i);
}
}
|
the_stack_data/14199333.c | #include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <fcntl.h> /* Definition of AT_* constants */
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h> /* Definition of AT_* constants */
#include <unistd.h>
//Process P1
int main(int argc, char const *argv[])
{
int fd;
mkfifo("desdfifo", S_IRUSR | S_IWUSR); //to create FIFO
fd = open("desdfifo", O_WRONLY);
write(fd, "DESD\n", 5);
close(fd);
unlink("desdfifo"); //to remove Named FIFO
return 0;
}
|
the_stack_data/346011.c | /*
#
# ----------------------------------------------------------------------------
#
# Copyright 2019 IBM Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ----------------------------------------------------------------------------
#
*/
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
int main(int argc, const char **argv) {
if(argc != 2){
printf("Usage: chop-detrace <bin_file>\n");
if (argc == 1) { exit(0); }
exit(-1);
}
FILE *fp = fopen(argv[1], "r");
assert(fp);
long dat;
int trace_id = 0;
while (fread(&dat, sizeof(long), 1, fp) > 0) {
switch (dat) {
case -1: printf("# trace %d\n", trace_id); break;
case -2: ++trace_id; break;
default: printf("%lx\n", dat);
};
}
}
|
the_stack_data/361757.c | //
// aes.c
//
// Created by Tom Corwine on 10/14/13.
//
#include <stdio.h>
|
the_stack_data/1007961.c | //
// Created by nicholas on 2021/2/24.
//
#include<stdio.h>
int main(void) {
int *p = NULL;
printf("%d \n", *p);
return 0;
}
|
the_stack_data/242330651.c | /* Copyright 2000 by Abacus Research and
* Development, Inc. All rights reserved.
*/
#if defined (CFM_PROBLEMS)
#warning "No CFM support for now, even though it limped previously."
#elif defined (powerpc)
/*
* In addition to fleshing this out, it probably makes sense to use mmap
* to pull in certain sections (including temporarily the loader section).
*/
#include "rsys/common.h"
#include <sys/mman.h>
#include <assert.h>
#include "FileMgr.h"
#include "OSUtil.h"
#include "MemoryMgr.h"
#include "SegmentLdr.h"
#include "AliasMgr.h"
#include "rsys/cfm.h"
#include "rsys/pef.h"
#include "rsys/file.h"
#include "rsys/interfacelib.h"
#include "rsys/mathlib.h"
#include "rsys/launch.h"
#include "rsys/hfs.h"
#include "rsys/string.h"
#include "ppc_stubs.h"
typedef enum
{
process_share = 1,
global_share = 4,
protected_share = 5,
}
share_kind_t;
enum
{
code_section_type = 0,
unpacked_data_section_type,
pattern_data_section_type,
constant_section_type,
loader_section_type,
debug_section_type,
executable_data_section_type,
exception_section_type,
traceback_section_type,
};
#undef roundup
#define roundup(n, m) \
({ \
typeof (n) __n; \
typeof (m) __m; \
\
__n = (n); \
__m = (m); \
(__n + __m - 1) / m * m; \
})
#undef rounddown
#define rounddown(n, m) \
({ \
typeof (n) __n; \
typeof (m) __m; \
\
__n = (n); \
__m = (m); \
__n / m * m; \
})
PRIVATE OSErr
try_to_get_memory (void **addrp, syn68k_addr_t default_syn_address,
uint32 total_size, int alignment)
{
OSErr retval;
#if !defined(linux)
retval = paramErr;
#else
void *default_address;
void *received_address;
default_address = SYN68K_TO_US (default_syn_address);
total_size = roundup (total_size, getpagesize());
received_address = mmap (default_address, total_size,
PROT_EXEC|PROT_READ|PROT_WRITE,
MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
if (received_address == (void *) -1)
retval = memFullErr;
else
{
retval = noErr;
*addrp = received_address;
}
#endif
return retval;
}
enum
{
readable_section = (1 << 0),
writable_section = (1 << 1),
executable_section = (1 << 2),
};
PRIVATE OSErr
load_unpacked_section (const void *mmapped_addr,
syn68k_addr_t default_address, uint32 total_size,
uint32 packed_size, uint32 unpacked_size,
uint32 section_offset, share_kind_t share_kind,
int alignment, section_info_t *infop)
{
OSErr retval;
void *addr;
if (packed_size != unpacked_size)
warning_unexpected ("%d %d", packed_size, unpacked_size);
if (!(infop->perms & writable_section) && total_size == unpacked_size)
{
retval = noErr;
addr = (char *) mmapped_addr + section_offset;
}
else
{
retval = try_to_get_memory (&addr, default_address, total_size,
alignment);
if (retval == noErr)
{
memcpy (addr, (char *) mmapped_addr + section_offset, packed_size);
memset (addr + unpacked_size, 0, total_size - unpacked_size);
}
}
if (retval == noErr)
{
infop->start = US_TO_SYN68K (addr);
infop->length = total_size;
}
return retval;
}
PRIVATE OSErr
repeat_block (uint32 repeat_count, uint32 block_size, uint8 **destpp,
const uint8 **srcpp, uint32 *dest_lengthp, uint32 *src_lengthp)
{
OSErr retval;
uint32 total_bytes_to_write;
total_bytes_to_write = repeat_count * block_size;
if (total_bytes_to_write > *dest_lengthp || block_size > *src_lengthp)
retval = paramErr;
else
{
while (repeat_count-- > 0)
{
memcpy (*destpp, *srcpp, block_size);
*destpp += block_size;
}
*srcpp += block_size;
*dest_lengthp -= total_bytes_to_write;
*src_lengthp -= block_size;
retval = noErr;
}
return retval;
}
PRIVATE OSErr
extract_count (const uint8 **srcpp, uint32 *lengthp, uint32 *countp)
{
OSErr retval;
uint32 count;
uint8 next_piece;
count = *countp;
do
{
retval = (*lengthp > 0) ? noErr : paramErr;
if (retval == noErr)
{
next_piece = **srcpp;
++*srcpp;
--*lengthp;
count = (count << 7) | (next_piece & 0x7F);
}
}
while (retval == noErr && (next_piece & 0x80));
if (retval == noErr)
*countp = count;
return retval;
}
PRIVATE OSErr
interleave (uint32 repeat_count, uint32 custom_size, uint8 **destpp,
const uint8 **srcpp, uint32 *dest_lengthp, uint32 *src_lengthp,
uint32 common_size, const uint8 *common_mem)
{
OSErr retval;
uint32 total_size;
total_size = repeat_count * (common_size + custom_size) + common_size;
if (total_size > *dest_lengthp || custom_size * repeat_count > *src_lengthp)
retval = paramErr;
else
{
++repeat_count;
while (repeat_count-- > 0)
{
if (common_mem)
memcpy (*destpp, common_mem, common_size);
else
memset (*destpp, 0, common_size);
*destpp += common_size;
*dest_lengthp -= common_size;
if (repeat_count > 0)
{
memcpy (*destpp, *srcpp, custom_size);
*destpp += custom_size;
*dest_lengthp -= custom_size;
*srcpp += custom_size;
*src_lengthp -= custom_size;
}
}
retval = noErr;
}
return retval;
}
PRIVATE OSErr
pattern_initialize (uint8 *addr, const uint8 *patmem, uint32 packed_size,
uint32 unpacked_size)
{
OSErr retval;
retval = noErr;
while (retval == noErr && packed_size > 0)
{
uint8 opcode;
uint32 count;
opcode = *patmem >> 5;
count = (*patmem & 0x1f);
++patmem;
--packed_size;
if (!count)
retval = extract_count (&patmem, &packed_size, &count);
if (retval == noErr)
{
switch (opcode)
{
case 0:
if (count > unpacked_size)
retval = paramErr;
else
{
memset (addr, 0, count);
addr += count;
unpacked_size -= count;
}
break;
case 1:
retval = repeat_block (1, count, &addr, &patmem, &unpacked_size,
&packed_size);
break;
case 2:
{
uint32 repeat_count;
repeat_count = 0;
retval = extract_count (&patmem, &packed_size, &repeat_count);
if (retval == noErr)
retval = repeat_block (repeat_count + 1, count, &addr,
&patmem, &unpacked_size,
&packed_size);
}
break;
case 3:
case 4:
{
uint32 common_size;
uint32 custom_size;
common_size = count;
custom_size = 0;
retval = extract_count (&patmem, &packed_size, &custom_size);
if (retval == noErr)
{
uint32 repeat_count;
repeat_count = 0;
retval = extract_count (&patmem, &packed_size,
&repeat_count);
if (retval == noErr)
{
const uint8 *common_mem;
if (opcode == 4)
common_mem = 0;
else
{
common_mem = patmem;
if (common_size > packed_size)
retval = paramErr;
else
{
patmem += common_size;
packed_size -= common_size;
}
}
if (retval == noErr)
interleave (repeat_count, custom_size, &addr,
&patmem, &unpacked_size, &packed_size,
common_size, common_mem);
}
}
}
break;
default:
warning_unexpected ("%d", opcode);
assert (0);
break;
}
}
}
if (retval == noErr && (packed_size || unpacked_size))
{
retval = paramErr;
warning_unexpected ("%d %d", packed_size, unpacked_size);
assert (0);
}
return retval;
}
PRIVATE OSErr
load_pattern_section (const void *mmapped_addr,
syn68k_addr_t default_address, uint32 total_size,
uint32 packed_size, uint32 unpacked_size,
uint32 section_offset, share_kind_t share_kind,
int alignment, section_info_t *infop)
{
OSErr retval;
void *addr;
retval = try_to_get_memory (&addr, default_address, total_size, alignment);
if (retval == noErr)
{
uint8 *patmem;
patmem = (typeof (patmem)) ((char *) mmapped_addr + section_offset);
retval = pattern_initialize (addr, patmem, packed_size, unpacked_size);
if (retval == noErr)
{
memset (addr + unpacked_size, 0,
total_size - unpacked_size);
infop->start = US_TO_SYN68K (addr);
infop->length = total_size;
}
}
return retval;
}
PRIVATE void
repeatedly_relocate (uint32 count, uint8 **relocAddressp, uint32 val)
{
uint32 *p;
p = (uint32 *) *relocAddressp;
while (count-- > 0)
{
*p = CL (CL (*p) + val);
++p;
}
*relocAddressp = (uint8 *) p;
}
PRIVATE OSErr
check_existing_connections (Str63 library, OSType arch, LoadFlags loadflags,
ConnectionID *cidp, Ptr *mainaddrp, Str255 errName)
{
/* TODO */
#warning TODO
return fragLibNotFound;
}
PRIVATE void
get_root_and_app (INTEGER *root_vrefp, LONGINT *root_diridp,
INTEGER *app_vrefp , LONGINT *app_diridp)
{
#warning TODO
/* TODO */
}
PRIVATE OSErr
check_file (INTEGER vref, LONGINT dirid, Str255 file, boolean_t shlb_test_p,
Str63 library, OSType arch, LoadFlags loadflags,
ConnectionID *cidp, Ptr *mainaddrp, Str255 errName)
{
OSErr retval;
FSSpec fs;
retval = FSMakeFSSpec (vref, dirid, file, &fs);
if (retval != noErr)
retval = fragLibNotFound;
else
{
if (shlb_test_p)
{
OSErr err;
FInfo finfo;
err = FSpGetFInfo (&fs, &finfo);
if (err != noErr || finfo.fdType != TICK("shlb"))
retval = fragLibNotFound;
}
if (retval == noErr)
{
INTEGER rn;
rn = FSpOpenResFile (&fs, fsRdPerm);
if (rn == -1)
retval = fragLibNotFound;
else
{
Handle cfrg0;
cfrg0 = Get1Resource (T('c','f','r','g'), 0);
if (!cfrg0)
retval = fragLibNotFound;
else
{
cfir_t *cfirp;
cfirp = ROMlib_find_cfrg (cfrg0, arch, kImportLibraryCFrag,
library);
if (!cfirp)
retval = fragLibNotFound;
else
retval = GetDiskFragment (&fs,
CFIR_OFFSET_TO_FRAGMENT (cfirp),
CFIR_FRAGMENT_LENGTH (cfirp), "",
loadflags, cidp, mainaddrp,
errName);
}
CloseResFile (rn);
}
}
}
return retval;
}
PRIVATE OSErr
check_vanddir (INTEGER vref, LONGINT dirid, int descend_count, Str63 library,
OSType arch, LoadFlags loadflags, ConnectionID *cidp,
Ptr *mainaddrp, Str255 errName)
{
CInfoPBRec pb;
Str255 s;
OSErr retval;
INTEGER dirindex;
OSErr err;
int errcount;
retval = fragLibNotFound;
pb.hFileInfo.ioNamePtr = RM (&s[0]);
pb.hFileInfo.ioVRefNum = CW (vref);
err = noErr;
errcount = 0;
for (dirindex = 1;
retval != noErr && err != fnfErr && errcount != 3;
dirindex++)
{
pb.hFileInfo.ioFDirIndex = CW (dirindex);
pb.hFileInfo.ioDirID = CL (dirid);
err = PBGetCatInfo(&pb, FALSE);
if (err)
{
if (err != fnfErr)
{
warning_unexpected ("PBGetCatInfo err = %d\n", err);
++errcount;
}
}
else
{
errcount = 0;
if (pb.hFileInfo.ioFlAttrib & ATTRIB_ISADIR)
{
if (descend_count > 0)
{
retval = check_vanddir (vref, CL (pb.hFileInfo.ioDirID),
descend_count-1, library,
arch, loadflags, cidp, mainaddrp,
errName);
}
}
else if (pb.hFileInfo.ioFlFndrInfo.fdType == TICKX ("shlb"))
retval = check_file (vref, dirid, s, FALSE, library, arch,
loadflags, cidp, mainaddrp, errName);
}
}
return retval;
}
/*
* This sort of implements the "Searching for Import Libraries" algorithm
* described on 1-17 and 1-18 of the Mac Runtime Architectures manual. The
* biggest difference is that we don't search for the best fit between
* everything available in the Extensions folder, the ROM registry and the
* file registry. Instead we currently stop when we find any fit.
*
* Additionally, we don't really have a ROM registry per-se. Instead
* we have the home-brewed InterfaceLib and MathLib files. We don't
* have anything corresponding to the file registry.
*
*/
P6 (PUBLIC pascal trap, OSErr, GetSharedLibrary, Str63, library, OSType, arch,
LoadFlags, loadflags, ConnectionID *, cidp, Ptr *, mainaddrp, Str255,
errName)
{
OSErr retval;
warning_trace_info ("GetSharedLibrary (\"%.*s\")\n", library[0],
library+1);
retval = check_existing_connections (library, arch, loadflags, cidp,
mainaddrp, errName);
if (retval == fragLibNotFound)
{
INTEGER root_vref, app_vref;
LONGINT root_dirid, app_dirid;
get_root_and_app (&root_vref, &root_dirid, &app_vref, &app_dirid);
if (root_vref != app_vref || root_dirid != app_dirid)
retval = check_vanddir (root_vref, root_dirid, 0, library, arch,
loadflags, cidp, mainaddrp, errName);
if (retval != noErr)
retval = check_file (ROMlib_exevrefnum, 0, ROMlib_exefname, FALSE,
library, arch, loadflags, cidp, mainaddrp,
errName);
if (retval != noErr)
retval = check_vanddir (app_vref, app_dirid, 0, library, arch,
loadflags, cidp, mainaddrp, errName);
if (retval != noErr)
retval = check_vanddir (ROMlib_exevrefnum, 0, 0, library, arch,
loadflags, cidp, mainaddrp, errName);
if (retval != noErr)
{
INTEGER extensions_vref;
LONGINT extensions_dirid;
if (FindFolder (0, kExtensionFolderType, FALSE, &extensions_vref,
&extensions_dirid) == noErr)
retval = check_vanddir (extensions_vref, extensions_dirid, 1,
library, arch, loadflags, cidp,
mainaddrp, errName);
}
if (retval != noErr)
{
if (EqualString (library, "\7MathLib", FALSE, TRUE))
retval = ROMlib_GetMathLib (library, arch, loadflags, cidp,
mainaddrp, errName);
else if (EqualString (library, "\14InterfaceLib", FALSE, TRUE))
retval = ROMlib_GetInterfaceLib (library, arch, loadflags, cidp,
mainaddrp, errName);
}
}
return retval;
}
P1 (PUBLIC pascal trap, OSErr, CloseConnection, ConnectionID *, cidp)
{
warning_trace_info ("cidp = %p, cid = 0x%x", cidp, (uint32) *cidp);
return noErr;
}
enum { tracking_val_start = 0x88123456 };
PRIVATE uint32 num_tracking_vals;
PRIVATE char **tracking_symbols;
PRIVATE void
tracking_handler (int signum, struct sigcontext sc)
{
uint32 r12;
uint32 val;
r12 = sc.regs->gpr[PT_R12];
val = r12 - (uint32) tracking_val_start;
if (val < num_tracking_vals)
fprintf (stderr, "Need glue for '%s'\n", tracking_symbols[val]);
ExitToShell ();
}
PUBLIC void
ROMlib_release_tracking_values (void)
{
if (num_tracking_vals > 0)
{
int i;
for (i = 0; i < num_tracking_vals; ++i)
free (tracking_symbols[i]);
tracking_symbols = realloc (tracking_symbols, 0);
num_tracking_vals = 0;
}
signal (SIGSEGV, (void *) tracking_handler);
}
PRIVATE uint32
tracking_value (const char *symbol_name)
{
uint32 retval;
tracking_symbols = realloc (tracking_symbols,
(num_tracking_vals+1) *
sizeof *tracking_symbols);
tracking_symbols[num_tracking_vals] = strdup (symbol_name);
retval = tracking_val_start + num_tracking_vals++;
warning_trace_info ("name = '%s' (%p)\n", symbol_name, (Ptr) retval);
return retval;
}
PRIVATE OSErr
symbol_lookup (uint32 *indexp, Ptr *valp, uint8 imports[][4],
const char *symbol_names, CFragClosureID closure_id)
{
OSErr retval;
int index;
uint8 flags;
uint8 class;
const char *symbol_name;
index = *indexp;
flags = imports[index][0] >> 4;
class = imports[index][0] & 0xf;
symbol_name = (symbol_names +
(imports[index][1] << 16) +
(imports[index][2] << 8) +
(imports[index][3] << 0));
{
int i;
int n_libs;
n_libs = N_LIBS (closure_id);
for (i = 0; i < n_libs; ++i)
{
const lib_t *l;
uint32 first_symbol;
l = &closure_id->libs[i];
first_symbol = LIB_FIRST_SYMBOL (l);
if (index >= first_symbol &&
index < first_symbol + LIB_N_SYMBOLS (l))
{
Str255 sym255;
OSErr err;
sym255[0] = MIN (strlen (symbol_name), 255);
memcpy (sym255+1, symbol_name, sym255[0]);
err = FindSymbol (LIB_CID (l), sym255, valp, 0);
if (err != noErr)
{
if (flags & 8)
*valp = kUnresolvedCFragSymbolAddress;
else
*valp = (Ptr) tracking_value (symbol_name);
}
/*-->*/ break;
}
}
}
++*indexp;
retval = noErr;
return retval;
}
PRIVATE OSErr
relocate (const PEFLoaderRelocationHeader_t reloc_headers[],
int section, uint32 reloc_count, uint8 reloc_instrs[][2],
uint8 imports[][4], const char *symbol_names,
CFragClosureID closure_id, ConnectionID connp)
{
OSErr retval;
uint8 *relocAddress;
uint32 importIndex;
syn68k_addr_t sectionC;
syn68k_addr_t sectionD;
int32 repeat_remaining;
repeat_remaining = -1; /* i.e. not currently processing RelocSmRepeat or
RelocLgRepeat */
relocAddress = (uint8 *) SYN68K_TO_US (connp->sects[section].start);
importIndex = 0;
sectionC = connp->sects[0].start;
sectionD = connp->sects[1].start;
retval = noErr;
while (reloc_count-- > 0)
{
uint8 msb;
msb = reloc_instrs[0][0];
if ((msb >> 6) == 0)
{
uint8 lsb;
int skipCount;
int relocCount;
lsb = reloc_instrs[0][1];
skipCount = (msb << 2) | (lsb >> 6);
relocCount = (lsb & 0x3f);
relocAddress += skipCount * 4;
repeatedly_relocate (relocCount, &relocAddress, sectionD);
}
else
{
switch ((msb >> 5))
{
case 2:
{
int sub_op;
int run_length;
sub_op = (msb >> 1) & 0xf;
run_length = (((msb & 1) << 8)
| reloc_instrs[0][1]);
++run_length;
switch (sub_op)
{
case 0: /* RelocBySectC */
repeatedly_relocate (run_length, &relocAddress, sectionC);
break;
case 1: /* RelocBySectD */
repeatedly_relocate (run_length, &relocAddress, sectionD);
break;
case 2:
while (run_length-- > 0)
{
repeatedly_relocate (1, &relocAddress, sectionC);
repeatedly_relocate (1, &relocAddress, sectionD);
relocAddress += 4;
}
break;
case 3: /* RelocTVector8 */
while (run_length-- > 0)
{
repeatedly_relocate (1, &relocAddress, sectionC);
repeatedly_relocate (1, &relocAddress, sectionD);
}
break;
case 4:
while (run_length-- > 0)
{
repeatedly_relocate (1, &relocAddress, sectionD);
relocAddress += 4;
}
break;
case 5: /* RelocImportRun */
while (retval == noErr && run_length-- > 0)
{
Ptr symbol_val;
retval = symbol_lookup (&importIndex, &symbol_val,
imports, symbol_names,
closure_id);
if (retval == noErr)
repeatedly_relocate (1, &relocAddress,
(uint32) symbol_val);
}
break;
default:
warning_unexpected ("%d", sub_op);
assert (0);
retval = paramErr;
}
}
break;
case 3:
{
int sub_op;
int index;
Ptr symbol_val;
sub_op = (msb >> 1) & 0xf;
index = (((msb & 1) << 8)
| reloc_instrs[0][1]);
switch (sub_op)
{
case 0:
importIndex = index;
retval = symbol_lookup (&importIndex, &symbol_val,
imports, symbol_names,
closure_id);
if (retval == noErr)
repeatedly_relocate (1, &relocAddress,
(uint32) symbol_val);
break;
case 1:
sectionC = connp->sects[index].start;
warning_unimplemented ("RelocSmSetSectC not tested much");
assert (0);
break;
case 2:
sectionD = connp->sects[index].start;
warning_unimplemented ("RelocSmSetSectD not tested much");
break;
case 3:
fprintf (stderr, "RelocSmBySection\n");
assert (0);
break;
default:
warning_unexpected ("sub_op");
fprintf (stderr, "Relocte By Index sub_op = %d\n", sub_op);
assert (0);
break;
}
}
break;
default:
switch ((msb >> 4))
{
case 8:
{
uint32 offset;
offset = ((msb & 0xf) << 8) | (reloc_instrs[0][1]);
++offset;
relocAddress += offset;
}
break;
case 9:
{
warning_unimplemented ("RelocSmRepeat not tested much");
if (repeat_remaining != -1)
--repeat_remaining;
else
{
uint8 lsb;
lsb = reloc_instrs[0][1];
repeat_remaining = lsb + 1;
}
if (repeat_remaining > 0)
{
int blockCount;
blockCount = (msb & 0xF) + 2;
reloc_count += blockCount;
reloc_instrs -= blockCount;
}
else
repeat_remaining = -1;
}
break;
default:
switch (msb >> 2)
{
case 0x28:
{
uint32 offset;
offset = ((msb & 3) << 24 |
(reloc_instrs[0][1]) << 16 |
(reloc_instrs[0][2]) << 8 |
(reloc_instrs[0][3]));
relocAddress
= (uint8 *)
SYN68K_TO_US (connp->sects[section].start) + offset;
}
--reloc_count;
++reloc_instrs;
break;
case 0x29:
{
Ptr symbol_val;
importIndex = ((msb & 3) << 24 |
(reloc_instrs[0][1]) << 16 |
(reloc_instrs[0][2]) << 8 |
(reloc_instrs[0][3]));
retval = symbol_lookup (&importIndex, &symbol_val,
imports, symbol_names,
closure_id);
if (retval == noErr)
repeatedly_relocate (1, &relocAddress,
(uint32) symbol_val);
}
--reloc_count;
++reloc_instrs;
break;
case 0x2c:
fprintf (stderr, "RelocLgRepeat\n");
assert (0);
--reloc_count;
++reloc_instrs;
break;
case 0x2d:
fprintf (stderr, "RelocLgSetOrBySection\n");
assert (0);
--reloc_count;
++reloc_instrs;
break;
default:
warning_unexpected ("0x%x", msb);
retval = paramErr;
break;
}
}
}
}
++reloc_instrs;
}
return retval;
}
PRIVATE CFragClosureID
begin_closure (uint32 n_libs, PEFImportedLibrary_t *libs,
const char *symbol_names, OSType arch)
{
CFragClosureID retval;
int i;
OSErr err;
retval = (typeof (retval)) NewPtr (sizeof *retval + n_libs * sizeof (lib_t));
N_LIBS_X (retval) = CL (n_libs);
#warning eventually need to worry about errors
for (err = noErr, i = 0; /* err == noErr && */ i < n_libs; ++i)
{
Str63 libName;
Ptr mainAddr;
Str255 errName;
int offset;
const char *cname;
offset = PEFIL_NAME_OFFSET (&libs[i]);
cname = symbol_names + offset;
libName[0] = MIN(strlen (cname), 63);
memcpy (libName+1, cname, libName[0]);
err = GetSharedLibrary (libName, arch, kReferenceCFrag,
&LIB_CID_X (&retval->libs[i]),
&mainAddr, errName);
if (err != noErr)
{
warning_unexpected ("%.*s", libName[0], libName+1);
LIB_CID_X (&retval->libs[i]) = (void *) 0x12348765;
}
LIB_N_SYMBOLS_X (&retval->libs[i]) = PEFIL_SYMBOL_COUNT_X (&libs[i]);
LIB_FIRST_SYMBOL_X (&retval->libs[i]) = PEFIL_FIRST_SYMBOL_X (&libs[i]);
}
return retval;
}
PRIVATE OSErr
load_loader_section (const void *addr,
syn68k_addr_t default_address, uint32 total_size,
uint32 packed_size, uint32 unpacked_size,
uint32 section_offset, share_kind_t share_kind,
int alignment, syn68k_addr_t *mainAddrp, OSType arch,
ConnectionID connp)
{
OSErr retval;
char *loader_section_bytes;
PEFLoaderInfoHeader_t *lihp;
uint32 n_libs;
uint32 n_imports;
uint32 n_reloc_headers;
PEFImportedLibrary_t *libs;
uint8 (*imports)[4];
PEFLoaderRelocationHeader_t *reloc_headers;
uint8 *relocation_area;
char *symbol_names;
int i;
CFragClosureID closure_id;
loader_section_bytes = (char *)addr + section_offset;
lihp = (PEFLoaderInfoHeader_t *) loader_section_bytes;
connp->lihp = lihp;
n_libs = PEFLIH_IMPORTED_LIBRARY_COUNT (lihp);
libs = (PEFImportedLibrary_t *) &lihp[1];
n_imports = PEFLIH_IMPORTED_SYMBOL_COUNT (lihp);
imports = (uint8 (*)[4])&libs[n_libs];
n_reloc_headers = PEFLIH_RELOC_SECTION_COUNT (lihp);
reloc_headers = (PEFLoaderRelocationHeader_t *) imports[n_imports];
relocation_area = (char *)
(loader_section_bytes + PEFLIH_RELOC_INSTR_OFFSET (lihp));
symbol_names = (typeof (symbol_names))
(loader_section_bytes + PEFLIH_STRINGS_OFFSET (lihp));
closure_id = begin_closure (n_libs, libs, symbol_names, arch);
for (i = 0, retval = noErr; retval == noErr && i < n_reloc_headers; ++i)
{
uint32 reloc_count;
uint8 (*reloc_instrs)[2];
reloc_count = PEFRLH_RELOC_COUNT (&reloc_headers[i]);
reloc_instrs = (typeof (reloc_instrs))
(relocation_area
+ PEFRLH_FIRST_RELOC_OFFSET (&reloc_headers[i]));
retval = relocate (reloc_headers,
PEFRLH_SECTION_INDEX (&reloc_headers[i]),
reloc_count,
reloc_instrs, imports, symbol_names,
closure_id, connp);
}
if (retval == noErr && lihp->initSection != 0xffffffff)
{
uint32 *init_addr;
uint32 init_toc;
uint32 (*init_routine) (uint32);
InitBlock init_block;
warning_unimplemented ("register preservation, bad init_block");
// #warning this code has a lot of problems (register preservation, bad init_block)
init_addr = (uint32 *) SYN68K_TO_US
(connp->sects[PEFLIH_INIT_SECTION (lihp)].start +
PEFLIH_INIT_OFFSET (lihp));
memset (&init_block, 0xFA, sizeof init_block);
init_routine = (uint32 (*)(uint32)) SYN68K_TO_US (init_addr[0]);
init_toc = (uint32) SYN68K_TO_US (init_addr[1]);
#if defined (powerpc) || defined (__ppc__)
retval = ppc_call (init_toc, init_routine, (uint32) &init_block);
#else
warning_unexpected (NULL_STRING);
retval = paramErr;
#endif
if (retval)
{
warning_unexpected ("%d", retval);
retval = noErr;
}
}
if (retval == noErr)
*mainAddrp = (connp->sects[PEFLIH_MAIN_SECTION (lihp)].start +
PEFLIH_MAIN_OFFSET (lihp));
return retval;
}
PRIVATE OSErr
do_pef_section (ConnectionID connp, const void *addr,
const PEFSectionHeader_t *sections, int i,
boolean_t instantiate_p,
syn68k_addr_t *mainAddrp, OSType arch)
{
OSErr retval;
const PEFSectionHeader_t *shp;
syn68k_addr_t default_address;
uint32 total_size;
uint32 packed_size;
uint32 unpacked_size;
uint32 section_offset;
int share_kind;
int alignment;
shp = §ions[i];
#if 1
{
uint32 def;
def = PEFSH_DEFAULT_ADDRESS (shp);
if (def)
fprintf (stderr, "***def = 0x%x***\n", def);
}
#endif
#if 0
default_address = PEFSH_DEFAULT_ADDRESS (shp);
#else
default_address = 0;
// #warning defaultAddress ignored -- dont implement without testing
#endif
total_size = PEFSH_TOTAL_SIZE (shp);
packed_size = PEFSH_PACKED_SIZE (shp);
unpacked_size = PEFSH_UNPACKED_SIZE (shp);
section_offset = PEFSH_CONTAINER_OFFSET (shp);
share_kind = PEFSH_SHARE_KIND (shp);
alignment = PEFSH_ALIGNMENT (shp);
switch (PEFSH_SECTION_KIND (shp))
{
case code_section_type:
connp->sects[i].perms = readable_section | executable_section;
goto unpacked_common;
case unpacked_data_section_type:
connp->sects[i].perms = readable_section | writable_section;
goto unpacked_common;
case constant_section_type:
connp->sects[i].perms = readable_section;
goto unpacked_common;
case executable_data_section_type:
connp->sects[i].perms = (readable_section | writable_section |
executable_section);
goto unpacked_common;
unpacked_common:
retval = load_unpacked_section (addr, default_address, total_size,
packed_size, unpacked_size,
section_offset, share_kind, alignment,
&connp->sects[i]);
break;
case pattern_data_section_type:
connp->sects[i].perms = readable_section | writable_section;
retval = load_pattern_section (addr, default_address, total_size,
packed_size, unpacked_size,
section_offset, share_kind, alignment,
&connp->sects[i]);
break;
case loader_section_type:
retval = load_loader_section (addr, default_address, total_size,
packed_size, unpacked_size,
section_offset, share_kind, alignment,
mainAddrp, arch, connp);
break;
default:
warning_unexpected ("%d", PEFSH_SECTION_KIND (shp));
retval = noErr;
break;
}
return retval;
}
/*
* NOTE: it would be nice if someone else provided code to flush the
* instruction cache. I'm a little nervous that my code below will fail on
* multi-processor systems.
*/
typedef enum { ICACHE } flush_type_t;
PRIVATE void
cacheflush (void *start, uint32 length, flush_type_t flush)
{
#if defined (powerpc) || defined (__ppc__)
enum { CACHE_LINE_SIZE = 32, };
char *p, *ep;
switch (flush)
{
case ICACHE:
for (p = start, ep = p + roundup (length, CACHE_LINE_SIZE);
p != ep;
p += CACHE_LINE_SIZE)
{
asm volatile ("dcbf 0,%0" : : "r" (p) : "memory");
asm volatile ("sync" : : : "memory");
asm volatile ("icbi 0,%0" : : "r"(p) : "memory");
}
asm volatile ("isync" : : : "memory");
break;
default:
warning_unexpected ("%d", flush);
break;
}
#endif
}
PRIVATE OSErr
do_pef_sections (ConnectionID connp, const PEFContainerHeader_t *headp,
syn68k_addr_t *mainAddrp, OSType arch)
{
OSErr retval;
PEFSectionHeader_t *sections;
int n_sects;
int i;
n_sects = connp->n_sects;
sections = (typeof (sections)) ((char *) headp + sizeof *headp);
memset (connp->sects, 0, sizeof connp->sects[0] * n_sects);
for (i = 0, retval = noErr; retval == noErr && i < n_sects; ++i)
retval = do_pef_section (connp, headp, sections, i,
i < PEF_CONTAINER_INSTSECTION_COUNT(headp),
mainAddrp, arch);
// #warning need to back out cleanly if a section fails to load
#if defined (linux)
if (retval == noErr)
{
int i;
for (i = 0; i < n_sects; ++i)
{
if (connp->sects[i].length)
{
int prot;
prot = 0;
if (connp->sects[i].perms & executable_section)
{
cacheflush (SYN68K_TO_US (connp->sects[i].start),
connp->sects[i].length, ICACHE);
prot |= PROT_EXEC;
}
if (connp->sects[i].perms & readable_section)
prot |= PROT_READ;
if (connp->sects[i].perms & writable_section)
prot |= PROT_WRITE;
if (!prot)
prot |= PROT_NONE;
if (mprotect (SYN68K_TO_US (connp->sects[i].start),
roundup (connp->sects[i].length, getpagesize ()),
prot) != 0)
warning_unexpected ("%d", errno);
}
}
}
#endif
return retval;
}
PUBLIC ConnectionID
ROMlib_new_connection (uint32 n_sects)
{
ConnectionID retval;
Size n_bytes;
n_bytes = sizeof *retval + n_sects * sizeof (section_info_t);
retval = (ConnectionID) NewPtrSysClear (n_bytes);
if (retval)
retval->n_sects = n_sects;
return retval;
}
P7 (PUBLIC pascal trap, OSErr, GetMemFragment, void *, addr,
uint32, length, Str63, fragname, LoadFlags, flags,
ConnectionID *, connp, Ptr *, mainAddrp, Str255, errname)
{
OSErr retval;
syn68k_addr_t main_addr;
PEFContainerHeader_t *headp;
warning_unimplemented ("ignoring flags = 0x%x\n", flags);
main_addr = 0;
*connp = 0;
headp = addr;
if (PEF_CONTAINER_TAG1_X(headp) != CLC (T('J','o','y','!')))
warning_unexpected ("0x%x", PEF_CONTAINER_TAG1 (headp));
if (PEF_CONTAINER_TAG2_X(headp) != CLC (T('p','e','f','f')))
warning_unexpected ("0x%x", PEF_CONTAINER_TAG2 (headp));
if (PEF_CONTAINER_ARCHITECTURE_X(headp)
!= CLC (T('p','w','p','c')))
warning_unexpected ("0x%x",
PEF_CONTAINER_ARCHITECTURE (headp));
if (PEF_CONTAINER_FORMAT_VERSION_X(headp) != CLC (1))
warning_unexpected ("0x%x",
PEF_CONTAINER_FORMAT_VERSION (headp));
// #warning ignoring (old_dev, old_imp, current) version
*connp = ROMlib_new_connection (PEF_CONTAINER_SECTION_COUNT (headp));
if (!*connp)
retval = fragNoMem;
else
retval = do_pef_sections (*connp, headp, &main_addr,
PEF_CONTAINER_ARCHITECTURE (headp));
if (retval == noErr)
*mainAddrp = (Ptr) SYN68K_TO_US (main_addr);
return retval;
}
typedef struct
{
void *addr; /* virtual address */
FSSpec fs; /* canonicalized file tht this came from */
LONGINT offset_req;
LONGINT length_req;
off_t offset_act;
size_t length_act;
boolean_t mapped_to_eof_p;
int refcount;
}
context_t;
PRIVATE int n_context_slots = 0;
PRIVATE int n_active_contexts = 0;
PRIVATE context_t *contexts = 0;
PRIVATE boolean_t
fsmatch (FSSpecPtr fsp1, FSSpecPtr fsp2)
{
boolean_t retval;
retval = (fsp1->vRefNum == fsp2->vRefNum &&
fsp1->parID == fsp2->parID &&
EqualString (fsp1->name, fsp2->name, FALSE, TRUE));
return retval;
}
PRIVATE boolean_t
match (FSSpecPtr fsp, LONGINT offset, LONGINT length, const context_t *cp)
{
boolean_t retval;
retval = (cp->refcount > 0 &&
fsmatch (fsp, (FSSpecPtr) &cp->fs) &&
offset >= cp->offset_act &&
((length == kWholeFork && cp->mapped_to_eof_p) ||
(offset + length <= cp->offset_act + cp->length_act)));
return retval;
}
PRIVATE OSErr
get_context (int *contextidp, boolean_t *exists_pp, FSSpecPtr fsp,
LONGINT offset, LONGINT length)
{
PRIVATE OSErr retval;
int i;
int free_slot;
retval = noErr;
free_slot = -1;
for (i = 0;
i < n_context_slots && !match (fsp, offset, length, &contexts[i]);
++i)
if (contexts[i].refcount == 0)
free_slot = i;
if (i < n_context_slots)
{
++contexts[i].refcount;
*contextidp = i;
*exists_pp = TRUE;
}
else
{
if (free_slot == -1)
{
size_t new_size;
context_t *new_contexts;
if (n_context_slots != n_active_contexts)
warning_unexpected ("%d %d", n_context_slots, n_active_contexts);
new_size = sizeof *new_contexts * (n_active_contexts + 1);
new_contexts = realloc (contexts, new_size);
if (new_contexts == NULL)
retval = memFullErr;
else
{
free_slot = n_active_contexts;
++n_active_contexts;
++n_context_slots;
contexts = new_contexts;
}
}
if (retval == noErr)
{
context_t *cp;
cp = &contexts[free_slot];
cp->fs = *fsp;
cp->offset_req = offset;
cp->length_req = length;
cp->refcount = 1;
*contextidp = free_slot;
*exists_pp = FALSE;
}
}
return retval;
}
PRIVATE OSErr
release_context (int context)
{
OSErr retval;
if (context >= n_context_slots || contexts[context].refcount <= 0)
retval = paramErr;
else
{
retval = noErr;
if (--contexts[context].refcount == 0)
{
--n_active_contexts;
munmap (contexts[context].addr, contexts[context].length_act);
}
}
return retval;
}
PRIVATE context_t *
contextp_from_id (int context)
{
context_t *retval;
if (context >= n_context_slots || contexts[context].refcount <= 0)
retval = 0;
else
retval = &contexts[context];
return retval;
}
PRIVATE OSErr
try_to_mmap_file (FSSpecPtr fsp, LONGINT offset, LONGINT length,
int *contextidp)
{
OSErr retval;
INTEGER vref;
retval = noErr;
/* canonicalize fsp to make sure */
vref = CW (fsp->vRefNum);
if (ISWDNUM (vref) || pstr_index_after (fsp->name, ':', 0))
{
FSSpecPtr newfsp;
newfsp = alloca (sizeof *newfsp);
retval = FSMakeFSSpec (vref, CL (fsp->parID), fsp->name, newfsp);
if (retval == noErr)
fsp = newfsp;
}
if (retval == noErr)
{
boolean_t exists_p;
int context;
retval = get_context (&context, &exists_p, fsp, offset, length);
if (retval == noErr)
{
if (exists_p)
*contextidp = context;
else
{
INTEGER rn;
retval = FSpOpenDF (fsp, fsRdPerm, &rn);
if (retval != noErr)
release_context (context);
else
{
const fcbrec *fp;
context_t *cp;
size_t pagesize;
pagesize = getpagesize ();
fp = (const fcbrec *) (MR(FCBSPtr) + rn);
/* NOTE: right now we let them place it anywhere they
want -- eventually it probably makes sense to try to
get it placed high */
cp = contextp_from_id (context);
cp->offset_act = rounddown (offset, pagesize);
cp->mapped_to_eof_p = length == kWholeFork;
if (!cp->mapped_to_eof_p)
cp->length_act = length + (offset - cp->offset_act);
else
{
LONGINT eof;
OSErr err;
err = GetEOF (rn, &eof);
if (err == noErr)
cp->length_act = eof - cp->offset_act;
else
{
warning_unexpected ("err = %d", err);
cp->length_act = -1; /* force mmap to fail */
}
}
cp->addr = mmap (0, cp->length_act, PROT_READ, MAP_PRIVATE,
fp->fcfd, cp->offset_act);
if (cp->addr != MAP_FAILED)
*contextidp = context;
else
{
retval = ROMlib_maperrno ();
release_context (context);
}
FSClose (rn);
}
}
}
}
return retval;
}
P8 (PUBLIC pascal trap, OSErr, GetDiskFragment, FSSpecPtr, fsp,
LONGINT, offset, LONGINT, length, Str63, fragname, LoadFlags, flags,
ConnectionID *, connp, Ptr *, mainAddrp, Str255, errname)
{
OSErr retval;
int context;
warning_unimplemented ("ignoring flags = 0x%x\n", flags);
retval = try_to_mmap_file (fsp, offset, length, &context);
if (retval == noErr)
{
const context_t *cp;
void *addr;
cp = contextp_from_id (context);
addr = (char *) cp->addr + offset - cp->offset_act;
retval = GetMemFragment (addr, length, fragname, flags, connp,
mainAddrp, errname);
}
return retval;
}
#endif
|
the_stack_data/118087.c | void axpy(int n, double *y, double a, double *x) {
register int i;
/*@ begin Loop(transform Unroll(ufactor=5, parallelize=True)
for (i=0; i<=n-1; i++)
y[i]+=a*x[i];
) @*/
for (i=0; i<=n-1; i++)
y[i]+=a*x[i];
/*@ end @*/
}
|
the_stack_data/37270.c | static int f(int n)
{
__label__ n;
n: return n;
}
/*
* check-name: __label__ scope
*/
|
the_stack_data/10536.c | // RUN: %clang_cc1 -triple aarch64-windows-gnu -Oz -emit-llvm %s -o - | FileCheck %s
void *test_sponentry() {
return __builtin_sponentry();
}
// CHECK-LABEL: define dso_local i8* @test_sponentry()
// CHECK: = tail call i8* @llvm.sponentry()
// CHECK: ret i8*
|
the_stack_data/211079822.c | /**
* This file was autogenerated from coopbtn.png by WiiBuilder.
*/
const unsigned char coopbtn[] = {
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52,
0x00, 0x00, 0x00, 0x9C, 0x00, 0x00, 0x00, 0x34, 0x08, 0x06, 0x00, 0x00, 0x00, 0x7F, 0xF7, 0x51,
0xCE, 0x00, 0x00, 0x00, 0x04, 0x67, 0x41, 0x4D, 0x41, 0x00, 0x00, 0xB1, 0x8F, 0x0B, 0xFC, 0x61,
0x05, 0x00, 0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x0E, 0xC2, 0x00, 0x00, 0x0E,
0xC2, 0x01, 0x15, 0x28, 0x4A, 0x80, 0x00, 0x00, 0x00, 0x19, 0x74, 0x45, 0x58, 0x74, 0x53, 0x6F,
0x66, 0x74, 0x77, 0x61, 0x72, 0x65, 0x00, 0x70, 0x61, 0x69, 0x6E, 0x74, 0x2E, 0x6E, 0x65, 0x74,
0x20, 0x34, 0x2E, 0x30, 0x2E, 0x32, 0x31, 0xF1, 0x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CA, 0x0A, 0x38, 0xEB, 0xF8, 0xFC, 0x06, 0xC6, 0x37, 0xE1, 0x8E, 0x60, 0xDA, 0xCB, 0x9A, 0x30,
0x43, 0x51, 0xBA, 0x74, 0x29, 0x19, 0xE1, 0xC7, 0x10, 0x45, 0xCF, 0x9E, 0xDD, 0x69, 0xD9, 0x92,
0xB9, 0xB4, 0x7F, 0xAF, 0x0F, 0x5D, 0x0F, 0xB9, 0xC2, 0x62, 0xB5, 0x2F, 0xEB, 0x17, 0x4B, 0xF4,
0xD2, 0xFF, 0x02, 0x87, 0x59, 0x55, 0x1B, 0x7E, 0xA9, 0xC0, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x49,
0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82
};
const int coopbtn_size = 9079;
|
the_stack_data/738337.c | void main(){
//int *c = *q;
}
//opt-6.0 -S -load ../lib/libAndersen.so -anders-aa -dump-result ./test7.ll -o andertest.ll
|
the_stack_data/1208637.c | void ft_putcount_to_ptr(int *ptr, int cur_count)
{
*ptr = cur_count;
}
void ft_putcount_to_hptr(short int *ptr, int cur_count)
{
*ptr = (short int)cur_count;
}
void ft_putcount_to_hhptr(signed char *ptr, int cur_count)
{
*ptr = (signed char)cur_count;
}
void ft_putcount_to_lptr(long int *ptr, int cur_count)
{
*ptr = (long int)cur_count;
}
void ft_putcount_to_llptr(long long *ptr, int cur_count)
{
*ptr = (long long)cur_count;
}
|
the_stack_data/7935.c | #include <stdio.h>
#include <stdlib.h>
double findMedianSortedArrays(int *, int, int *, int);
int main(){
int nums1[]={0,0,0,0,0};
int nums2[]={-1,0,0,0,0,0,1};
float result;
result=findMedianSortedArrays(nums1,sizeof(nums1)/sizeof(int),nums2,sizeof(nums2)/sizeof(int));
printf("%.5f\n",result);
return 0;
}
//time complexity:O(m+n), space complexity:O(m+n)
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size){
if(nums1Size==0 && nums2Size==1) return nums2[0];
if(nums1Size==1 && nums1Size==0) return nums1[0];
else{
int *temp=malloc((nums1Size+nums2Size)*sizeof(int));
int nums1ptr=0,nums2ptr=0;
//merge two sorted -array
while(nums1ptr<nums1Size && nums2ptr<nums2Size){
if(nums1[nums1ptr]<nums2[nums2ptr]){
temp[nums1ptr+nums2ptr]=nums1[nums1ptr];
nums1ptr++;
}
else{
temp[nums1ptr+nums2ptr]=nums2[nums2ptr];
nums2ptr++;
}
}
//copy the rest of the array
if(nums1ptr==nums1Size && nums2ptr<nums2Size){
while(nums2ptr<nums2Size){
temp[nums1ptr+nums2ptr]=nums2[nums2ptr];
nums2ptr++;
}
}
else if(nums1ptr<nums1Size && nums2ptr==nums2Size){
while(nums1ptr<nums1Size){
temp[nums1ptr+nums2ptr]=nums1[nums1ptr];
nums1ptr++;
}
}
//deal with the median of the merged array
if((nums1Size+nums2Size)%2==0){
float result=temp[(nums1Size+nums2Size)/2] + temp[(nums1Size+nums2Size)/2-1];
free(temp);
return result/2;
}
else{
float result=temp[(nums1Size+nums2Size-1)/2];
free(temp);
return result;
}
}
} |
the_stack_data/471110.c | // BUG: sleeping function called from invalid context in tpk_write
// https://syzkaller.appspot.com/bug?id=64dd1729dea8dab1b77bcf56198bbdac40cabf40
// status:open
// autogenerated by syzkaller (https://github.com/google/syzkaller)
#define _GNU_SOURCE
#include <arpa/inet.h>
#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <sched.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/prctl.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <linux/capability.h>
#include <linux/genetlink.h>
#include <linux/if_addr.h>
#include <linux/if_link.h>
#include <linux/in6.h>
#include <linux/neighbour.h>
#include <linux/net.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/veth.h>
static bool write_file(const char* file, const char* what, ...)
{
char buf[1024];
va_list args;
va_start(args, what);
vsnprintf(buf, sizeof(buf), what, args);
va_end(args);
buf[sizeof(buf) - 1] = 0;
int len = strlen(buf);
int fd = open(file, O_WRONLY | O_CLOEXEC);
if (fd == -1)
return false;
if (write(fd, buf, len) != len) {
int err = errno;
close(fd);
errno = err;
return false;
}
close(fd);
return true;
}
struct nlmsg {
char* pos;
int nesting;
struct nlattr* nested[8];
char buf[1024];
};
static struct nlmsg nlmsg;
static void netlink_init(struct nlmsg* nlmsg, int typ, int flags,
const void* data, int size)
{
memset(nlmsg, 0, sizeof(*nlmsg));
struct nlmsghdr* hdr = (struct nlmsghdr*)nlmsg->buf;
hdr->nlmsg_type = typ;
hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
memcpy(hdr + 1, data, size);
nlmsg->pos = (char*)(hdr + 1) + NLMSG_ALIGN(size);
}
static void netlink_attr(struct nlmsg* nlmsg, int typ, const void* data,
int size)
{
struct nlattr* attr = (struct nlattr*)nlmsg->pos;
attr->nla_len = sizeof(*attr) + size;
attr->nla_type = typ;
memcpy(attr + 1, data, size);
nlmsg->pos += NLMSG_ALIGN(attr->nla_len);
}
static int netlink_send_ext(struct nlmsg* nlmsg, int sock, uint16_t reply_type,
int* reply_len)
{
if (nlmsg->pos > nlmsg->buf + sizeof(nlmsg->buf) || nlmsg->nesting)
exit(1);
struct nlmsghdr* hdr = (struct nlmsghdr*)nlmsg->buf;
hdr->nlmsg_len = nlmsg->pos - nlmsg->buf;
struct sockaddr_nl addr;
memset(&addr, 0, sizeof(addr));
addr.nl_family = AF_NETLINK;
unsigned n = sendto(sock, nlmsg->buf, hdr->nlmsg_len, 0,
(struct sockaddr*)&addr, sizeof(addr));
if (n != hdr->nlmsg_len)
exit(1);
n = recv(sock, nlmsg->buf, sizeof(nlmsg->buf), 0);
if (hdr->nlmsg_type == NLMSG_DONE) {
*reply_len = 0;
return 0;
}
if (n < sizeof(struct nlmsghdr))
exit(1);
if (reply_len && hdr->nlmsg_type == reply_type) {
*reply_len = n;
return 0;
}
if (n < sizeof(struct nlmsghdr) + sizeof(struct nlmsgerr))
exit(1);
if (hdr->nlmsg_type != NLMSG_ERROR)
exit(1);
return -((struct nlmsgerr*)(hdr + 1))->error;
}
static int netlink_send(struct nlmsg* nlmsg, int sock)
{
return netlink_send_ext(nlmsg, sock, 0, NULL);
}
static int netlink_next_msg(struct nlmsg* nlmsg, unsigned int offset,
unsigned int total_len)
{
struct nlmsghdr* hdr = (struct nlmsghdr*)(nlmsg->buf + offset);
if (offset == total_len || offset + hdr->nlmsg_len > total_len)
return -1;
return hdr->nlmsg_len;
}
static void netlink_device_change(struct nlmsg* nlmsg, int sock,
const char* name, bool up, const char* master,
const void* mac, int macsize,
const char* new_name)
{
struct ifinfomsg hdr;
memset(&hdr, 0, sizeof(hdr));
if (up)
hdr.ifi_flags = hdr.ifi_change = IFF_UP;
hdr.ifi_index = if_nametoindex(name);
netlink_init(nlmsg, RTM_NEWLINK, 0, &hdr, sizeof(hdr));
if (new_name)
netlink_attr(nlmsg, IFLA_IFNAME, new_name, strlen(new_name));
if (master) {
int ifindex = if_nametoindex(master);
netlink_attr(nlmsg, IFLA_MASTER, &ifindex, sizeof(ifindex));
}
if (macsize)
netlink_attr(nlmsg, IFLA_ADDRESS, mac, macsize);
int err = netlink_send(nlmsg, sock);
(void)err;
}
const int kInitNetNsFd = 239;
#define DEVLINK_FAMILY_NAME "devlink"
#define DEVLINK_CMD_PORT_GET 5
#define DEVLINK_CMD_RELOAD 37
#define DEVLINK_ATTR_BUS_NAME 1
#define DEVLINK_ATTR_DEV_NAME 2
#define DEVLINK_ATTR_NETDEV_NAME 7
#define DEVLINK_ATTR_NETNS_FD 138
static int netlink_devlink_id_get(struct nlmsg* nlmsg, int sock)
{
struct genlmsghdr genlhdr;
struct nlattr* attr;
int err, n;
uint16_t id = 0;
memset(&genlhdr, 0, sizeof(genlhdr));
genlhdr.cmd = CTRL_CMD_GETFAMILY;
netlink_init(nlmsg, GENL_ID_CTRL, 0, &genlhdr, sizeof(genlhdr));
netlink_attr(nlmsg, CTRL_ATTR_FAMILY_NAME, DEVLINK_FAMILY_NAME,
strlen(DEVLINK_FAMILY_NAME) + 1);
err = netlink_send_ext(nlmsg, sock, GENL_ID_CTRL, &n);
if (err) {
return -1;
}
attr = (struct nlattr*)(nlmsg->buf + NLMSG_HDRLEN +
NLMSG_ALIGN(sizeof(genlhdr)));
for (; (char*)attr < nlmsg->buf + n;
attr = (struct nlattr*)((char*)attr + NLMSG_ALIGN(attr->nla_len))) {
if (attr->nla_type == CTRL_ATTR_FAMILY_ID) {
id = *(uint16_t*)(attr + 1);
break;
}
}
if (!id) {
return -1;
}
recv(sock, nlmsg->buf, sizeof(nlmsg->buf), 0); /* recv ack */
return id;
}
static void netlink_devlink_netns_move(const char* bus_name,
const char* dev_name, int netns_fd)
{
struct genlmsghdr genlhdr;
int sock;
int id, err;
sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
if (sock == -1)
exit(1);
id = netlink_devlink_id_get(&nlmsg, sock);
if (id == -1)
goto error;
memset(&genlhdr, 0, sizeof(genlhdr));
genlhdr.cmd = DEVLINK_CMD_RELOAD;
netlink_init(&nlmsg, id, 0, &genlhdr, sizeof(genlhdr));
netlink_attr(&nlmsg, DEVLINK_ATTR_BUS_NAME, bus_name, strlen(bus_name) + 1);
netlink_attr(&nlmsg, DEVLINK_ATTR_DEV_NAME, dev_name, strlen(dev_name) + 1);
netlink_attr(&nlmsg, DEVLINK_ATTR_NETNS_FD, &netns_fd, sizeof(netns_fd));
err = netlink_send(&nlmsg, sock);
if (err) {
}
error:
close(sock);
}
static struct nlmsg nlmsg2;
static void initialize_devlink_ports(const char* bus_name, const char* dev_name,
const char* netdev_prefix)
{
struct genlmsghdr genlhdr;
int len, total_len, id, err, offset;
uint16_t netdev_index;
int sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
if (sock == -1)
exit(1);
int rtsock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if (rtsock == -1)
exit(1);
id = netlink_devlink_id_get(&nlmsg, sock);
if (id == -1)
goto error;
memset(&genlhdr, 0, sizeof(genlhdr));
genlhdr.cmd = DEVLINK_CMD_PORT_GET;
netlink_init(&nlmsg, id, NLM_F_DUMP, &genlhdr, sizeof(genlhdr));
netlink_attr(&nlmsg, DEVLINK_ATTR_BUS_NAME, bus_name, strlen(bus_name) + 1);
netlink_attr(&nlmsg, DEVLINK_ATTR_DEV_NAME, dev_name, strlen(dev_name) + 1);
err = netlink_send_ext(&nlmsg, sock, id, &total_len);
if (err) {
goto error;
}
offset = 0;
netdev_index = 0;
while ((len = netlink_next_msg(&nlmsg, offset, total_len)) != -1) {
struct nlattr* attr = (struct nlattr*)(nlmsg.buf + offset + NLMSG_HDRLEN +
NLMSG_ALIGN(sizeof(genlhdr)));
for (; (char*)attr < nlmsg.buf + offset + len;
attr = (struct nlattr*)((char*)attr + NLMSG_ALIGN(attr->nla_len))) {
if (attr->nla_type == DEVLINK_ATTR_NETDEV_NAME) {
char* port_name;
char netdev_name[IFNAMSIZ];
port_name = (char*)(attr + 1);
snprintf(netdev_name, sizeof(netdev_name), "%s%d", netdev_prefix,
netdev_index);
netlink_device_change(&nlmsg2, rtsock, port_name, true, 0, 0, 0,
netdev_name);
break;
}
}
offset += len;
netdev_index++;
}
error:
close(rtsock);
close(sock);
}
static void initialize_devlink_pci(void)
{
int netns = open("/proc/self/ns/net", O_RDONLY);
if (netns == -1)
exit(1);
int ret = setns(kInitNetNsFd, 0);
if (ret == -1)
exit(1);
netlink_devlink_netns_move("pci", "0000:00:10.0", netns);
ret = setns(netns, 0);
if (ret == -1)
exit(1);
close(netns);
initialize_devlink_ports("pci", "0000:00:10.0", "netpci");
}
#define MAX_FDS 30
static void setup_common()
{
if (mount(0, "/sys/fs/fuse/connections", "fusectl", 0, 0)) {
}
}
static void loop();
static void sandbox_common()
{
prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
setpgrp();
setsid();
int netns = open("/proc/self/ns/net", O_RDONLY);
if (netns == -1)
exit(1);
if (dup2(netns, kInitNetNsFd) < 0)
exit(1);
close(netns);
struct rlimit rlim;
rlim.rlim_cur = rlim.rlim_max = (200 << 20);
setrlimit(RLIMIT_AS, &rlim);
rlim.rlim_cur = rlim.rlim_max = 32 << 20;
setrlimit(RLIMIT_MEMLOCK, &rlim);
rlim.rlim_cur = rlim.rlim_max = 136 << 20;
setrlimit(RLIMIT_FSIZE, &rlim);
rlim.rlim_cur = rlim.rlim_max = 1 << 20;
setrlimit(RLIMIT_STACK, &rlim);
rlim.rlim_cur = rlim.rlim_max = 0;
setrlimit(RLIMIT_CORE, &rlim);
rlim.rlim_cur = rlim.rlim_max = 256;
setrlimit(RLIMIT_NOFILE, &rlim);
if (unshare(CLONE_NEWNS)) {
}
if (unshare(CLONE_NEWIPC)) {
}
if (unshare(0x02000000)) {
}
if (unshare(CLONE_NEWUTS)) {
}
if (unshare(CLONE_SYSVSEM)) {
}
typedef struct {
const char* name;
const char* value;
} sysctl_t;
static const sysctl_t sysctls[] = {
{"/proc/sys/kernel/shmmax", "16777216"},
{"/proc/sys/kernel/shmall", "536870912"},
{"/proc/sys/kernel/shmmni", "1024"},
{"/proc/sys/kernel/msgmax", "8192"},
{"/proc/sys/kernel/msgmni", "1024"},
{"/proc/sys/kernel/msgmnb", "1024"},
{"/proc/sys/kernel/sem", "1024 1048576 500 1024"},
};
unsigned i;
for (i = 0; i < sizeof(sysctls) / sizeof(sysctls[0]); i++)
write_file(sysctls[i].name, sysctls[i].value);
}
int wait_for_loop(int pid)
{
if (pid < 0)
exit(1);
int status = 0;
while (waitpid(-1, &status, __WALL) != pid) {
}
return WEXITSTATUS(status);
}
static void drop_caps(void)
{
struct __user_cap_header_struct cap_hdr = {};
struct __user_cap_data_struct cap_data[2] = {};
cap_hdr.version = _LINUX_CAPABILITY_VERSION_3;
cap_hdr.pid = getpid();
if (syscall(SYS_capget, &cap_hdr, &cap_data))
exit(1);
const int drop = (1 << CAP_SYS_PTRACE) | (1 << CAP_SYS_NICE);
cap_data[0].effective &= ~drop;
cap_data[0].permitted &= ~drop;
cap_data[0].inheritable &= ~drop;
if (syscall(SYS_capset, &cap_hdr, &cap_data))
exit(1);
}
static int do_sandbox_none(void)
{
if (unshare(CLONE_NEWPID)) {
}
int pid = fork();
if (pid != 0)
return wait_for_loop(pid);
setup_common();
sandbox_common();
drop_caps();
if (unshare(CLONE_NEWNET)) {
}
initialize_devlink_pci();
loop();
exit(1);
}
static void close_fds()
{
int fd;
for (fd = 3; fd < MAX_FDS; fd++)
close(fd);
}
uint64_t r[2] = {0xffffffffffffffff, 0xffffffffffffffff};
void loop(void)
{
intptr_t res = 0;
memcpy((void*)0x200000c0, "/dev/ptmx\000", 10);
res = syscall(__NR_openat, 0xffffffffffffff9cul, 0x200000c0ul, 0ul, 0ul);
if (res != -1)
r[0] = res;
*(uint32_t*)0x20000040 = 0xf;
syscall(__NR_ioctl, r[0], 0x5423ul, 0x20000040ul);
syscall(__NR_ioctl, r[0], 0x400455c8ul, 4ul);
memcpy((void*)0x20001540, "/dev/ttyprintk\000", 15);
res = syscall(__NR_openat, 0xffffffffffffff9cul, 0x20001540ul, 0ul, 0ul);
if (res != -1)
r[1] = res;
*(uint32_t*)0x20001580 = 7;
syscall(__NR_ioctl, r[1], 0x5423ul, 0x20001580ul);
close_fds();
}
int main(void)
{
syscall(__NR_mmap, 0x20000000ul, 0x1000000ul, 3ul, 0x32ul, -1, 0);
do_sandbox_none();
return 0;
}
|
the_stack_data/12865.c | //
// main.c
// ordinamento_selezione_minimo
//
// Created by Vincenzo on 17/11/2020.
// Copyright © 2020 Vincenzo. All rights reserved.
//
#include<stdio.h>
void ord_sel_min(char array[], int n_a);
void min_val_ind(char a[], int n, char *min_array, int *i_min);
void scambiare_c(char *c1, char *c2);
void visualizza_array(char a[], int n);
int main()
{
int n_a,n;
char a[]={'p','z','a','r','b','c','m','s','d','n','o','e','g','f','u','w','t','h'};
n_a=18;
printf("\nArray non ordinato:\n");
visualizza_array(a,n);
ord_sel_min(a,n_a);
printf("\nArray ordinato:\n");
visualizza_array(a,n);
}
void scambiare_c(char *c1, char *c2)
{
char temp;
temp= *c1;
*c1=*c2;
*c2=temp;
}
void visualizza_array(char a[], int n)
{
int i;
for(i=0;i<18;i++)
{
printf("%3c", a[i]);
}
}
void ord_sel_min(char array[], int n_a)
{
int i, indice_min;
char min_array;
for(i=0;i<n_a-1;i++)
{
min_val_ind(&array[i],n_a-i, &min_array, &indice_min);
scambiare_c(&array[i], &array[indice_min+i]);
}
}
void min_val_ind(char a[], int n, char *min_array, int *i_min)
{
int i;
*min_array=a[0];
*i_min=0;
for(i=1;i<n;i++)
{
if(a[i]<*min_array)
{
*min_array=a[i];
*i_min=i;
}
}
}
|
the_stack_data/154358.c | // https://syzkaller.appspot.com/bug?id=01571dec4578f8dfdbc9da609a84b6b34e7d4d62
// autogenerated by syzkaller (https://github.com/google/syzkaller)
#define _GNU_SOURCE
#include <arpa/inet.h>
#include <dirent.h>
#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <setjmp.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/prctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include <linux/genetlink.h>
#include <linux/if_addr.h>
#include <linux/if_link.h>
#include <linux/in6.h>
#include <linux/neighbour.h>
#include <linux/net.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/usb/ch9.h>
#include <linux/veth.h>
static unsigned long long procid;
static __thread int skip_segv;
static __thread jmp_buf segv_env;
static void segv_handler(int sig, siginfo_t* info, void* ctx)
{
uintptr_t addr = (uintptr_t)info->si_addr;
const uintptr_t prog_start = 1 << 20;
const uintptr_t prog_end = 100 << 20;
int skip = __atomic_load_n(&skip_segv, __ATOMIC_RELAXED) != 0;
int valid = addr < prog_start || addr > prog_end;
if (skip && valid) {
_longjmp(segv_env, 1);
}
exit(sig);
}
static void install_segv_handler(void)
{
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = SIG_IGN;
syscall(SYS_rt_sigaction, 0x20, &sa, NULL, 8);
syscall(SYS_rt_sigaction, 0x21, &sa, NULL, 8);
memset(&sa, 0, sizeof(sa));
sa.sa_sigaction = segv_handler;
sa.sa_flags = SA_NODEFER | SA_SIGINFO;
sigaction(SIGSEGV, &sa, NULL);
sigaction(SIGBUS, &sa, NULL);
}
#define NONFAILING(...) \
({ \
int ok = 1; \
__atomic_fetch_add(&skip_segv, 1, __ATOMIC_SEQ_CST); \
if (_setjmp(segv_env) == 0) { \
__VA_ARGS__; \
} else \
ok = 0; \
__atomic_fetch_sub(&skip_segv, 1, __ATOMIC_SEQ_CST); \
ok; \
})
static void sleep_ms(uint64_t ms)
{
usleep(ms * 1000);
}
static uint64_t current_time_ms(void)
{
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts))
exit(1);
return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
}
static void use_temporary_dir(void)
{
char tmpdir_template[] = "./syzkaller.XXXXXX";
char* tmpdir = mkdtemp(tmpdir_template);
if (!tmpdir)
exit(1);
if (chmod(tmpdir, 0777))
exit(1);
if (chdir(tmpdir))
exit(1);
}
static bool write_file(const char* file, const char* what, ...)
{
char buf[1024];
va_list args;
va_start(args, what);
vsnprintf(buf, sizeof(buf), what, args);
va_end(args);
buf[sizeof(buf) - 1] = 0;
int len = strlen(buf);
int fd = open(file, O_WRONLY | O_CLOEXEC);
if (fd == -1)
return false;
if (write(fd, buf, len) != len) {
int err = errno;
close(fd);
errno = err;
return false;
}
close(fd);
return true;
}
struct nlmsg {
char* pos;
int nesting;
struct nlattr* nested[8];
char buf[4096];
};
static void netlink_init(struct nlmsg* nlmsg, int typ, int flags,
const void* data, int size)
{
memset(nlmsg, 0, sizeof(*nlmsg));
struct nlmsghdr* hdr = (struct nlmsghdr*)nlmsg->buf;
hdr->nlmsg_type = typ;
hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
memcpy(hdr + 1, data, size);
nlmsg->pos = (char*)(hdr + 1) + NLMSG_ALIGN(size);
}
static void netlink_attr(struct nlmsg* nlmsg, int typ, const void* data,
int size)
{
struct nlattr* attr = (struct nlattr*)nlmsg->pos;
attr->nla_len = sizeof(*attr) + size;
attr->nla_type = typ;
if (size > 0)
memcpy(attr + 1, data, size);
nlmsg->pos += NLMSG_ALIGN(attr->nla_len);
}
static void netlink_nest(struct nlmsg* nlmsg, int typ)
{
struct nlattr* attr = (struct nlattr*)nlmsg->pos;
attr->nla_type = typ;
nlmsg->pos += sizeof(*attr);
nlmsg->nested[nlmsg->nesting++] = attr;
}
static void netlink_done(struct nlmsg* nlmsg)
{
struct nlattr* attr = nlmsg->nested[--nlmsg->nesting];
attr->nla_len = nlmsg->pos - (char*)attr;
}
static int netlink_send_ext(struct nlmsg* nlmsg, int sock, uint16_t reply_type,
int* reply_len, bool dofail)
{
if (nlmsg->pos > nlmsg->buf + sizeof(nlmsg->buf) || nlmsg->nesting)
exit(1);
struct nlmsghdr* hdr = (struct nlmsghdr*)nlmsg->buf;
hdr->nlmsg_len = nlmsg->pos - nlmsg->buf;
struct sockaddr_nl addr;
memset(&addr, 0, sizeof(addr));
addr.nl_family = AF_NETLINK;
ssize_t n = sendto(sock, nlmsg->buf, hdr->nlmsg_len, 0,
(struct sockaddr*)&addr, sizeof(addr));
if (n != (ssize_t)hdr->nlmsg_len) {
if (dofail)
exit(1);
return -1;
}
n = recv(sock, nlmsg->buf, sizeof(nlmsg->buf), 0);
if (reply_len)
*reply_len = 0;
if (n < 0) {
if (dofail)
exit(1);
return -1;
}
if (n < (ssize_t)sizeof(struct nlmsghdr)) {
errno = EINVAL;
if (dofail)
exit(1);
return -1;
}
if (hdr->nlmsg_type == NLMSG_DONE)
return 0;
if (reply_len && hdr->nlmsg_type == reply_type) {
*reply_len = n;
return 0;
}
if (n < (ssize_t)(sizeof(struct nlmsghdr) + sizeof(struct nlmsgerr))) {
errno = EINVAL;
if (dofail)
exit(1);
return -1;
}
if (hdr->nlmsg_type != NLMSG_ERROR) {
errno = EINVAL;
if (dofail)
exit(1);
return -1;
}
errno = -((struct nlmsgerr*)(hdr + 1))->error;
return -errno;
}
static int netlink_send(struct nlmsg* nlmsg, int sock)
{
return netlink_send_ext(nlmsg, sock, 0, NULL, true);
}
static int netlink_query_family_id(struct nlmsg* nlmsg, int sock,
const char* family_name, bool dofail)
{
struct genlmsghdr genlhdr;
memset(&genlhdr, 0, sizeof(genlhdr));
genlhdr.cmd = CTRL_CMD_GETFAMILY;
netlink_init(nlmsg, GENL_ID_CTRL, 0, &genlhdr, sizeof(genlhdr));
netlink_attr(nlmsg, CTRL_ATTR_FAMILY_NAME, family_name,
strnlen(family_name, GENL_NAMSIZ - 1) + 1);
int n = 0;
int err = netlink_send_ext(nlmsg, sock, GENL_ID_CTRL, &n, dofail);
if (err < 0) {
return -1;
}
uint16_t id = 0;
struct nlattr* attr = (struct nlattr*)(nlmsg->buf + NLMSG_HDRLEN +
NLMSG_ALIGN(sizeof(genlhdr)));
for (; (char*)attr < nlmsg->buf + n;
attr = (struct nlattr*)((char*)attr + NLMSG_ALIGN(attr->nla_len))) {
if (attr->nla_type == CTRL_ATTR_FAMILY_ID) {
id = *(uint16_t*)(attr + 1);
break;
}
}
if (!id) {
errno = EINVAL;
return -1;
}
recv(sock, nlmsg->buf, sizeof(nlmsg->buf), 0);
return id;
}
static void netlink_add_device_impl(struct nlmsg* nlmsg, const char* type,
const char* name)
{
struct ifinfomsg hdr;
memset(&hdr, 0, sizeof(hdr));
netlink_init(nlmsg, RTM_NEWLINK, NLM_F_EXCL | NLM_F_CREATE, &hdr,
sizeof(hdr));
if (name)
netlink_attr(nlmsg, IFLA_IFNAME, name, strlen(name));
netlink_nest(nlmsg, IFLA_LINKINFO);
netlink_attr(nlmsg, IFLA_INFO_KIND, type, strlen(type));
}
static void netlink_device_change(struct nlmsg* nlmsg, int sock,
const char* name, bool up, const char* master,
const void* mac, int macsize,
const char* new_name)
{
struct ifinfomsg hdr;
memset(&hdr, 0, sizeof(hdr));
if (up)
hdr.ifi_flags = hdr.ifi_change = IFF_UP;
hdr.ifi_index = if_nametoindex(name);
netlink_init(nlmsg, RTM_NEWLINK, 0, &hdr, sizeof(hdr));
if (new_name)
netlink_attr(nlmsg, IFLA_IFNAME, new_name, strlen(new_name));
if (master) {
int ifindex = if_nametoindex(master);
netlink_attr(nlmsg, IFLA_MASTER, &ifindex, sizeof(ifindex));
}
if (macsize)
netlink_attr(nlmsg, IFLA_ADDRESS, mac, macsize);
int err = netlink_send(nlmsg, sock);
if (err < 0) {
}
}
static struct nlmsg nlmsg;
#define MAX_FDS 30
#define USB_MAX_IFACE_NUM 4
#define USB_MAX_EP_NUM 32
#define USB_MAX_FDS 6
struct usb_endpoint_index {
struct usb_endpoint_descriptor desc;
int handle;
};
struct usb_iface_index {
struct usb_interface_descriptor* iface;
uint8_t bInterfaceNumber;
uint8_t bAlternateSetting;
uint8_t bInterfaceClass;
struct usb_endpoint_index eps[USB_MAX_EP_NUM];
int eps_num;
};
struct usb_device_index {
struct usb_device_descriptor* dev;
struct usb_config_descriptor* config;
uint8_t bDeviceClass;
uint8_t bMaxPower;
int config_length;
struct usb_iface_index ifaces[USB_MAX_IFACE_NUM];
int ifaces_num;
int iface_cur;
};
struct usb_info {
int fd;
struct usb_device_index index;
};
static struct usb_info usb_devices[USB_MAX_FDS];
static int usb_devices_num;
static bool parse_usb_descriptor(const char* buffer, size_t length,
struct usb_device_index* index)
{
if (length < sizeof(*index->dev) + sizeof(*index->config))
return false;
memset(index, 0, sizeof(*index));
index->dev = (struct usb_device_descriptor*)buffer;
index->config = (struct usb_config_descriptor*)(buffer + sizeof(*index->dev));
index->bDeviceClass = index->dev->bDeviceClass;
index->bMaxPower = index->config->bMaxPower;
index->config_length = length - sizeof(*index->dev);
index->iface_cur = -1;
size_t offset = 0;
while (true) {
if (offset + 1 >= length)
break;
uint8_t desc_length = buffer[offset];
uint8_t desc_type = buffer[offset + 1];
if (desc_length <= 2)
break;
if (offset + desc_length > length)
break;
if (desc_type == USB_DT_INTERFACE &&
index->ifaces_num < USB_MAX_IFACE_NUM) {
struct usb_interface_descriptor* iface =
(struct usb_interface_descriptor*)(buffer + offset);
index->ifaces[index->ifaces_num].iface = iface;
index->ifaces[index->ifaces_num].bInterfaceNumber =
iface->bInterfaceNumber;
index->ifaces[index->ifaces_num].bAlternateSetting =
iface->bAlternateSetting;
index->ifaces[index->ifaces_num].bInterfaceClass = iface->bInterfaceClass;
index->ifaces_num++;
}
if (desc_type == USB_DT_ENDPOINT && index->ifaces_num > 0) {
struct usb_iface_index* iface = &index->ifaces[index->ifaces_num - 1];
if (iface->eps_num < USB_MAX_EP_NUM) {
memcpy(&iface->eps[iface->eps_num].desc, buffer + offset,
sizeof(iface->eps[iface->eps_num].desc));
iface->eps_num++;
}
}
offset += desc_length;
}
return true;
}
static struct usb_device_index* add_usb_index(int fd, const char* dev,
size_t dev_len)
{
int i = __atomic_fetch_add(&usb_devices_num, 1, __ATOMIC_RELAXED);
if (i >= USB_MAX_FDS)
return NULL;
if (!parse_usb_descriptor(dev, dev_len, &usb_devices[i].index))
return NULL;
__atomic_store_n(&usb_devices[i].fd, fd, __ATOMIC_RELEASE);
return &usb_devices[i].index;
}
static struct usb_device_index* lookup_usb_index(int fd)
{
for (int i = 0; i < USB_MAX_FDS; i++) {
if (__atomic_load_n(&usb_devices[i].fd, __ATOMIC_ACQUIRE) == fd) {
return &usb_devices[i].index;
}
}
return NULL;
}
struct vusb_connect_string_descriptor {
uint32_t len;
char* str;
} __attribute__((packed));
struct vusb_connect_descriptors {
uint32_t qual_len;
char* qual;
uint32_t bos_len;
char* bos;
uint32_t strs_len;
struct vusb_connect_string_descriptor strs[0];
} __attribute__((packed));
static const char default_string[] = {8, USB_DT_STRING, 's', 0, 'y', 0, 'z', 0};
static const char default_lang_id[] = {4, USB_DT_STRING, 0x09, 0x04};
static bool
lookup_connect_response_in(int fd, const struct vusb_connect_descriptors* descs,
const struct usb_ctrlrequest* ctrl,
char** response_data, uint32_t* response_length)
{
struct usb_device_index* index = lookup_usb_index(fd);
uint8_t str_idx;
if (!index)
return false;
switch (ctrl->bRequestType & USB_TYPE_MASK) {
case USB_TYPE_STANDARD:
switch (ctrl->bRequest) {
case USB_REQ_GET_DESCRIPTOR:
switch (ctrl->wValue >> 8) {
case USB_DT_DEVICE:
*response_data = (char*)index->dev;
*response_length = sizeof(*index->dev);
return true;
case USB_DT_CONFIG:
*response_data = (char*)index->config;
*response_length = index->config_length;
return true;
case USB_DT_STRING:
str_idx = (uint8_t)ctrl->wValue;
if (descs && str_idx < descs->strs_len) {
*response_data = descs->strs[str_idx].str;
*response_length = descs->strs[str_idx].len;
return true;
}
if (str_idx == 0) {
*response_data = (char*)&default_lang_id[0];
*response_length = default_lang_id[0];
return true;
}
*response_data = (char*)&default_string[0];
*response_length = default_string[0];
return true;
case USB_DT_BOS:
*response_data = descs->bos;
*response_length = descs->bos_len;
return true;
case USB_DT_DEVICE_QUALIFIER:
if (!descs->qual) {
struct usb_qualifier_descriptor* qual =
(struct usb_qualifier_descriptor*)response_data;
qual->bLength = sizeof(*qual);
qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
qual->bcdUSB = index->dev->bcdUSB;
qual->bDeviceClass = index->dev->bDeviceClass;
qual->bDeviceSubClass = index->dev->bDeviceSubClass;
qual->bDeviceProtocol = index->dev->bDeviceProtocol;
qual->bMaxPacketSize0 = index->dev->bMaxPacketSize0;
qual->bNumConfigurations = index->dev->bNumConfigurations;
qual->bRESERVED = 0;
*response_length = sizeof(*qual);
return true;
}
*response_data = descs->qual;
*response_length = descs->qual_len;
return true;
default:
break;
}
break;
default:
break;
}
break;
default:
break;
}
return false;
}
typedef bool (*lookup_connect_out_response_t)(
int fd, const struct vusb_connect_descriptors* descs,
const struct usb_ctrlrequest* ctrl, bool* done);
static bool lookup_connect_response_out_generic(
int fd, const struct vusb_connect_descriptors* descs,
const struct usb_ctrlrequest* ctrl, bool* done)
{
switch (ctrl->bRequestType & USB_TYPE_MASK) {
case USB_TYPE_STANDARD:
switch (ctrl->bRequest) {
case USB_REQ_SET_CONFIGURATION:
*done = true;
return true;
default:
break;
}
break;
}
return false;
}
#define UDC_NAME_LENGTH_MAX 128
struct usb_raw_init {
__u8 driver_name[UDC_NAME_LENGTH_MAX];
__u8 device_name[UDC_NAME_LENGTH_MAX];
__u8 speed;
};
enum usb_raw_event_type {
USB_RAW_EVENT_INVALID = 0,
USB_RAW_EVENT_CONNECT = 1,
USB_RAW_EVENT_CONTROL = 2,
};
struct usb_raw_event {
__u32 type;
__u32 length;
__u8 data[0];
};
struct usb_raw_ep_io {
__u16 ep;
__u16 flags;
__u32 length;
__u8 data[0];
};
#define USB_RAW_EPS_NUM_MAX 30
#define USB_RAW_EP_NAME_MAX 16
#define USB_RAW_EP_ADDR_ANY 0xff
struct usb_raw_ep_caps {
__u32 type_control : 1;
__u32 type_iso : 1;
__u32 type_bulk : 1;
__u32 type_int : 1;
__u32 dir_in : 1;
__u32 dir_out : 1;
};
struct usb_raw_ep_limits {
__u16 maxpacket_limit;
__u16 max_streams;
__u32 reserved;
};
struct usb_raw_ep_info {
__u8 name[USB_RAW_EP_NAME_MAX];
__u32 addr;
struct usb_raw_ep_caps caps;
struct usb_raw_ep_limits limits;
};
struct usb_raw_eps_info {
struct usb_raw_ep_info eps[USB_RAW_EPS_NUM_MAX];
};
#define USB_RAW_IOCTL_INIT _IOW('U', 0, struct usb_raw_init)
#define USB_RAW_IOCTL_RUN _IO('U', 1)
#define USB_RAW_IOCTL_EVENT_FETCH _IOR('U', 2, struct usb_raw_event)
#define USB_RAW_IOCTL_EP0_WRITE _IOW('U', 3, struct usb_raw_ep_io)
#define USB_RAW_IOCTL_EP0_READ _IOWR('U', 4, struct usb_raw_ep_io)
#define USB_RAW_IOCTL_EP_ENABLE _IOW('U', 5, struct usb_endpoint_descriptor)
#define USB_RAW_IOCTL_EP_DISABLE _IOW('U', 6, __u32)
#define USB_RAW_IOCTL_EP_WRITE _IOW('U', 7, struct usb_raw_ep_io)
#define USB_RAW_IOCTL_EP_READ _IOWR('U', 8, struct usb_raw_ep_io)
#define USB_RAW_IOCTL_CONFIGURE _IO('U', 9)
#define USB_RAW_IOCTL_VBUS_DRAW _IOW('U', 10, __u32)
#define USB_RAW_IOCTL_EPS_INFO _IOR('U', 11, struct usb_raw_eps_info)
#define USB_RAW_IOCTL_EP0_STALL _IO('U', 12)
#define USB_RAW_IOCTL_EP_SET_HALT _IOW('U', 13, __u32)
#define USB_RAW_IOCTL_EP_CLEAR_HALT _IOW('U', 14, __u32)
#define USB_RAW_IOCTL_EP_SET_WEDGE _IOW('U', 15, __u32)
static int usb_raw_open()
{
return open("/dev/raw-gadget", O_RDWR);
}
static int usb_raw_init(int fd, uint32_t speed, const char* driver,
const char* device)
{
struct usb_raw_init arg;
strncpy((char*)&arg.driver_name[0], driver, sizeof(arg.driver_name));
strncpy((char*)&arg.device_name[0], device, sizeof(arg.device_name));
arg.speed = speed;
return ioctl(fd, USB_RAW_IOCTL_INIT, &arg);
}
static int usb_raw_run(int fd)
{
return ioctl(fd, USB_RAW_IOCTL_RUN, 0);
}
static int usb_raw_event_fetch(int fd, struct usb_raw_event* event)
{
return ioctl(fd, USB_RAW_IOCTL_EVENT_FETCH, event);
}
static int usb_raw_ep0_write(int fd, struct usb_raw_ep_io* io)
{
return ioctl(fd, USB_RAW_IOCTL_EP0_WRITE, io);
}
static int usb_raw_ep0_read(int fd, struct usb_raw_ep_io* io)
{
return ioctl(fd, USB_RAW_IOCTL_EP0_READ, io);
}
static int usb_raw_ep_enable(int fd, struct usb_endpoint_descriptor* desc)
{
return ioctl(fd, USB_RAW_IOCTL_EP_ENABLE, desc);
}
static int usb_raw_ep_disable(int fd, int ep)
{
return ioctl(fd, USB_RAW_IOCTL_EP_DISABLE, ep);
}
static int usb_raw_configure(int fd)
{
return ioctl(fd, USB_RAW_IOCTL_CONFIGURE, 0);
}
static int usb_raw_vbus_draw(int fd, uint32_t power)
{
return ioctl(fd, USB_RAW_IOCTL_VBUS_DRAW, power);
}
static int usb_raw_ep0_stall(int fd)
{
return ioctl(fd, USB_RAW_IOCTL_EP0_STALL, 0);
}
static void set_interface(int fd, int n)
{
struct usb_device_index* index = lookup_usb_index(fd);
if (!index)
return;
if (index->iface_cur >= 0 && index->iface_cur < index->ifaces_num) {
for (int ep = 0; ep < index->ifaces[index->iface_cur].eps_num; ep++) {
int rv = usb_raw_ep_disable(
fd, index->ifaces[index->iface_cur].eps[ep].handle);
if (rv < 0) {
} else {
}
}
}
if (n >= 0 && n < index->ifaces_num) {
for (int ep = 0; ep < index->ifaces[n].eps_num; ep++) {
int rv = usb_raw_ep_enable(fd, &index->ifaces[n].eps[ep].desc);
if (rv < 0) {
} else {
index->ifaces[n].eps[ep].handle = rv;
}
}
index->iface_cur = n;
}
}
static int configure_device(int fd)
{
struct usb_device_index* index = lookup_usb_index(fd);
if (!index)
return -1;
int rv = usb_raw_vbus_draw(fd, index->bMaxPower);
if (rv < 0) {
return rv;
}
rv = usb_raw_configure(fd);
if (rv < 0) {
return rv;
}
set_interface(fd, 0);
return 0;
}
#define USB_MAX_PACKET_SIZE 4096
struct usb_raw_control_event {
struct usb_raw_event inner;
struct usb_ctrlrequest ctrl;
char data[USB_MAX_PACKET_SIZE];
};
struct usb_raw_ep_io_data {
struct usb_raw_ep_io inner;
char data[USB_MAX_PACKET_SIZE];
};
static volatile long
syz_usb_connect_impl(uint64_t speed, uint64_t dev_len, const char* dev,
const struct vusb_connect_descriptors* descs,
lookup_connect_out_response_t lookup_connect_response_out)
{
if (!dev) {
return -1;
}
int fd = usb_raw_open();
if (fd < 0) {
return fd;
}
if (fd >= MAX_FDS) {
close(fd);
return -1;
}
struct usb_device_index* index = add_usb_index(fd, dev, dev_len);
if (!index) {
return -1;
}
char device[32];
sprintf(&device[0], "dummy_udc.%llu", procid);
int rv = usb_raw_init(fd, speed, "dummy_udc", &device[0]);
if (rv < 0) {
return rv;
}
rv = usb_raw_run(fd);
if (rv < 0) {
return rv;
}
bool done = false;
while (!done) {
struct usb_raw_control_event event;
event.inner.type = 0;
event.inner.length = sizeof(event.ctrl);
rv = usb_raw_event_fetch(fd, (struct usb_raw_event*)&event);
if (rv < 0) {
return rv;
}
if (event.inner.type != USB_RAW_EVENT_CONTROL)
continue;
char* response_data = NULL;
uint32_t response_length = 0;
if (event.ctrl.bRequestType & USB_DIR_IN) {
if (!lookup_connect_response_in(fd, descs, &event.ctrl, &response_data,
&response_length)) {
usb_raw_ep0_stall(fd);
continue;
}
} else {
if (!lookup_connect_response_out(fd, descs, &event.ctrl, &done)) {
usb_raw_ep0_stall(fd);
continue;
}
response_data = NULL;
response_length = event.ctrl.wLength;
}
if ((event.ctrl.bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD &&
event.ctrl.bRequest == USB_REQ_SET_CONFIGURATION) {
rv = configure_device(fd);
if (rv < 0) {
return rv;
}
}
struct usb_raw_ep_io_data response;
response.inner.ep = 0;
response.inner.flags = 0;
if (response_length > sizeof(response.data))
response_length = 0;
if (event.ctrl.wLength < response_length)
response_length = event.ctrl.wLength;
response.inner.length = response_length;
if (response_data)
memcpy(&response.data[0], response_data, response_length);
else
memset(&response.data[0], 0, response_length);
if (event.ctrl.bRequestType & USB_DIR_IN) {
rv = usb_raw_ep0_write(fd, (struct usb_raw_ep_io*)&response);
} else {
rv = usb_raw_ep0_read(fd, (struct usb_raw_ep_io*)&response);
}
if (rv < 0) {
return rv;
}
}
sleep_ms(200);
return fd;
}
static volatile long syz_usb_connect(volatile long a0, volatile long a1,
volatile long a2, volatile long a3)
{
uint64_t speed = a0;
uint64_t dev_len = a1;
const char* dev = (const char*)a2;
const struct vusb_connect_descriptors* descs =
(const struct vusb_connect_descriptors*)a3;
return syz_usb_connect_impl(speed, dev_len, dev, descs,
&lookup_connect_response_out_generic);
}
#define FS_IOC_SETFLAGS _IOW('f', 2, long)
static void remove_dir(const char* dir)
{
int iter = 0;
DIR* dp = 0;
retry:
while (umount2(dir, MNT_DETACH) == 0) {
}
dp = opendir(dir);
if (dp == NULL) {
if (errno == EMFILE) {
exit(1);
}
exit(1);
}
struct dirent* ep = 0;
while ((ep = readdir(dp))) {
if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0)
continue;
char filename[FILENAME_MAX];
snprintf(filename, sizeof(filename), "%s/%s", dir, ep->d_name);
while (umount2(filename, MNT_DETACH) == 0) {
}
struct stat st;
if (lstat(filename, &st))
exit(1);
if (S_ISDIR(st.st_mode)) {
remove_dir(filename);
continue;
}
int i;
for (i = 0;; i++) {
if (unlink(filename) == 0)
break;
if (errno == EPERM) {
int fd = open(filename, O_RDONLY);
if (fd != -1) {
long flags = 0;
if (ioctl(fd, FS_IOC_SETFLAGS, &flags) == 0) {
}
close(fd);
continue;
}
}
if (errno == EROFS) {
break;
}
if (errno != EBUSY || i > 100)
exit(1);
if (umount2(filename, MNT_DETACH))
exit(1);
}
}
closedir(dp);
for (int i = 0;; i++) {
if (rmdir(dir) == 0)
break;
if (i < 100) {
if (errno == EPERM) {
int fd = open(dir, O_RDONLY);
if (fd != -1) {
long flags = 0;
if (ioctl(fd, FS_IOC_SETFLAGS, &flags) == 0) {
}
close(fd);
continue;
}
}
if (errno == EROFS) {
break;
}
if (errno == EBUSY) {
if (umount2(dir, MNT_DETACH))
exit(1);
continue;
}
if (errno == ENOTEMPTY) {
if (iter < 100) {
iter++;
goto retry;
}
}
}
exit(1);
}
}
static void kill_and_wait(int pid, int* status)
{
kill(-pid, SIGKILL);
kill(pid, SIGKILL);
for (int i = 0; i < 100; i++) {
if (waitpid(-1, status, WNOHANG | __WALL) == pid)
return;
usleep(1000);
}
DIR* dir = opendir("/sys/fs/fuse/connections");
if (dir) {
for (;;) {
struct dirent* ent = readdir(dir);
if (!ent)
break;
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
continue;
char abort[300];
snprintf(abort, sizeof(abort), "/sys/fs/fuse/connections/%s/abort",
ent->d_name);
int fd = open(abort, O_WRONLY);
if (fd == -1) {
continue;
}
if (write(fd, abort, 1) < 0) {
}
close(fd);
}
closedir(dir);
} else {
}
while (waitpid(-1, status, __WALL) != pid) {
}
}
static void setup_test()
{
prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
setpgrp();
write_file("/proc/self/oom_score_adj", "1000");
}
#define NL802154_CMD_SET_SHORT_ADDR 11
#define NL802154_ATTR_IFINDEX 3
#define NL802154_ATTR_SHORT_ADDR 10
static void setup_802154()
{
int sock_route = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if (sock_route == -1)
exit(1);
int sock_generic = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
if (sock_generic < 0)
exit(1);
int nl802154_family_id =
netlink_query_family_id(&nlmsg, sock_generic, "nl802154", true);
for (int i = 0; i < 2; i++) {
char devname[] = "wpan0";
devname[strlen(devname) - 1] += i;
uint64_t hwaddr = 0xaaaaaaaaaaaa0002 + (i << 8);
uint16_t shortaddr = 0xaaa0 + i;
int ifindex = if_nametoindex(devname);
struct genlmsghdr genlhdr;
memset(&genlhdr, 0, sizeof(genlhdr));
genlhdr.cmd = NL802154_CMD_SET_SHORT_ADDR;
netlink_init(&nlmsg, nl802154_family_id, 0, &genlhdr, sizeof(genlhdr));
netlink_attr(&nlmsg, NL802154_ATTR_IFINDEX, &ifindex, sizeof(ifindex));
netlink_attr(&nlmsg, NL802154_ATTR_SHORT_ADDR, &shortaddr,
sizeof(shortaddr));
int err = netlink_send(&nlmsg, sock_generic);
if (err < 0) {
}
netlink_device_change(&nlmsg, sock_route, devname, true, 0, &hwaddr,
sizeof(hwaddr), 0);
if (i == 0) {
netlink_add_device_impl(&nlmsg, "lowpan", "lowpan0");
netlink_done(&nlmsg);
netlink_attr(&nlmsg, IFLA_LINK, &ifindex, sizeof(ifindex));
int err = netlink_send(&nlmsg, sock_route);
if (err < 0) {
}
}
}
close(sock_route);
close(sock_generic);
}
static void execute_one(void);
#define WAIT_FLAGS __WALL
static void loop(void)
{
int iter = 0;
for (;; iter++) {
char cwdbuf[32];
sprintf(cwdbuf, "./%d", iter);
if (mkdir(cwdbuf, 0777))
exit(1);
int pid = fork();
if (pid < 0)
exit(1);
if (pid == 0) {
if (chdir(cwdbuf))
exit(1);
setup_test();
execute_one();
exit(0);
}
int status = 0;
uint64_t start = current_time_ms();
for (;;) {
if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
break;
sleep_ms(1);
if (current_time_ms() - start < 5000) {
continue;
}
kill_and_wait(pid, &status);
break;
}
remove_dir(cwdbuf);
}
}
void execute_one(void)
{
NONFAILING(memcpy((void*)0x20000000,
"\x12\x01\x00\x00\xc5\x6c\x17\x10\x4f\x17\x11\xa3\x23\x91"
"\x00\x00\x00\x01\x09\x02\x1b\x00\x01\x00\x00\x00\x00\x09"
"\x04\x00\x00\x01\xff\xff\xff\x00\x09\x05\x8f\x01",
40));
NONFAILING(syz_usb_connect(0, 0x2d, 0x20000000, 0));
}
int main(void)
{
syscall(__NR_mmap, 0x1ffff000ul, 0x1000ul, 0ul, 0x32ul, -1, 0ul);
syscall(__NR_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x32ul, -1, 0ul);
syscall(__NR_mmap, 0x21000000ul, 0x1000ul, 0ul, 0x32ul, -1, 0ul);
setup_802154();
install_segv_handler();
for (procid = 0; procid < 6; procid++) {
if (fork() == 0) {
use_temporary_dir();
loop();
}
}
sleep(1000000);
return 0;
}
|
the_stack_data/72012532.c | /* PR c/63567 */
/* { dg-do compile } */
/* { dg-options "" } */
struct T { int i; };
struct S { struct T t; };
struct S s = { .t = { (int) { 1 } } };
|
the_stack_data/775360.c |
extern void __VERIFIER_error() __attribute__ ((__noreturn__));
extern void __VERIFIER_assume(int);
void __VERIFIER_assert(int cond) { if(!(cond)) { ERROR: __VERIFIER_error(); } }
extern int __VERIFIER_nondet_int(void);
int N;
int main()
{
N = __VERIFIER_nondet_int();
if(N <= 0) return 1;
int i;
int sum[1];
int a[N];
int b[N];
int c[N];
sum[0] = 0;
for(i=0; i<N; i++)
{
a[i] = 1;
}
for(i=0; i<N; i++)
{
b[i] = 1;
}
for(i=0; i<N; i++)
{
c[i] = 1;
}
for(i=0; i<N; i++)
{
sum[0] = sum[0] + a[i];
}
for(i=0; i<N; i++)
{
sum[0] = sum[0] + b[i];
}
for(i=0; i<N; i++)
{
sum[0] = sum[0] + c[i];
}
__VERIFIER_assert(sum[0] <= 2*N);
return 1;
}
|
the_stack_data/51699157.c | /*
Create a console application that prints the current date and time.
Expected Output
15 September 2015 16:25:17
*/
#include <stdio.h>
#include <time.h>
int main ()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "Current local time and date: %s", asctime (timeinfo) );
return 0;
}
|
the_stack_data/68887265.c | /*
Author : Arnob Mahmud
mail : [email protected]
*/
#include <stdio.h>
int main(int argc, char const *argv[])
{
float A[10][10], At[10][10], m, n;
printf("Enter row : ");
scanf("%f", &m);
printf("Enter column : ");
scanf("%f", &n);
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
printf("A[%d][%d] : ", i, j);
scanf("%f", &A[i][j]);
}
}
printf("Array : \n");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
printf("%.2f\t", A[i][j]);
}
printf("\n");
}
printf("Transpose of Matrix A : \n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
At[i][j] = A[j][i];
printf("%.2f\t", At[i][j]);
}
printf("\n");
}
return 0;
} |
the_stack_data/6387936.c | #include <sys/types.h>
#include <stdint.h>
#include <stddef.h>
#undef KEY
#if defined(__i386)
# define KEY '_','_','i','3','8','6'
#elif defined(__x86_64)
# define KEY '_','_','x','8','6','_','6','4'
#elif defined(__ppc__)
# define KEY '_','_','p','p','c','_','_'
#elif defined(__ppc64__)
# define KEY '_','_','p','p','c','6','4','_','_'
#endif
#define SIZE (sizeof(double))
char info_size[] = {'I', 'N', 'F', 'O', ':', 's','i','z','e','[',
('0' + ((SIZE / 10000)%10)),
('0' + ((SIZE / 1000)%10)),
('0' + ((SIZE / 100)%10)),
('0' + ((SIZE / 10)%10)),
('0' + (SIZE % 10)),
']',
#ifdef KEY
' ','k','e','y','[', KEY, ']',
#endif
'\0'};
#ifdef __CLASSIC_C__
int main(argc, argv) int argc; char *argv[];
#else
int main(int argc, char *argv[])
#endif
{
int require = 0;
require += info_size[argc];
(void)argv;
return require;
}
|
Subsets and Splits