SilabGarza - RS Private Server  
Home Forum Play Register Store Market Highscores Mark Forums Read
Go Back   SilabGarza - RS Private Server > SilGar Media > Computers > Programming
Reload this Page jDCPU-16


jDCPU-16
Old
  (#1 (permalink))
Sinisoul is Offline
Senior Member
image
 
Sinisoul's Avatar
 
Posts: 230
Cookies Given: 1
Cookies Received: 16
Time Online: 9 d 15 h 40 m
   
Default jDCPU-16 - 05-06-2012, 11:21 PM

Here is one of my programming projects. It's a virtual CPU emulator that follows this specification: http://0x10c.com/doc/dcpu-16.txt. It was semi popular for a bit when the game was announced but feel out of popularity for GUI based emulators. This was one of the hardest projects I had to undertake but was one of the most rewarding for me because this was the time when I initially learned regular expressions The assembler takes in the DCPU like ASM language, here is an example I wrote up earlier for the XTEA block cipher:

Code:
;                     _____              _______    _    _         
;                    |  __     \             |__   __| | |  | |
;                    |   |__) |   _ _ __   ___| | ___|  | _| | __ 
;                    |   _  / | | | '_ \ / _ \ |/ _ \ |/ / |/ /  
;                    | | \ \ |_| | | | |  __/ |  __/   <|   <   
;                    |_|  \_\__,_|_| |_|\___|_|\___|_|\_\_|\_\
;                                    
;                                   xtea.dasm
;
;  Notes: Running subroutines within a subroutine is not allowed and will need to be
;         fixed so that it is possible to do so. Register A is used for most calculations
;		  coupled with the stack because there needs to be a solid state mediator. 
;

; Push the two values we want to encipher.
SET PUSH, 0x20      ; rounds
SET PUSH, 0x000A    ; k3
SET PUSH, 0x000B    ; k2
SET PUSH, 0x000C    ; k1
SET PUSH, 0x000D    ; k0
SET PUSH, 0x0001    ; v1
SET PUSH, 0x0000    ; v0
JSR encipher        ; Memory address pushed to stack of where the data was stored.
SET A, POP          ; Fetch the memory address pushed to the stack in the previous sub routine.
SET PUSH, 0x20      ; rounds
SET PUSH, 0x000A    ; k3
SET PUSH, 0x000B    ; k2
SET PUSH, 0x000C    ; k1
SET PUSH, 0x000D    ; k0
SET PUSH, [1+A]     ; v1
SET PUSH, [A]       ; v0
JSR decipher        ; Memory address pushed to stack of where the data was stored.
SET PC, kill

; Sub routine to encipher.
:encipher
	SET Z, POP           ; Store the sub routine reference.
	                     ; Pop all the parameters off the stack.
	SET [0x2000], POP    ; v0
	SET [0x2001], POP    ; v1
	SET [0x2002], POP    ; k0
	SET [0x2003], POP    ; k1
	SET [0x2004], POP    ; k2
	SET [0x2005], POP    ; k3
	SET [0x2006], POP    ; rounds
	SET [0x2007], 0xF1BB ; Calculated ratio.
	SET [0x2008], 0x0000 ; Sum
	SET I, 0x0           ; Set the iterator for the loop to zero
	:encipherloop
		SET A, [0x2001]           ; Set register A to v1
		SHR A, 4                  ; Weird endian-ness.
		SET PUSH, A               ; Push the expression onto the stack.
		SET A, [0x2001]           ; Set register A to v1
		SHL A, 5                  ; Weird endian-ness.
		XOR A, POP                ; Evaluate the last expression with the current one.
		ADD A, [0x2001]           ; Add v1 to register A.
		SET PUSH, A               ; Push the expresison onto the stack.
		SET A, [0x2008]           ; Set register A to the sum.
		AND A, 0x3                ; Bit AND A to get the current key.
		SET A, [0x2002+A]         ; Set register A to current key.
		ADD A, [0x2008]           ; Add sum.
		XOR A, POP                ; Set register A to XOR and the previous expression.
		ADD [0x2000], A           ; Add the previous expression to v0.
		ADD [0x2008], [0x2007]    ; Add the delta to sum.
		SET A, [0x2000]           ; Set register A to v0
		SHR A, 4                  ; Weird endian-ness.
		SET PUSH, A               ; Push the expression onto the stack.
		SET A, [0x2000]           ; Set register A to v0
		SHL A, 5                  ; Weird endian-ness.
		XOR A, POP                ; Evaluate the last expression with the current one.
		ADD A, [0x2000]           ; Add v0 to register A.
		SET PUSH, A               ; Push the expresison onto the stack.
		SET A, [0x2008]           ; Set register A to the sum.
		SHL A, 11                 ; Weird endian-ness.
		AND A, 0x3                ; Bit AND A to get the current key.
		SET A, [0x2002+A]         ; Set register A to current key.
		ADD A, [0x2008]           ; Add sum.
		XOR A, POP                ; Set register A to XOR and the previous expression.
		ADD [0x2001], A           ; Add the previous expression to v0.
		ADD I, 0x1
		IFG [0x2006], I
			set PC, encipherloop	
	SET PUSH, 0x2000              ; Push the address that the data is located at.
	SET PC, Z                     ; Jump back to the point where the routine was called.

