aboutsummaryrefslogtreecommitdiff
path: root/rtl/src/verilog/demo_adder.v
blob: a86f1143fb0c05ac198988e0fbce741a9e8545de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
`timescale 1ns / 1ps

module demo_adder
	(
		clk, rst,
		x, y, z,
		ctl, sts
	);
	
		//
		// Ports
		//
	input		wire				clk;	// clock
	input		wire				rst;	// reset
	
	input		wire	[31: 0]	x;		// x
	input		wire	[31: 0]	y;		// y
	output	wire	[31: 0]	z;		// z = x + y
	
	input		wire	[15: 0]	ctl;	// control
	output	wire	[15: 0]	sts;	// status
	
	
		//
		// Internal Registers
		//
	reg	[31: 0]	z_reg		= {32{1'b0}};
	reg	[15: 0]	sts_reg	= {16{1'b0}};
	reg	[15: 0]	ctl_dly	= {16{1'b0}};
	
	assign z		= z_reg;
	assign sts	= sts_reg;


		//
		// Control Logic
		//
	always @(posedge clk)
		//
		if (rst)	ctl_dly	<= {16{1'b0}};
		else		ctl_dly	<= ctl;
		
		/* This flag is set whenever different value is written to control register. */
		
	wire	adder_go = (ctl != ctl_dly) ? 1'b1 : 1'b0;
	
	
		//
		// Adder Logic
		//
	always @(posedge clk)
		//
		if (rst)					z_reg	<= {32{1'b0}};
		else if (adder_go)	z_reg	<= x + y;
	

		//
		// Status Logic
		//
	always @(posedge clk)
		//
		if (rst)					sts_reg	<= {16{1'b0}};
		else if (adder_go)	sts_reg	<= ctl;

	
endmodule