Fun and Games with CRV: Sudoku

This week let's mix it up a bit and do something less work-related. Everybody probably knows what Sudoku is, but just in case you don't here's a link to the Wikipedia page.


This is a companion discussion topic for the original entry at https://verificationgentleman.netlify.app/2014/05/10/fun-and-games-with-crv-sudoku.html

Hi,
Is the code you posted compiling? I am getting constraint inconsistent errors.

$unit, “foreach (this.grid[ i])unique {this.grid[i]};”
The number of indices in foreach is 1. It is either less than the number of
unpacked dimensions, which is 2, or greater than the total number of (both
packed and unpacked) dimensions, which is 2, of the argument grid
Make sure that the number of indices is valid

Seems like your tool doesn’t want to loop over a single dimension of grid.

I have decided to read all of Tudor’s posts to find out gaps in SystemVerilog or UVM knowledge. And wanna say that in 2022 the unique in VCS P-2019.06 SP2 -3 works

class sudoku;

	rand byte unsigned matrix[9][9];
	rand byte unsigned trans_matrix[9][9];
	rand byte unsigned sub_matrix[3][3][3][3];


	constraint trans_matrix_cnstr {
		foreach(matrix[i,j]){
			trans_matrix[i][j] == matrix[j][i];
		}
	}

	constraint sub_matrix_cnstr {
		foreach(matrix[i,j]){
			sub_matrix[i/3][j/3][i%3][j%3] == matrix[i][j];

		}
	}

	constraint all_element_inside_1_to_9_cnstr{
		foreach(matrix[i,j]){
			matrix[i][j] inside {[1:9]};
		}
	}

	constraint unique_in_row_cnstr{
		foreach(matrix[i]){
			unique{matrix[i]};
		}
	}

	constraint unique_in_column_cnstr{
		foreach(trans_matrix[i]){
			unique{trans_matrix[i]};
		}
	}

	constraint sub_grid_unique_cnstr{
		foreach(sub_matrix[a,b,i,j]){
			unique{sub_matrix[a][b]};
		}
	}

	function string get_string();
		string tmp;
		foreach(matrix[i,]) begin
			string row;
			foreach(matrix[,j]) begin
				row = {row,$sformatf("%0d ",matrix[i][j])};
			end
			tmp = {tmp,row,"\n"};
		end
		return tmp;
	endfunction


	function void print();
		$write("%0s",get_string());
	endfunction

endclass


class sudoku_init extends sudoku;

	constraint init_cnstr {
		matrix[0][0] == 5;
	    matrix[0][1] == 3;
	    matrix[0][4] == 7;

	    matrix[1][0] == 6;
	    matrix[1][3] == 1;
	    matrix[1][4] == 9;
	    matrix[1][5] == 5;

    	matrix[2][1] == 9;
    	matrix[2][2] == 8;
    	matrix[2][7] == 6;

    	matrix[3][0] == 8;
    	matrix[3][4] == 6;
    	matrix[3][8] == 3;

    	matrix[4][0] == 4;
    	matrix[4][3] == 8;
    	matrix[4][5] == 3;
    	matrix[4][8] == 1;

    	matrix[5][0] == 7;
    	matrix[5][4] == 2;
    	matrix[5][8] == 6;

    	matrix[6][1] == 6;
    	matrix[6][6] == 2;
    	matrix[6][7] == 8;

    	matrix[7][3] == 4;
    	matrix[7][4] == 1;
    	matrix[7][5] == 9;
    	matrix[7][8] == 5;

    	matrix[8][4] == 8;
    	matrix[8][7] == 7;
    	matrix[8][8] == 9;
	}

endclass


program top;

	sudoku_init item;

	initial begin
		item = new();
		if(!item.randomize()) begin
			$display("Fatal randomize failed");
		end
		else begin
			item.print();
		end

	end


endprogram

result

5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 3 4 8
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 5 3 7 9 1
7 1 3 9 2 4 8 5 6
9 6 1 5 3 7 2 8 4
2 8 7 4 1 9 6 3 5
3 4 5 2 8 6 1 7 9

1 Like