; Subroutine to decipher
:decipher
	SET Z, POP              ; Store the sub routine reference.
	                        ; Pop all the parameters off the stack.
	SET [0x2000], POP    ; v0
	SET [0x2001], POP    ; v1
	SET [0x2002], POP    ; k0
	SET [0x2003], POP    ; k1
	SET [0x2004], POP    ; k2
	SET [0x2005], POP    ; k3
	SET [0x2006], POP    ; rounds
	SET [0x2007], 0xF1BB ; Calculated ratio.
	SET [0x2008], [0x2006]
	MUL [0x2008], [0x2007]
	SET I, 0x0
	:decipherloop
		SET A, [0x2000]           ; Set register A to v1
		SHR A, 4                  ; Weird endian-ness.
		SET PUSH, A               ; Push the expression onto the stack.
		SET A, [0x2000]           ; Set register A to v1
		SHL A, 5                  ; Weird endian-ness.
		XOR A, POP                ; Evaluate the last expression with the current one.
		ADD A, [0x2000]           ; Add v1 to register A.
		SET PUSH, A               ; Push the expresison onto the stack.
		SET A, [0x2008]           ; Set register A to the sum.
		SHL A, 11                 ; Weird endian-ness.
		AND A, 0x3                ; Bit AND A to get the current key.
		SET A, [0x2002+A]         ; Set register A to current key.
		ADD A, [0x2008]           ; Add sum.
		XOR A, POP                ; Set register A to XOR and the previous expression.
		SUB [0x2001], A           ; Add the previous expression to v0.
		SUB [0x2008], [0x2007]    ; Add the delta to sum.
		SET A, [0x2001]           ; Set register A to v0
		SHR A, 4                  ; Weird endian-ness.
		SET PUSH, A               ; Push the expression onto the stack.
		SET A, [0x2001]           ; Set register A to v0
		SHL A, 5                  ; Weird endian-ness.
		XOR A, POP                ; Evaluate the last expression with the current one.
		ADD A, [0x2001]           ; Add v0 to register A.
		SET PUSH, A               ; Push the expresison onto the stack.
		SET A, [0x2008]           ; Set register A to the sum.
		AND A, 0x3                ; Bit AND A to get the current key.
		SET A, [0x2002+A]         ; Set register A to current key.
		ADD A, [0x2008]           ; Add sum.
		XOR A, POP                ; Set register A to XOR and the previous expression.
		SUB [0x2000], A           ; Add the previous expression to v0.
		ADD I, 0x1
		IFG [0x2006], I
			set PC, decipherloop
	SET PUSH, 0x2000     ; Push the address that the data is located at.
	SET PC, Z            ; Jump back to the point where the routine was called.

