0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

概要

micropythonでshrike-lite、やってみた。
verilogでAVRを書く。
javascriptでavrのアセンブラ書いてみた。
rom化に使用する。

写真

image.png

サンプルコード



function opcode4(a) {
	var res;
  switch (a) 
  {
	case "0000":
		res = "0";
	break;
	case "0001":
		res = "1";
	break;
	case "0010":
		res = "2";
	break;
	case "0011":
		res = "3";
	break;
	case "0100":
		res = "4";
	break;
	case "0101":
		res = "5";
	break;
	case "0110":
		res = "6";
	break;
	case "0111":
		res = "7";
	break;
	case "1000":
		res = "8";
	break;
	case "1001":
		res = "9";
	break;
	case "1010":
		res = "A";
	break;
	case "1011":
		res = "B";
	break;
	case "1100":
		res = "C";
	break;
	case "1101":
		res = "D";
	break;
	case "1110":
		res = "E";
	break;
	case "1111":
		res = "F";
	break;
	}
  return res;
};
function opcode(s) {
  var res;
  res = opcode4(s.substring(0, 4)) + opcode4(s.substring(4, 8)) + opcode4(s.substring(8, 12)) + opcode4(s.substring(12, 16));  
  return res + "\n";
}
function ado12(i) {
	var d;
	if (i < 0)
	{
		d = Math.abs(i);
		d = ~d + 1 >>> 0;
	}
	else
	{
		d = i >>> 0;
	}
	var b = d.toString(2);
  return ('000000000000' + b).slice(-12);
}
function ado7(i) {
	var d;
	if (i < 0)
	{
		d = Math.abs(i);
		d = ~d + 1 >>> 0;
	}
	else
	{
		d = i >>> 0;
	}
	var b = d.toString(2);
  return ('000000000000' + b).slice(-7);
}
function reg5(i) {
	var b = i.toString(2);
  return ('00000' + b).slice(-5);
}
function reg5l(i) {
	var d = i & 0xf;
	var b = d.toString(2);
  return ('0000' + b).slice(-5);
}
function reg5h(i) {
	var d = i >> 4;
	var b = d.toString(2);
  return ('00' + b).slice(-1);
}
function s3(i) {
	if (i == 0)
		return '000';
	var b = i.toString(2);
  return ('000' + b).slice(-3);
}
function reg4(i) {
	var b = i.toString(2);
  return ('00000' + b).slice(-4);
}
function imm6l(i) {
	var d = i & 0xf;
	var b = d.toString(2);
  return ('0000' + b).slice(-4);
}
function imm6h(i) {
	var d = i >> 4;
	var b = d.toString(2);
  return ('00' + b).slice(-2);
}
function imm8l(i) {
	var d = i & 0xf;
	var b = d.toString(2);
  return ('0000' + b).slice(-4);
}
function imm8h(i) {
	var d = i >> 4;
	var b = d.toString(2);
  return ('0000' + b).slice(-4);
}
var asm = {
 	PC: 0,
	add: function(r0, r1) {
		out.value += opcode("000011" + reg5h(r1) + reg5(r0) + reg5l(r1));
		this.PC++;
	},
	sub: function(r0, r1) {
		out.value += opcode("000110" + reg5h(r1) + reg5(r0) + reg5l(r1));
		this.PC++;
	},
	and: function(r0, r1) {
		out.value += opcode("001000" + reg5h(r1) + reg5(r0) + reg5l(r1));
		this.PC++;
	},
	or: function(r0, r1) {
		out.value += opcode("001010" + reg5h(r1) + reg5(r0) + reg5l(r1));
		this.PC++;
	},
	eor: function(r0, r1) {
		out.value += opcode("001001" + reg5h(r1) + reg5(r0) + reg5l(r1));
		this.PC++;
	},
	mul: function(r0, r1) {
		out.value += opcode("100111" + reg5h(r1) + reg5(r0) + reg5l(r1));
		this.PC++;
	},
	mov: function(r0, r1) {
		out.value += opcode("001011" + reg5h(r1) + reg5(r0) + reg5l(r1));
		this.PC++;
	},
	lsl: function(r0, r1) {
		out.value += opcode("000011" + reg5h(r1) + reg5(r0) + reg5l(r1));
		this.PC++;
	},
	cp: function(r0, r1) {
		out.value += opcode("000101" + reg5h(r1) + reg5(r0) + reg5l(r1));
		this.PC++;
	},
	inc: function(r) {
		out.value += opcode("1001010" + reg5(r) + "0011");
		this.PC++;
	},
	dec: function(r) {
		out.value += opcode("1001010" + reg5(r) + "1010");
		this.PC++;
	},
	lsr: function(r) {
		out.value += opcode("1001010" + reg5(r) + "0110");
		this.PC++;
	},
	cpi: function(r, i) {
		out.value += opcode("0011" + imm8h(i) + reg4(r) + imm8l(i));
		this.PC++;
	},
	ldi: function(r, i) {
		out.value += opcode("1110" + imm8h(i) + reg4(r) + imm8l(i));
		this.PC++;
	},
	in: function(io, r) {
		out.value += opcode("10110" + imm6h(io) + reg5(r) + imm6l(io));
		this.PC++;
	},
	out: function(io, r) {
		out.value += opcode("10111" + imm6h(io) + reg5(r) + imm6l(io));
		this.PC++;
	},
	breq: function(i) {
		out.value += opcode("111100" + ado7(i) + "001");
		this.PC++;
  },
	brne: function(i) {
		out.value += opcode("111101" + ado7(i) + "001");
		this.PC++;
  },
	brge: function(i) {
		out.value += opcode("111100" + ado7(i) + "100");
		this.PC++;
  },
	brlt: function(i) {
		out.value += opcode("111101" + ado7(i) + "100");
		this.PC++;
  },
	rcall: function(i) {
		out.value += opcode("1101" + ado12(i));
		this.PC++;
  },
	rjmp: function(i) {
		out.value += opcode("1100" + ado12(i));
		this.PC++;
  },
	ret: function() {
		out.value += opcode("1001010100001000");
		this.PC++;
  },
	reti: function() {
		out.value += opcode("1001010100011000");
		this.PC++;
  },
	nop: function() {
		out.value += opcode("0000000000000000");
		this.PC++;
  },
	cbi: function(a, b) {
		out.value += opcode("10011000" + reg5l(a) + s3(b));
		this.PC++;
  },
	sbi: function(a, b) {
		out.value += opcode("10011010" + reg5l(a) + s3(b));
		this.PC++;
  },
	sbrc: function(a, b) {
		out.value += opcode("1111110" + reg5l(a) + "0" + s3(b));
		this.PC++;
  },
	sbrs: function(a, b) {
		out.value += opcode("1111111" + reg5l(a) + "0" + s3(b));
		this.PC++;
  }
}




成果物

投入したソースコード

      asm.sbi(1, 0)
      asm.ldi(16, 85)
      asm.ldi(17, 8)
      asm.cbi(2, 0)
      asm.ldi(18, 104)
      asm.dec(18)
      asm.brne(-2)
      asm.sbrc(16, 0)
      asm.sbi(2, 0)
      asm.ldi(18, 104)
      asm.dec(18)
      asm.brne(-2)
      asm.sbrs(16, 0)
      asm.cbi(2, 0)
      asm.ldi(18, 104)
      asm.dec(18)
      asm.brne(-2)
      asm.lsr(16)
      asm.dec(17)
      asm.brne(-10) 
      asm.sbi(2, 0)
      asm.ldi(18, 104)
      asm.dec(18)
      asm.brne(-2)
      asm.rjmp(-24)

アセンブル結果

9A08
E505
E018
9810
E628
952A
F7F1
FC00
9A10
E628
952A
F7F1
FE00
9810
E628
952A
F7F1
9506
951A
F7B1
9A10
E628
952A
F7F1
CFE8

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?