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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
`timescale 1ns / 1ps

module brainfuck_cpu(
		input 			reset,
		input 			clk,
		
		output [7:0] 	address_bus,
		inout  [7:0] 	data_bus,
		output			output_en,
		
		output 			code_mem_en,
		output 			data_mem_en,
		output 			io_mem_en
    );

	// Registers
	reg [7:0] rip;
	reg [7:0] rdp;
	reg [7:0] rnl;
	
	reg [7:0] icache;
	reg [7:0] dcache;
	
	// Instruction decoding
	wire i_inc_dp, i_dec_dp, i_inc_data, i_dec_data;
	wire i_out, i_in;
	wire i_forward, i_backward;
	
	assign i_inc_dp   = (icache == 8'h3e); // >
	assign i_dec_dp   = (icache == 8'h3c); // <
	assign i_inc_data = (icache == 8'h2b); // +
	assign i_dec_data = (icache == 8'h2d); // -
	assign i_out      = (icache == 8'h2e); // .
	assign i_in       = (icache == 8'h2c); // ,
	assign i_forward  = (icache == 8'h5b); // [
	assign i_backward = (icache == 8'h5d); // ]
	
	// Bus access
	reg [3:0] access_latch;
	
	parameter ACCESS_NONE  = 4'b0000;
	parameter ACCESS_CODE  = 4'b0100;
	parameter ACCESS_DATA  = 4'b0010;
	parameter ACCESS_IO    = 4'b0001;
	
	parameter ACCESS_WRITE = 4'b1000;
	parameter ACCESS_READ  = 4'b0000;
	
	assign { output_en, code_mem_en, data_mem_en, io_mem_en } = access_latch;
	
	reg  [7:0] address_latch;
	assign     address_bus   = (access_latch[2:0]) ? address_latch : 8'b0;
	
	wire [7:0] data_in       = data_bus;
	assign     data_bus      = (output_en) ? dcache : 1'bz;
	
	// FSM definitions
	reg  [2:0] state;
	
	parameter FETCH    = 0;
	parameter DECODE   = 1; 
	parameter EXECUTE1 = 2;
	parameter EXECUTE2 = 3;
	parameter EXECUTE3 = 4;
	parameter SEARCH1  = 5;
	parameter SEARCH2  = 6;
	parameter SEARCH3  = 7;
	parameter SEARCH4  = 8;
	
	// Finite state machine
	always @ (posedge clk) begin
		if (reset == 1'b1) begin
			rip   <= 8'b0;
			rdp   <= 8'b0;
			rnl   <= 8'b0;
			state <= FETCH;
		end else begin
			case (state)
				FETCH: begin
					access_latch  <= ACCESS_CODE | ACCESS_READ;
					address_latch <= rip;
					
					state <= DECODE;
				end
				
				DECODE: begin
					icache        <= data_in;
					rip           <= rip + 1;
					
					state <= EXECUTE1;
				end
				
				EXECUTE1: begin
					if (i_inc_dp || i_dec_dp) begin
						if (i_inc_dp) rdp <= rdp + 1;
						if (i_dec_dp) rdp <= rdp - 1;
						
						access_latch  <= ACCESS_NONE;
						
						state <= FETCH;
					end else if (i_inc_data || i_dec_data || i_out || i_forward || i_backward) begin
						access_latch  <= ACCESS_DATA | ACCESS_READ;
						address_latch <= rdp;
						
						state <= EXECUTE2;
					end else if (i_in) begin
						access_latch  <= ACCESS_IO | ACCESS_READ;
						address_latch <= 8'b0;

						state <= EXECUTE2;
					end
				end
				
				EXECUTE2: begin
					dcache <= data_in;
					
					state  <= EXECUTE3;
				end
				
				EXECUTE3: begin
					if (i_inc_data || i_dec_data) begin
						if(i_inc_data) dcache <= dcache + 1;
						if(i_dec_data) dcache <= dcache - 1;
						
						access_latch  <= ACCESS_DATA | ACCESS_WRITE;
						// address_latch still contains the address from EXECUTE1,
						// and dcache contains the updated data
						
						state <= FETCH;
					end else if (i_out) begin
						access_latch  <= ACCESS_IO | ACCESS_WRITE;
						address_latch <= 8'b0;
						
						state <= FETCH;
					end else if (i_in) begin
						access_latch  <= ACCESS_DATA | ACCESS_WRITE;
						address_latch <= rdp;
						// dcache containst the requested data
						
						state <= FETCH;
					end else if (i_forward || i_backward) begin
						if ((dcache == 8'b0 && i_forward) ||
							 (dcache != 8'b0 && i_backward)) begin
							state <= SEARCH1;
						end else begin
							state <= FETCH;
						end
					end
				end
				
				SEARCH1: begin
					access_latch  <= ACCESS_CODE | ACCESS_READ;
					address_latch <= rip;
					
					state <= SEARCH2;
				end
				
				SEARCH2: begin
					dcache <= data_in;
					
					state <= SEARCH3;
				end
				
				SEARCH3: begin
					if (i_forward)  rip <= rip + 1;
					
					if (dcache == 8'h5b) begin // [
						if (i_forward) begin
							rnl   <= rnl + 1;
							
							state <= SEARCH1;
						end
						if (i_backward) begin
							// rip is at the [
							if (rnl == 0) begin
								rip   <= rip + 1;
								
								state <= SEARCH4;
							end else begin
								rnl   <= rnl - 1;
								rip   <= rip - 1;
								
								state <= SEARCH1;
							end
						end
					end
					
					if (dcache == 8'h5d) begin // ]
						if (i_forward) begin
							if (rnl == 0) begin
								// rip is already on the cell next to ]
								state <= FETCH;
							end else begin
								rnl   <= rnl - 1;
								
								state <= SEARCH1;
							end
						end
						if (i_backward) begin
							rnl   <= rnl + 1;
							
							state <= SEARCH1;
						end
					end
				end
			endcase
		end
	end
endmodule