/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tools.mips;

import java.util.ArrayList;
import java.util.Map;

/**
 *
 * @author EUGENIO CARVALHO
 */
class RegisterBank {

    long[] registers;

    public final long REG_HI;
    public final long REG_LO;
    public long last;
    public ArrayList<String> names = new ArrayList<String>();

    public RegisterBank(long i) {

        registers = new long[(int) i + 2];
        REG_HI = i;
        REG_LO = i + 1;

        for (Map.Entry<String, String> en : target.mips.Mips.registers.entrySet()) {
            names.add(en.getKey());
        }
        names.add("hi");
        names.add("lo");
    }

    public long R(Integer i) {
        return registers[i];
    }

    public long R(long i) {
        return registers[(int) i];
    }

    public RegisterBank W(long i, long v) {
        registers[(int) i] = v;
        last = i;
        return this;
    }

    @Override
    public String toString() {

        String out = "", t;

        for (int i = 0; i < registers.length; i++) {
            if (last == i) {
                t = "-> ";
            } else {
                t = "   ";
            }
            out = out.concat(t)
                    .concat(names.get(i) + "\t:" + registers[i])
                    .concat(",\n");
        }

        return out;
    }

}
