예제 #1
0
파일: asm_test.go 프로젝트: bieber/drones
func (s *suite) TestOpcodes(c *C) {
	code := ""
	opcodes := make([]uint16, 0, len(vm.OpcodeNames()))
	for opcode, name := range vm.OpcodeNames() {
		code = code + name + "\n"
		opcodes = append(opcodes, uint16(opcode))
	}
	binary, err := Assemble(bufio.NewReader(strings.NewReader(code)))

	c.Assert(err, IsNil)
	c.Assert(binary, NotNil)
	c.Assert(len(binary)/2, Equals, len(vm.OpcodeNames()))

	i := 0
	for _, opcode := range opcodes {
		c.Assert(binary[i], Equals, opcode)
		i += 2
	}
}
예제 #2
0
파일: asm.go 프로젝트: bieber/drones
func buildCode() []uint16 {
	if errorCodes != nil {
		return nil
	}

	opcodeNames := vm.OpcodeNames()
	opcodes := make(map[string]uint16, len(opcodeNames))
	for k, v := range opcodeNames {
		opcodes[v] = uint16(k)
	}

	mem := make([]uint16, maxAddress)

	for _, op := range ops {
		code, ok := opcodes[op.op]
		if !ok {
			errorCodes = append(
				errorCodes,
				fmt.Sprintf("Line %d: Invalid opcode %s", op.line, op.op),
			)
			return nil
		}

		var arg uint16
		if op.label == "" {
			arg = op.num
		} else {
			label, ok := labels[op.label]
			if !ok {
				errorCodes = append(
					errorCodes,
					fmt.Sprintf("Line %d: Invalid label %s", op.line, op.label),
				)
				return nil
			}
			arg = label.address
		}
		mem[op.address] = code
		mem[op.address+1] = arg
	}

	for _, w := range words {
		for k, v := range w.words {
			mem[int(w.address)+k] = v
		}
	}

	return mem
}