<commit_before>/*
 * Copyright (c) 2009 - 2011, <NAME> and <NAME>
 * Based on the C# implementation
 */

import java.io.File;
import java.io.RandomAccessFile;

import com.xoba.util.ILogger;
import com.xoba.util.LogFactory;

public class Retro {

	private static final ILogger logger = LogFactory.getDefault().create();

	private int sp, rsp, ip;
	private final int ports[] = new int[12];

	private final int data[], address[], memory[];

	public static final int VM_NOP = 0;
	public static final int VM_LIT = 1;
	public static final int VM_DUP = 2;
	public static final int VM_DROP = 3;
	public static final int VM_SWAP = 4;
	public static final int VM_PUSH = 5;
	public static final int VM_POP = 6;
	public static final int VM_LOOP = 7;
	public static final int VM_JUMP = 8;
	public static final int VM_RETURN = 9;
	public static final int VM_LT_JUMP = 10;
	public static final int VM_GT_JUMP = 11;
	public static final int VM_NE_JUMP = 12;
	public static final int VM_EQ_JUMP = 13;
	public static final int VM_FETCH = 14;
	public static final int VM_STORE = 15;
	public static final int VM_ADD = 16;
	public static final int VM_SUB = 17;
	public static final int VM_MUL = 18;
	public static final int VM_DIVMOD = 19;
	public static final int VM_AND = 20;
	public static final int VM_OR = 21;
	public static final int VM_XOR = 22;
	public static final int VM_SHL = 23;
	public static final int VM_SHR = 24;
	public static final int VM_ZERO_EXIT = 25;
	public static final int VM_INC = 26;
	public static final int VM_DEC = 27;
	public static final int VM_IN = 28;
	public static final int VM_OUT = 29;
	public static final int VM_WAIT = 30;

	/**
	 * Returns the value in the opposite endian
	 * 
	 * @return int
	 */
	public int switchEndian(int value) {
		int b1 = value & 0xff;
		int b2 = (value >> 8) & 0xff;
		int b3 = (value >> 16) & 0xff;
		int b4 = (value >> 24) & 0xff;
		return b1 << 24 | b2 << 16 | b3 << 8 | b4;
	}

	public void loadImage(String name) {
		memory[0] = 0;
		File f = new File(name);
		if (f.exists()) {
			try {
				RandomAccessFile in = new RandomAccessFile(f, "r");
				long n = in.length() / 4;
				try {
					for (int i = 0; i < n; i++) {
						memory[i] = switchEndian(in.readInt());
					}
				} finally {
					in.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void saveImage(String name) {
		try {
			RandomAccessFile out = new RandomAccessFile(name, "rw");
			try {
				for (int i = 0; i < memory.length; i++) {
					out.writeInt(switchEndian(memory[i]));
				}
			} finally {
				out.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void initialize() {
		for (int i = 0; i < memory.length; i++) {
			memory[i] = 0;
		}
		loadImage("retroImage");
		if (memory[0] == 0) {
			System.out.println("Could not find image file!");
			System.exit(-1);
		}
	}

	public Retro(int dataStackSize, int addressStackSize, int memorySize) {
		data = new int[dataStackSize];
		address = new int[addressStackSize];
		memory = new int[memorySize];
	}

	public void handleDevices() {

		if (ports[0] == 1) {
			return;
		}
<commit_msg>starting to abstract memory a bit more now...<commit_after>/*
 * Copyright (c) 2009 - 2011, <NAME> and <NAME>
 * Based on the C# implementation
 */

import java.io.File;
import java.io.RandomAccessFile;

import com.xoba.util.ILogger;
import com.xoba.util.LogFactory;

public class Retro {

	private static final ILogger logger = LogFactory.getDefault().create();

	private int sp, rsp, ip;
	private final int ports[] = new int[12];

	private final int data[], address[], memory[];

	public static final int VM_NOP = 0;
	public static final int VM_LIT = 1;
	public static final int VM_DUP = 2;
	public static final int VM_DROP = 3;
	public static final int VM_SWAP = 4;
	public static final int VM_PUSH = 5;
	public static final int VM_POP = 6;
	public static final int VM_LOOP = 7;
	public static final int VM_JUMP = 8;
	public static final int VM_RETURN = 9;
	public static final int VM_LT_JUMP = 10;
	public static final int VM_GT_JUMP = 11;
	public static final int VM_NE_JUMP = 12;
	public static final int VM_EQ_JUMP = 13;
	public static final int VM_FETCH = 14;
	public static final int VM_STORE = 15;
	public static final int VM_ADD = 16;
	public static final int VM_SUB = 17;
	public static final int VM_MUL = 18;
	public static final int VM_DIVMOD = 19;
	public static final int VM_AND = 20;
	public static final int VM_OR = 21;
	public static final int VM_XOR = 22;
	public static final int VM_SHL = 23;
	public static final int VM_SHR = 24;
	public static final int VM_ZERO_EXIT = 25;
	public static final int VM_INC = 26;
	public static final int VM_DEC = 27;
	public static final int VM_IN = 28;
	public static final int VM_OUT = 29;
	public static final int VM_WAIT = 30;

	/**
	 * Returns the value in the opposite endian
	 * 
	 * @return int
	 */
	public int switchEndian(int value) {
		int b1 = value & 0xff;
		int b2 = (value >> 8) & 0xff;
		int b3 = (value >> 16) & 0xff;
		int b4 = (value >> 24) & 0xff;
		return b1 << 24 | b2 << 16 | b3 << 8 | b4;
	}

	public void loadImage(String name) {
		memory[0] = 0;
		File f = new File(name);
		if (f.exists()) {
			try {
				RandomAccessFile in = new RandomAccessFile(f, "r");
				long n = in.length() / 4;
				try {
					for (int i = 0; i < n; i++) {
						memory[i] = switchEndian(in.readInt());
					}
				} finally {
					in.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void saveImage(String name) {
		try {
			RandomAccessFile out = new RandomAccessFile(name, "rw");
			try {
				for (int i = 0; i < memory.length; i++) {
					out.writeInt(switchEndian(memory[i]));
				}
			} finally {
				out.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void initialize() {
		for (int i = 0; i < memory.length; i++) {
			memory[i] = 0;
		}
		loadImage("retroImage");
		if (memory[0] == 0) {
			System.out.println("Could not find image file!");
			System.exit(-1);
		}
	}

	public Retro(int dataStackSize, int addressStackSize, int memorySize) {
		data = new int[dataStackSize];
		address = new int[addressStackSize];
		memory = new int[memorySize];
	}

	private static interface IMemory {

		public int get(int pc);

		public void set(int pc, int value);

		public void set(int pc, int[] buffer);

	}

	public void handleDevices() {

		if (ports[0] == 1) {
			return;
		}

{'fraction_non_alphanumeric': 0.11482806908355375, 'fraction_numerical': 0.02987396919246927, 'mean_word_length': 3.179453836150845, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 19, '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': '1268047', 'n_tokens_mistral': 2625, 'n_tokens_neox': 2231, 'n_words': 789}