コード例 #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
}