C++ 除算器

このエントリーをはてなブックマークに追加このエントリのはてなブックマーク数このエントリをLivedoor Clipに追加このエントリをYahoo!ブックマークに追加このエントリをFC2ブックマークに追加このエントリをNifty Clipに追加このエントリをつぶやく

富豪回路

// div_u8.v
module div_u8 (
	n, d, q, r
);
	input [7:0] n, d;
	output [7:0] q, r;

	assign q = n / d;
	assign r = n % d;
endmodule

// div_8.sflp
declare div_u8 interface
{
	input n<8>, d<8>;
	output q<8>, r<8>;
}

circuit div_8
{
	input n<8>, d<8>;
	output q<8>, r<8>;
	instrin u(n, d), s(n, d);

	div_u8 div;

	instruct u par{ // unsigned div
		div.n = n;
		div.d = d;
		q = div.q;
		r = div.r;
	}

	instruct s par{ // signed div
		if(n<7>) div.n = -n;
		else div.n = n;
		if(d<7>) div.d = -d;
		else div.d = d;
		if(n<7>@d<7>) q = -div.q;
		else q = div.q;
		if(n<7>) r = -div.r;
		else r = div.r;
	}

}

// div_test.cpp
#include<stdio.h>
#include "Vdiv_8.h"

int main(void)
{
	Vdiv_8 *div = new Vdiv_8;

	int n, d;
	unsigned char q, r;

	for (n = 0; n < 256; n++){
		for (d = 1; d < 256; d++){

			div->n = n;
			div->d = d;

			div->u = 1;
			div->s = 0;
			div->eval();
			printf("H %02X / %02X = %02X . %02X\n", n, d, div->q, div->r);

			q = (unsigned char)n / (unsigned char)d;
			r = (unsigned char)n % (unsigned char)d;
			printf("U %02X / %02X = %02X . %02X\n", n, d, q, r);

			if (div->q != q || div->r != r){
				printf("err\n");
				getchar();
			}

			div->u = 0;
			div->s = 1;
			div->eval();
			printf("H %02X / %02X = %02X . %02X\n", n, d, div->q, div->r);

			q = (signed char)n / (signed char)d;
			r = (signed char)n % (signed char)d;
			printf("S %02X / %02X = %02X . %02X\n", n, d, q, r);

			if (div->q != q || div->r != r){
				printf("err\n");
				getchar();
			}

			printf("\n");
		}
	}

	div->final();
	delete div;

	return 0;
}

Posted by at 2016/3/9 22:03

よく投稿されているコード

タグ

最近投稿されたコード