module testb ; // stimulus or testbench

// Declare variables to manipulate inputs to module that will be tested
// Must be of type register
// control : controls the counting secuence 
// clk 	   : clock
// value   : counter count

reg control, clk;
wire [2:0] value; 

initial
   begin
      // create a dump of all variables to use gtkwave to view
      $dumpfile("ccounter.vcd");
      $dumpvars ;

      // print a message on the screen each time a variable changes
      $monitor("Time = %g control = %b count = %b", $time, control, value);

      // now describe the simulation
      control = 1'b0 ; 
      clk = 1'b0 ;      
      #60 control = 1'b1;
      #60 control = 1'b0;
      #60 $finish;
   end // initial begin

// create clock
always
   #5 clk = ~clk;

// instantiate module under test
ccounter mycount(clk, control, value);
endmodule // testb

/////////////////////////////////////////////////////////////
// module ccounter - custom counter
// If control = 0, counts 0,1,4,5,7
// if control = 1, counts 6,5,3,2,1
// Goes to 5 if it is at a count that isn't part of secuence
/////////////////////////////////////////////////////////////
module ccounter(clock, control, count);
   input clock ;
   input control ;
   output [2:0] count;
   reg [2:0] count = 3'b000;
  
  
always @ (posedge clock)
   if (control)
      case (count)
	 3'h0: count = 3'h1;
	 3'h1: count = 3'h4;
	 3'h4: count = 3'h5;
	 3'h5: count = 3'h7;
	 3'h7: count = 3'h0;
	 default:
	    count = 3'h5;
      endcase // case (count)
   else  // counting down
      case (count)
	 3'h1: count = 3'h6;
	 3'h2: count = 3'h1;
	 3'h3: count = 3'h2;
	 3'h5: count = 3'h3;
	 3'h6: count = 3'h5;
	 default:
	    count = 3'h5;
      endcase // case (count)
   
endmodule // ccounter

		  
		  