import random

from pymtl import *
from pclib.test import run_test_vector_sim
from RegFile import RegFile

static_test_vector = [
		('rX_address  rX* rY_address  rY* rZ_address  rZ clk_en'),
		[       0x0, 0x0,       0x0, 0x0,       0x0, 0x0, 1],
		[       0x0, 0x0,       0x0, 0x0,       0x0, 0xF, 1],
		[       0x0, 0x0,       0x0, 0x0,       0x1, 0xF, 1],
		[       0x1, 0xF,       0x0, 0x0,       0x2, 0xE, 1],
		[       0x0, 0x0,       0x2, 0xE,       0x3, 0xD, 1],
		[       0x3, 0xD,       0x0, 0x0,       0x4, 0xC, 1],
		[       0x0, 0x0,       0x4, 0xC,       0x5, 0xB, 1],
		[       0x5, 0xB,       0x0, 0x0,       0x6, 0xA, 1],
		[       0x0, 0x0,       0x6, 0xA,       0x7, 0x9, 1],
		[       0x7, 0x9,       0x0, 0x0,       0x8, 0x8, 1],
		[       0x0, 0x0,       0x8, 0x8,       0x9, 0x7, 1],
		[       0x9, 0x7,       0x0, 0x0,       0xA, 0x6, 1],
		[       0x0, 0x0,       0xA, 0x6,       0xB, 0x5, 1],
		[       0xB, 0x5,       0x0, 0x0,       0xC, 0x4, 1],
		[       0x0, 0x0,       0xC, 0x4,       0xD, 0x3, 1],
		[       0xD, 0x3,       0x0, 0x0,       0xE, 0x2, 1],
		[       0x0, 0x0,       0xE, 0x2,       0xF, 0x1, 1],
		[       0xF, 0x1,       0x0, 0x0,       0x0, 0x0, 1],
	]

def gen_data( width, length ):
	upper = 2**(width-1)-1
	lower = -2**(width-1)-1
	test_data = [None] * length
	for i in xrange( length ):
		if i != 0:
			test_data[i] = Bits(width, random.randint(lower,upper))
		else:
			test_data[i] = Bits(width, 0);
	return test_data


def gen_vector_table( data ):
	test_vector_table = [('rX_address rX* rY_address rY* rZ_address rZ clk_en')]
	for idx, val in enumerate(data):
		test_vector_table.append( [ 0, 0, 0, 0, idx, val , 1] )
		test_vector_table.append( [ idx, val, 0, 0, 0, 0 , 1] )		
	return test_vector_table


def test_rw( dump_vcd ):
	run_test_vector_sim( RegFile(dtype=16,nregs=16), static_test_vector, dump_vcd)

def test_rw_sequental( dump_vcd ):
	data = gen_data(16,8)
	test_vector_table = gen_vector_table(data)
	run_test_vector_sim( RegFile(dtype=Bits(16),nregs=8), test_vector_table, dump_vcd )		