; Just in case we are feeling a bit down :(
:kill
Code:
package org.sini;

import java.io.InputStream;
import java.io.IOException;
import static org.sini.Ops.*;

/**
 * Cpu.java
 * @version 1.0.1
 */
public final class Cpu {
    
    /**
     * As of DCPU-16 Version 1.1 the amount of memory is 128 kilobytes. 
     */
    private static final int AMOUNT_MEMORY = 0x10000;
    
    /**
     * The starting index in the memory array of video ram.
     */
    public static final int VIDEO_RAM = 0x8000;
    
    /**
     * The amount of registers in the DCPU.
     */
    private static final int AMOUNT_REGISTERS = 8;
    
    /**
     * The program counter index in the registers array.
     */
    public static final int PC = 8;
    
    /**
     * The stack pointer index in the registers array.
     */
    public static final int SP = 9;
    
    /**
     * The overflow index in the registers array.
     */
    public static final int O = 10;
    
    /**
     * The cycle index in the registers array.
     */
    public static final int C = 11;
    
    /**
     * The local memory of this [email protected] Cpu}.
     */
    Cell[] m;
    
    /**
     * The registers for the [email protected] Cpu}.
     */
    Cell[] r;
    
    /**
     * The [email protected] Cpu} will print out debugging information.
     */
    boolean debug;
    
    /**
     * Initializes this [email protected] Cpu}.
     */
    public void initialize() {
        m = new Cell[AMOUNT_MEMORY];
        for(int i = 0; i < m.length; i++)
            m[i] = new Cell(i);
        r = new Cell[AMOUNT_REGISTERS + 4];   
        for(int i = 0; i < r.length; i++)
            r[i] = new Cell(i);
        r[SP].v = 0xFFFF;
    }
    
    /**
     * Executes a program.
     * @param is The [email protected] InputStream} to mount the memory from.
     */
    public void execute(InputStream is) throws IOException {
        mount(is);
        execute();
    }
    
    /**
     * Executes a program.
     * @param memory The short array that represents the memory of the program.
     */
    public void execute(int[] memory) {
        mount(memory);
        execute();
    }
    
    /**
     * Executes the currently mounted program.
     */
    public void execute() {
        r[PC].v = 0;
        while(true) {
            int op = m[r[PC].v++].v;
            if((op & 0xF) != 0) {
                Object a = getValue(op >>> 4 & 0x3F, true);                                
                Object b = getValue(op >>> 10 & 0x3F, true);
                if(!(a instanceof Cell)) {
                    Object temp = a;
                    a = b;
                    b = temp;
                }   
                boolean bothLiteral = !(a instanceof Cell) && !(b instanceof Cell);
                int aValue = a instanceof Cell ? ((Cell) a).v : (Integer) a;
                int bValue = b instanceof Cell ? ((Cell) b).v : (Integer) b;
                switch(op & 0xF) {

                    case OP_SET:  
                        if(!bothLiteral)
                            ((Cell) a).v = bValue;
                        r[C].v++;
                        break;

                    case OP_ADD:  
                        if(!bothLiteral) {
                            int value = ((Cell) a).v + bValue;
                            if(value > 0xFFFF) {
                                r[O].v = 0x0001;
                                value &= 0xFFFF;
                            }
                            ((Cell) a).v = value;
                        }
                        r[C].v += 2;
                        break;

                    case OP_SUB:
                        if(!bothLiteral) {
                            int value = ((Cell) a).v - bValue;
                            if(value < 0) {
                                r[O].v = 0xFFFF;
                                value &= 0xFFFF;
                            }
                            ((Cell) a).v = value;
                        }
                        r[C].v += 2;
                        break;

                    case OP_MUL:
                        if(!bothLiteral) {
                            int value = ((Cell) a).v * bValue;
                            ((Cell) a).v = value & 0xFFFF;
                            r[O].v = value >>> 16;
                        }
                        r[C].v += 2;
                        break;

                    case OP_DIV:
                        if(!bothLiteral) {
                            if(bValue == 0) {
                                ((Cell) a).v = 0;
                                r[O].v = 0;
                            } else {
                                ((Cell) a).v = ((Cell) a).v/bValue & 0xFFFF;
                                r[O].v = (((Cell) a).v << 16)/bValue & 0xFFFF;
                            }
                        }
                        r[C].v += 3;
                        break;

                    case OP_MOD:
                        if(!bothLiteral) {
                            if(bValue == 0) {
                                ((Cell) a).v = 0;
                            } else
                                ((Cell) a).v = ((Cell) a).v % bValue;
                        }
                        r[C].v += 3;
                        break;

                    case OP_SHL:
                        if(!bothLiteral) {
                            int value = ((Cell) a).v << bValue;
                            r[O].v = value >>> 16;
                            ((Cell) a).v = value & 0xFFFF;
                        }
                        r[C].v += 2;
                        break;

                    case OP_SHR:
                        if(!bothLiteral) {
                            ((Cell) a).v = ((Cell) a).v >>> bValue;
                            r[O].v = ((Cell) a).v << 16 >>> bValue & 0xFFFF;
                        }
                        r[C].v += 2;
                        break;

                    case OP_AND:
                        if(!bothLiteral)
                            ((Cell) a).v &= bValue;
                        r[C].v++;
                        break;

                    case OP_BOR:
                        if(!bothLiteral)
                            ((Cell) a).v |= bValue;
                        r[C].v++;
                        break;

                    case OP_XOR:
                        if(!bothLiteral)
                            ((Cell) a).v ^= bValue;
                        r[C].v++;
                        break;

                    case OP_IFE:
                        if(aValue != bValue) {
                            op = m[r[PC].v++].v;
                            getValue(op >>> 4 & 0x3F, false);
                            getValue(op >>> 10 & 0x3F, false);
                            r[C].v++;
                        }
                        r[C].v += 2;
                        break;

                    case OP_IFN:
                        if(aValue == bValue) {
                            op = m[r[PC].v++].v;
                            getValue(op >>> 4 & 0x3F, false);
                            getValue(op >>> 10 & 0x3F, false);
                            r[C].v++;
                        }
                        r[C].v += 2;
                        break;

                    case OP_IFG:
                        if(aValue <= bValue) {
                            op = m[r[PC].v++].v;
                            getValue(op >>> 4 & 0x3F, false);
                            getValue(op >>> 10 & 0x3F, false);
                            r[C].v++;
                        }
                        r[C].v += 2;
                        break;

                     case OP_IFB:
                        if((aValue & bValue) == 0) {
                            op = m[r[PC].v++].v;
                            getValue(op >>> 4 & 0x3F, false);
                            getValue(op >>> 10 & 0x3F, false);
                            r[C].v++;
                        }
                        r[C].v += 2;
                        break;
                }
            } else {
                op >>>= 4;
                Object a = getValue(op >>> 6, true);
                int aValue = a instanceof Cell ? ((Cell) a).v : (Integer) a;
                switch(op & 0x3F) {

                    case 0:
                        return;
                        
                    case OP_JSR:
                        Cell cell = m[--r[SP].v];
                        cell.v = r[PC].v;
                        r[PC].v = aValue;
                        r[C].v += 2;
                        break;
                }
            }
        }
    }
    
    /**
     * Gets the v from a v opcode.
     * @param op The opcode.
     * @param modify Modify the stack pointer.
     * @return The v from the opcode.
     */
    public Object getValue(int op, boolean modify) {
        switch(op) {
            
            case 0x00:
            case 0x01:
            case 0x02:
            case 0x03:
            case 0x04:
            case 0x05:
            case 0x06:
            case 0x07:
                return r[op];
                
            case 0x08:
            case 0x09:
            case 0x0A:
            case 0x0B:
            case 0x0C:
            case 0x0D:
            case 0x0E:
            case 0x0F:
                return m[r[op - 0x08].v];
            
            case 0x10:
            case 0x11:
            case 0x12:
            case 0x13:
            case 0x14:
            case 0x15:
            case 0x16:
            case 0x17:
                r[C].v ++;
                return m[r[op - 0x10].v + m[r[PC].v++].v];
                
            case 0x18:
                return m[modify ? r[SP].v++ : r[SP].v];
                
            case 0x19:
                return m[r[SP].v];      
                
            case 0x1A:
                return m[modify ? --r[SP].v : r[SP].v];
            
            case 0x1B:
                return r[SP]; 
            
            case 0x1C:
                return r[PC];
                
            case 0x1D:
                return r[O];
                
            case 0x1E:
                r[C].v++;
                return m[m[r[PC].v++].v];
                
            case 0x1F:
                r[C].v++;
                return m[r[PC].v++];
                
            case 0x20:
            case 0x21:
            case 0x22:
            case 0x23:
            case 0x24:
            case 0x25:
            case 0x26:
            case 0x27:
            case 0x28:
            case 0x29:
            case 0x2A:
            case 0x2B:
            case 0x2C:
            case 0x2D:
            case 0x2E:
            case 0x2F:
            case 0x30:
            case 0x31:
            case 0x32:
            case 0x33:
            case 0x34:
            case 0x35:
            case 0x36:
            case 0x37:
            case 0x38:
            case 0x39:
            case 0x3A:
            case 0x3B:
            case 0x3C:
            case 0x3D:
            case 0x3E:
            case 0x3F:
                return op - 0x20;
                
            default:
                throw new RuntimeException("Unknown op in fetch: " + op);
                
        }
    }
    
    /**
     * Mount the memory of a program onto the memory of the [email protected] Cpu}.
     * @param is The [email protected] InputStream} to mount the memory from.
     */
    public void mount(InputStream is) throws IOException {
        initialize();
        int available = is.available();
        if(available % 2 != 0)
            throw new IOException();
        int offset = 0;
        while((available -= 2) >= 0) {
            m[offset++].v = is.read() << 8 | is.read();
        }
    }
    
    /**
     * Mount the memory of a program onto the memory of the [email protected] Cpu}.
     * @param memory The short array that represents the memory of the program.
     */
    public void mount(int[] memory) {
        initialize();
        for(int i = 0; i < memory.length; i++) 
            m[i].v = memory[i];
    }
}
Code:
package org.sini;

import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Asm.java
 * @version 1.0.0
 * @author RuneTekk Development (SiniSoul)
 */
public final class Asm {
    
    /**
     * The maximum amount of instructions allowed to compile.
     */
    private static final int MAXIMUM_INSTRUCTIONS = 0x1000;
    
    /**
     * The maximum amount of labels allowed to compile.
     */
    private static final int MAXIMUM_LABELS = 0xFFFF;
    
    /**
     * The pattern to find hexadecimal numbers in the code with.
     */
    private final static Pattern HEXADECIMAL_PATTERN = Pattern.compile("0x([^\\]\n+ ]*)");
    
    /**
     * The pattern to find decimal numbers in the code with.
     */
    private final static Pattern DECIMAL_PATTERN = Pattern.compile("\\d+");
    
    /**
     * The pattern to find instructions in the code with.
     */
    private static Pattern INSTRUCTION_PATTERN = Pattern.compile("[^\n ]+");
    
    /**
     * The pattern to find labels in the code with.
     */
    private static Pattern LABEL_PATTERN = Pattern.compile(":([^\n ]*)");
    
    /**
     * The pattern to find marked labels in the code with.
     */
    private static Pattern MARKER_PATTERN = Pattern.compile("#(\\d*)#");
    
    /**
     * Gets the line number from the index of a character.
     * @param str The string to get the line numbers from.
     * @param maxIndex The maximum index in the string.
     * @return The line number.
     */
    private static int getLineNumber(String str, int maxIndex) {
        int lineNumber = 1;
        int curIndex = -1;
        while((curIndex = str.indexOf('\n', curIndex + 1)) < maxIndex && curIndex != -1) {
            lineNumber++;
        }
        return lineNumber;
    }
    
    /**
     * Looks up an operator in the operators array.
     * @param str The string to lookup.
     * @return The opcode of the operator.
     */
    private static int lookupInstruction(String str) {
        for(int i = 0; i < Ops.OP_NAMES.length; i++)
            if(Ops.OP_NAMES[i] != null && Ops.OP_NAMES[i].toLowerCase().equals(str))
                return i;
        return -1;
    }
    
    /**
     * Looks up an operator in the operators array.
     * @param str The string to lookup.
     * @param The value is represented as a label.
     * @return The opcode of the operator.
     */
    private static int lookupValue(String str, boolean isLabel) {
        Matcher matcher = DECIMAL_PATTERN.matcher(str);
        if(matcher.find()) {
            int value = Integer.parseInt(matcher.group());
            if(str.matches("^" + DECIMAL_PATTERN.pattern() + "$") && value >= 0x0 && value <= 0x1F && !isLabel)
                return value + 0x20;
            else
                str = matcher.replaceAll("%nw%");
        }       
        for(int i = 0; i < Ops.V_NAMES.length; i++)
            if(Ops.V_NAMES[i] != null && Ops.V_NAMES[i].toLowerCase().equals(str))
                return i;
        return -1;
    }
    
    /**
     * Assembles the code into an instruction list.
     * @param is The [email protected] InputStream} to read from.
     * @return The assembled instructions.
     */
    public int[] assemble(InputStream is) throws IOException {
        String str = "";
        int read;
        while((read = is.read()) != -1) {
            str += (char) read;
        }
        return assemble(str);
    }
    
    /**
     * Assembles the code into an instruction list.
     * @param code The code to assemble into instructions.
     * @return The assembled instructions.
     */
    public int[] assemble(String code) {      
        code = code.toLowerCase().replaceAll("[^\\w\\[\\]+\n;: ]", "").replaceAll(";[^\n]*", "");
        Matcher matcher = LABEL_PATTERN.matcher(code);
        int labelOffset = 0;
        while(matcher.find()) {
            if(labelOffset > MAXIMUM_LABELS)
                throw new RuntimeException("Label overflow");
            String name = matcher.group(1);
            if(name.length() < 4)
                throw new RuntimeException("Illegal label, " + '\'' + name + '\'' + ", on line " + getLineNumber(code, matcher.start()));
            code = code.replaceFirst(matcher.group(), ":" + labelOffset);
            if(code.matches(matcher.group() + " "))
                throw new RuntimeException("Duplicate label, " + '\'' + name + '\'' + ", on line " + getLineNumber(code, code.indexOf(matcher.group())));
            Matcher referenceMatcher = Pattern.compile(name + "[\n ]").matcher(code);
            while(referenceMatcher.find()) {
                code = code.substring(0, referenceMatcher.start()) + "#" + labelOffset++ + "#" + code.substring(referenceMatcher.end() - 1);
            }
        }       
        while((matcher = HEXADECIMAL_PATTERN.matcher(code)).find()) {          
            String value = matcher.group(1);
            try {      
                code = code.substring(0, matcher.start()) + Integer.valueOf(value, 16) + code.substring(matcher.end());              
            } catch(Exception ex) {
                throw new RuntimeException("Invalid hexidecimal literal, " + '\'' + matcher.group() + '\'' + ", on line " + getLineNumber(code, matcher.start(1)));
            }
        }
        int[] labels = new int[MAXIMUM_LABELS];
        int counter = 0;
        matcher = INSTRUCTION_PATTERN.matcher(code);
        while(matcher.find()) {
            if(matcher.group().startsWith(":")) {
                int labelId = Integer.parseInt(matcher.group().substring(1));
                labels[labelId] = counter;
                continue;
            }
            int insnOpcode = lookupInstruction(matcher.group());
            if(insnOpcode < 0)
                throw new RuntimeException("Unknown instruction, " + '\'' + matcher.group() + '\'' + ", on line " + getLineNumber(code, matcher.start(0)));
            counter++;
            if(counter >= MAXIMUM_INSTRUCTIONS)
                throw new RuntimeException("Instruction overflow");
            int arguments = (insnOpcode & 0xF) == 0 ? 1 : 2;
            for(int argument = 0; argument < arguments; argument++) {
                if(!matcher.find())
                    throw new RuntimeException("Expected argument " + (argument + 1) + " after " + Ops.OP_NAMES[insnOpcode] + " on line " + getLineNumber(code, code.length()));
                String matcherValue = matcher.group();
                Matcher markerMatcher = MARKER_PATTERN.matcher(matcherValue);
                boolean isLabel = false;
                if((isLabel = markerMatcher.find())) {
                    counter++;
                    continue;
                }
                int valueOpcode = lookupValue(matcherValue, isLabel);
                if(valueOpcode < 0)
                    throw new RuntimeException("Unknown value, " + '\'' + matcher.group() + '\'' + ", on line " + getLineNumber(code, matcher.start(0)));                
                if(Ops.V_NAMES[valueOpcode].indexOf("%nw%") > -1) {
                    Matcher decimalMatcher = DECIMAL_PATTERN.matcher(matcher.group());
                    if(!decimalMatcher.find())
                        throw new RuntimeException("PANIC, Logic Error!");
                    int decimalValue = Integer.parseInt(decimalMatcher.group());
                    if(decimalValue > 65535 || decimalValue < 0)
                        throw new RuntimeException("Undetermined overflow error of uncertainty, " + '\'' + "0x" + Integer.toHexString(decimalValue) + '\'' + ", on line " + getLineNumber(code, decimalMatcher.start(0)));
                    counter++;
                }
            }
        }
        code = code.replaceAll(":[^\n ]+", "");
        int[] instructions = new int[MAXIMUM_INSTRUCTIONS];
        counter = 0;
        matcher = INSTRUCTION_PATTERN.matcher(code);
        while(matcher.find()) {
            int insnOpcode = lookupInstruction(matcher.group());          
            int currentCounter = counter++;
            int arguments = (insnOpcode & 0xF) == 0 ? 1 : 2;
            for(int argument = 0; argument < arguments; argument++) { 
                matcher.find();
                String matcherValue = matcher.group();
                Matcher markerMatcher = MARKER_PATTERN.matcher(matcherValue);
                boolean isLabel = false;
                if((isLabel = markerMatcher.find()))
                    matcherValue = markerMatcher.replaceAll("" + labels[Integer.parseInt(markerMatcher.group(1))]);
                int valueOpcode = lookupValue(matcherValue, isLabel);               
                if(Ops.V_NAMES[valueOpcode].indexOf("%nw%") > -1) {
                    Matcher decimalMatcher = DECIMAL_PATTERN.matcher(matcherValue);
                    decimalMatcher.find();
                    int decimalValue = Integer.parseInt(decimalMatcher.group());                  
                    instructions[counter++] = decimalValue;
                }
                insnOpcode |= (insnOpcode & 0xF) == 0 ? valueOpcode << 10 : valueOpcode << (argument == 0 ? 4 : 10);
            }
            instructions[currentCounter] = insnOpcode;
        }
        int[] programInstructions = new int[counter];
        System.arraycopy(instructions, 0, programInstructions, 0, programInstructions.length);
        return programInstructions;
    }
}


Wunderbar.
  
Old
  (#2 (permalink))
KingHybrid is Offline
<span style="color: purple">Proud Ingame/Sectional Staff</span>
image
 
KingHybrid's Avatar
 
Posts: 9,840
Cookies Given: 0
Cookies Received: 5
Time Online: 73 d 23 h 26 m
Clan: {R.M.L}
   
Default 05-06-2012, 11:25 PM

Dude.. I give coders props lol. I dont even have the slightest clue of what that heck that means or is -.-

Seems like ALOT of work though..


In-Game Mod - August 22 2010 In-Game Admin - January 23 2011
Report Sectional Mod - November 1 2010
  
Old
  (#3 (permalink))
Sinisoul is Offline
Senior Member
image
 
Sinisoul's Avatar
 
Posts: 230
Cookies Given: 1
Cookies Received: 16
Time Online: 9 d 15 h 40 m
   
Default 05-06-2012, 11:31 PM

Quote:
Originally Posted by KingHybrid View Post
Dude.. I give coders props lol. I dont even have the slightest clue of what that heck that means or is -.-

Seems like ALOT of work though..
Its a virtual computer I wrote in java along with a language compiler!!~!~@! If you hook it up to it's monitor.java you can make games like pong and stuff : > : > :> :>


Wunderbar.

Last edited by Sinisoul; 05-06-2012 at 11:33 PM.
  
Old
  (#4 (permalink))
Brittany is Offline
SG
image
 
Brittany's Avatar
 
Posts: 24,709
Cookies Given: 611
Cookies Received: 3,789
Time Online: 203 d 20 h 29 m
   
Default 05-06-2012, 11:40 PM

Quote:
Originally Posted by Sinisoul View Post
Its a virtual computer I wrote in java along with a language compiler!!~!~@! If you hook it up to it's monitor.java you can make games like pong and stuff : > : > :> :>
Hey you're finally learning English



Sometimes we just need a friend.
Someone to hold us when you can't stand.
Sometimes even to pretend like you matter for that split second that everything seems to be crashing down.
And sometimes it's just easier to say goodbye.

  
Old
  (#5 (permalink))
Sinisoul is Offline
Senior Member
image
 
Sinisoul's Avatar
 
Posts: 230
Cookies Given: 1
Cookies Received: 16
Time Online: 9 d 15 h 40 m
   
Default 05-06-2012, 11:42 PM

Quote:
Originally Posted by Mommy View Post


Hey you're finally learning English
I'm still a bit rusty.


Wunderbar.

Last edited by Sinisoul; 05-06-2012 at 11:44 PM.
  
adsense code2.3
Closed Thread

Bookmarks

jDCPU-16

Programming




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
adsense2.6
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 06:38 PM.





Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2017, vBulletin Solutions, Inc.