Ejemplo n.º 1
0
func New(assemblyFileName string, config *config.Config) (*Processor, error) {

	p := &Processor{
		&processor{
			done:                  false,
			instructionsFetched:   []string{},
			instructionsCompleted: []uint32{},
			dataLog:               map[uint32][]LogEvent{},

			branchHistoryTable:    map[uint32]uint32{},
			conditionalBranches:   0,
			unconditionalBranches: 0,
			mispredictedBranches:  0,
			noTakenBranches:       0,
			branchPredictorBits:   0,
			speculativeJumps:      0,

			instructionsMap: map[uint32]string{},
			instructionsSet: set.Init(),
			config:          config,

			programCounter:    0,
			registerMemory:    memory.New(config.RegistersMemorySize()),
			instructionMemory: memory.New(config.InstructionsMemorySize()),
			dataMemory:        memory.New(config.DataMemorySize()),
		},
	}

	logger.Print(config.ToString())

	// Instanciate functional units
	instructionsFinished := func() bool {
		return p.processor.done && p.InstructionsFetchedCounter() == p.InstructionsCompletedCounter() && p.SpeculativeJumps() == 0
	}
	p.processor.clockUnit = clock.New(config.CyclePeriod(), instructionsFinished)

	err := p.loadInstructionsMemory(assemblyFileName)
	if err != nil {
		return p, err
	}
	return p, nil
}
Ejemplo n.º 2
0
func TranslateFromFile(filename string, outputFilename string) (string, error) {

	// Read lines from file
	logger.Print(" => Reading assembly file: %s", filename)
	lines, err := utils.ReadLines(filename)
	if err != nil {
		return "", err
	}

	// Create output file
	f, err := os.Create(outputFilename)
	if err != nil {
		return "", err
	}
	defer f.Close()

	// Clean lines, remove labels and get map of labels
	memory, lines, labels := getLinesAndMapLabels(lines)

	// Print pre-filled memory macros
	for _, line := range memory {
		f.WriteString(fmt.Sprintf("%s\n", line))
	}

	// Translate instructions
	instructionSet := set.Init()
	for i, line := range lines {
		instruction, err := instructionSet.GetInstructionFromString(line, uint32(i)*consts.BYTES_PER_WORD, labels)
		if err != nil {
			return "", errors.New(fmt.Sprintf("Failed translating line %d: %s. %s", i, line, err.Error()))
		}
		hex := fmt.Sprintf("%08X", instruction.ToUint32())
		f.WriteString(fmt.Sprintf("%s // 0x%04X => %s\n", hex, i*consts.BYTES_PER_WORD, strings.Replace(line, "\t", " ", -1)))
	}
	logger.Print(" => Output hex file: %s", outputFilename)
	return outputFilename, nil
}