<commit_before>#include <unistd.h>

#include "emu.h"
#include "instr.h"

struct inprec {
	uint64_t	 ir_insn;
	size_t		 ir_len;
	char		 ir_inp[0];
};

/* Machine state */
uint32_t	 pc,
		 pc_start,
		 instr_size;
bool		 skip_next_instruction;
/* 24 bits are addressable in both Data and Program regions */
uint8_t		 memory[0x1000000];
/* Program memory is word-addressed */
uint16_t	 flash[ 0x1000000 / sizeof(uint16_t)];

/* Machine model characteristics */
bool		 pc22;
bool		 pc_mem_max_64k;
bool		 pc_mem_max_256b;

/* Emulater / GDB auxiliary info */
uint64_t	 start;		/* Start time in us */
uint64_t	 insns;
uint64_t	 insnlimit;
uint64_t	 insnreplaylim;
bool		 off;
bool		 replay_mode;
bool		 ctrlc;

bool		 tracehex;
FILE		*tracefile;

GHashTable	*input_record;			// insns -> inprec

// Could easily sort by popularity over time.
static struct instr_decode avr_instr[] = {
	{ 0x0000, 0xffff, instr_nop },
	{ 0x0100, 0xff00, instr_movw, .dddd74 = true, .rrrr30 = true },
	{ 0x0200, 0xff00, instr_muls, .dddd74 = true, .rrrr30 = true },
	{ 0x0300, 0xff88, instr_mulsu, .ddd64 = true, .rrr20 = true },
	{ 0x0308, 0xff88, instr_fmul, .ddd64 = true, .rrr20 = true },
	{ 0x0380, 0xff80, instr_fmulsu, .ddd64 = true, .rrr20 = true },
	{ 0x0400, 0xec00, instr_cpc, .ddddd84 = true, .rrrrr9_30 = true },
	{ 0x0800, 0xec00, instr_cpc, .ddddd84 = true, .rrrrr9_30 = true },
	{ 0x0c00, 0xec00, instr_adc, .ddddd84 = true, .rrrrr9_30 = true },
	{ 0x1000, 0xfc00, instr_cpse, .ddddd84 = true, .rrrrr9_30 = true },
	{ 0x2000, 0xfc00, instr_and, .ddddd84 = true, .rrrrr9_30 = true },
	{ 0x2400, 0xfc00, instr_xor, .ddddd84 = true, .rrrrr9_30 = true },
	{ 0x2800, 0xfc00, instr_or, .ddddd84 = true, .rrrrr9_30 = true },
	{ 0x2c00, 0xfc00, instr_mov, .ddddd84 = true, .rrrrr9_30 = true },
	{ 0x3000, 0xf000, instr_cpi, .dddd74 = true, .KKKK118_30 = true },
	{ 0x4000, 0xe000, instr_cpi, .dddd74 = true, .KKKK118_30 = true },
	{ 0x6000, 0xf000, instr_ori, .dddd74 = true, .KKKK118_30 = true },
	{ 0x7000, 0xf000, instr_andi, .dddd74 = true, .KKKK118_30 = true },
	{ 0x8000, 0xd200, instr_ldyz, .ddddd84 = true },
	{ 0x8200, 0xd200, instr_styz, .ddddd84 = true },
	{ 0x9000, 0xfe0f, instr_lds, .ddddd84 = true, .imm16 = true },
	{ 0x9001, 0xfe07, instr_ldyz, .ddddd84 = true },
	{ 0x9002, 0xfe07, instr_ldyz, .ddddd84 = true },
	{ 0x9004, 0xfe0c, instr_elpmz, .ddddd84 = true },
	{ 0x900c, 0xfe0e, instr_ldx, .ddddd84 = true },
	{ 0x900e, 0xfe0f, instr_ldx, .ddddd84 = true },
	{ 0x900f, 0xfe0f, instr_pop, .ddddd84 = true },
	{ 0x9200, 0xfe0f, instr_sts, .ddddd84 = true, .imm16 = true },
	{ 0x9201, 0xfe07, instr_styz, .ddddd84 = true },
	{ 0x9202, 0xfe07, instr_styz, .ddddd84 = true },
	{ 0x9204, 0xfe0f, instr_unimp/*XCH*/, .ddddd84 = true },
	{ 0x9205, 0xfe0f, instr_unimp/*LAS*/, .ddddd84 = true },
	{ 0x9206, 0xfe0f, instr_unimp/*LAC*/, .ddddd84 = true },
	{ 0x9207, 0xfe0f, instr_unimp/*LAT*/, .ddddd84 = true },
	{ 0x920c, 0xfe0e, instr_stx, .ddddd84 = true },
	{ 0x920e, 0xfe0f, instr_stx, .ddddd84 = true },
	{ 0x920f, 0xfe0f, instr_push, .ddddd84 = true },
	{ 0x9400, 0xfe0f, instr_com, .ddddd84 = true },
	{ 0x9401, 0xfe0f, instr_neg, .ddddd84 = true },
	{ 0x9402, 0xfe0f, instr_swap, .ddddd84 = true },
	{ 0x9403, 0xfe0f, instr_inc, .ddddd84 = true },
	{ 0x9405, 0xfe0f, instr_asr, .ddddd84 = true },
	{ 0x9406, 0xfe0f, instr_lsr, .ddddd84 = true },
	{ 0x9407, 0xfe0f, instr_ror, .ddddd84 = true },
	{ 0x9408, 0xff0f, instr_bclrset, .ddd64 = true },
	{ 0x940a, 0xfe0f, instr_dec, .ddddd84 = true },
	{ 0x940b, 0xff0f, instr_unimp/*DES(k)*/, .dddd74 = true },
	{ 0x940c, 0xfe0e, instr_jmp, .imm16 = true },
	{ 0x940e, 0xfe0e, instr_call, .imm16 = true },
	{ 0x9508, 0xffff, instr_ret },
	{ 0x9409, 0xfeef, instr_eicalljump },
	{ 0x9518, 0xffff, instr_reti },
	{ 0x9588, 0xffff, instr_unimp/*SLEEP*/ },
	{ 0x9598, 0xffff, instr_unimp/*BREAK*/ },<commit_msg>avr-emu: Decode WDR<commit_after>#include <unistd.h>

#include "emu.h"
#include "instr.h"

struct inprec {
	uint64_t	 ir_insn;
	size_t		 ir_len;
	char		 ir_inp[0];
};

/* Machine state */
uint32_t	 pc,
		 pc_start,
		 instr_size;
bool		 skip_next_instruction;
/* 24 bits are addressable in both Data and Program regions */
uint8_t		 memory[0x1000000];
/* Program memory is word-addressed */
uint16_t	 flash[ 0x1000000 / sizeof(uint16_t)];

/* Machine model characteristics */
bool		 pc22;
bool		 pc_mem_max_64k;
bool		 pc_mem_max_256b;

/* Emulater / GDB auxiliary info */
uint64_t	 start;		/* Start time in us */
uint64_t	 insns;
uint64_t	 insnlimit;
uint64_t	 insnreplaylim;
bool		 off;
bool		 replay_mode;
bool		 ctrlc;

bool		 tracehex;
FILE		*tracefile;

GHashTable	*input_record;			// insns -> inprec

// Could easily sort by popularity over time.
static struct instr_decode avr_instr[] = {
	{ 0x0000, 0xffff, instr_nop },
	{ 0x0100, 0xff00, instr_movw, .dddd74 = true, .rrrr30 = true },
	{ 0x0200, 0xff00, instr_muls, .dddd74 = true, .rrrr30 = true },
	{ 0x0300, 0xff88, instr_mulsu, .ddd64 = true, .rrr20 = true },
	{ 0x0308, 0xff88, instr_fmul, .ddd64 = true, .rrr20 = true },
	{ 0x0380, 0xff80, instr_fmulsu, .ddd64 = true, .rrr20 = true },
	{ 0x0400, 0xec00, instr_cpc, .ddddd84 = true, .rrrrr9_30 = true },
	{ 0x0800, 0xec00, instr_cpc, .ddddd84 = true, .rrrrr9_30 = true },
	{ 0x0c00, 0xec00, instr_adc, .ddddd84 = true, .rrrrr9_30 = true },
	{ 0x1000, 0xfc00, instr_cpse, .ddddd84 = true, .rrrrr9_30 = true },
	{ 0x2000, 0xfc00, instr_and, .ddddd84 = true, .rrrrr9_30 = true },
	{ 0x2400, 0xfc00, instr_xor, .ddddd84 = true, .rrrrr9_30 = true },
	{ 0x2800, 0xfc00, instr_or, .ddddd84 = true, .rrrrr9_30 = true },
	{ 0x2c00, 0xfc00, instr_mov, .ddddd84 = true, .rrrrr9_30 = true },
	{ 0x3000, 0xf000, instr_cpi, .dddd74 = true, .KKKK118_30 = true },
	{ 0x4000, 0xe000, instr_cpi, .dddd74 = true, .KKKK118_30 = true },
	{ 0x6000, 0xf000, instr_ori, .dddd74 = true, .KKKK118_30 = true },
	{ 0x7000, 0xf000, instr_andi, .dddd74 = true, .KKKK118_30 = true },
	{ 0x8000, 0xd200, instr_ldyz, .ddddd84 = true },
	{ 0x8200, 0xd200, instr_styz, .ddddd84 = true },
	{ 0x9000, 0xfe0f, instr_lds, .ddddd84 = true, .imm16 = true },
	{ 0x9001, 0xfe07, instr_ldyz, .ddddd84 = true },
	{ 0x9002, 0xfe07, instr_ldyz, .ddddd84 = true },
	{ 0x9004, 0xfe0c, instr_elpmz, .ddddd84 = true },
	{ 0x900c, 0xfe0e, instr_ldx, .ddddd84 = true },
	{ 0x900e, 0xfe0f, instr_ldx, .ddddd84 = true },
	{ 0x900f, 0xfe0f, instr_pop, .ddddd84 = true },
	{ 0x9200, 0xfe0f, instr_sts, .ddddd84 = true, .imm16 = true },
	{ 0x9201, 0xfe07, instr_styz, .ddddd84 = true },
	{ 0x9202, 0xfe07, instr_styz, .ddddd84 = true },
	{ 0x9204, 0xfe0f, instr_unimp/*XCH*/, .ddddd84 = true },
	{ 0x9205, 0xfe0f, instr_unimp/*LAS*/, .ddddd84 = true },
	{ 0x9206, 0xfe0f, instr_unimp/*LAC*/, .ddddd84 = true },
	{ 0x9207, 0xfe0f, instr_unimp/*LAT*/, .ddddd84 = true },
	{ 0x920c, 0xfe0e, instr_stx, .ddddd84 = true },
	{ 0x920e, 0xfe0f, instr_stx, .ddddd84 = true },
	{ 0x920f, 0xfe0f, instr_push, .ddddd84 = true },
	{ 0x9400, 0xfe0f, instr_com, .ddddd84 = true },
	{ 0x9401, 0xfe0f, instr_neg, .ddddd84 = true },
	{ 0x9402, 0xfe0f, instr_swap, .ddddd84 = true },
	{ 0x9403, 0xfe0f, instr_inc, .ddddd84 = true },
	{ 0x9405, 0xfe0f, instr_asr, .ddddd84 = true },
	{ 0x9406, 0xfe0f, instr_lsr, .ddddd84 = true },
	{ 0x9407, 0xfe0f, instr_ror, .ddddd84 = true },
	{ 0x9408, 0xff0f, instr_bclrset, .ddd64 = true },
	{ 0x940a, 0xfe0f, instr_dec, .ddddd84 = true },
	{ 0x940b, 0xff0f, instr_unimp/*DES(k)*/, .dddd74 = true },
	{ 0x940c, 0xfe0e, instr_jmp, .imm16 = true },
	{ 0x940e, 0xfe0e, instr_call, .imm16 = true },
	{ 0x9508, 0xffff, instr_ret },
	{ 0x9409, 0xfeef, instr_eicalljump },
	{ 0x9518, 0xffff, instr_reti },
	{ 0x9588, 0xffff, instr_unimp/*SLEEP*/ },
	{ 0x9598, 0xffff, instr_unimp/*BREAK*/ },
	{ 0x95a8, 0xffff, instr_unimp/*WDR*/ },
{'fraction_non_alphanumeric': 0.1532413970872535, 'fraction_numerical': 0.15169480603170513, 'mean_word_length': 3.862155388471178, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3963707', 'n_tokens_mistral': 4447, 'n_tokens_neox': 3733, 'n_words': 781}