File size: 6,514 Bytes
2166d7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# FlashAttention RTL - Simulation Setup & Run Guide

## Overview

This guide helps you set up and run the Cocotb testbench for the FlashAttention RTL engine.

**Simulator Stack:**
- **Verilator**: Open-source Verilog/SystemVerilog simulator (C++ based, Windows-compatible)
- **Cocotb**: Coroutine-based testbench framework (Python)
- **Python 3.8+**: Scripting language for testbench

**Target Module:** 8×8 Systolic Array for matrix multiplication (Q4.4 fixed-point)

---

## Prerequisites

### Windows System Requirements

Before proceeding, ensure you have:

1. **Python 3.8 or later**
   ```
   python --version  # Should show 3.8+
   ```

2. **Git Bash or WSL2** (for Unix-like commands on Windows)
   - Recommended: Git Bash (lightweight)
   - Alternative: Windows Subsystem for Linux 2

3. **C++ Compiler** (required by Verilator)
   - Option A: **Microsoft Visual C++ (MSVC)**
     - Install Visual Studio Community (free)
     - Select "Desktop Development with C++"
   - Option B: **MinGW-w64** (lightweight alternative)
     - Download from: https://www.mingw-w64.org/
     - Add `bin/` to Windows PATH

4. **Verilator 5.0+** (Verilog/SystemVerilog simulator)
   ```
   # Download Windows binary or build from source
   # https://www.veripool.org/verilator/
   verilator --version
   ```

---

## Step 1: Install Verilator (Windows)

### Option A: Using Pre-built Binary (Easiest)

1. Download Verilator Windows binary from: https://www.veripool.org/verilator/
2. Extract to `C:\Program Files\verilator\` (or custom location)
3. Add to Windows PATH:
   ```
   setx PATH "%PATH%;C:\Program Files\verilator\bin"
   ```
4. Verify:
   ```
   verilator --version
   ```

### Option B: Build from Source

```bash
# Clone Verilator
git clone https://github.com/verilated/verilator

cd verilator
git checkout v5.012  # Latest stable

# Configure and build
autoconf
./configure --prefix=C:\Program Files\verilator
make
make install

# Add to PATH
setx PATH "%PATH%;C:\Program Files\verilator\bin"
```

---

## Step 2: Set Up Python Environment

Navigate to project directory:

```bash
cd d:\goml\flashattentionv1
```

Create virtual environment:

```bash
# Create venv
python -m venv venv

# Activate (Windows Command Prompt)
venv\Scripts\activate

# OR activate (PowerShell)
venv\Scripts\Activate.ps1
```

Install dependencies:

```bash
pip install cocotb cocotb-test pytest numpy
```

Verify installation:

```bash
python -c "import cocotb; print(f'Cocotb {cocotb.__version__} installed')"
```

---

## Step 3: Verify RTL Files

Check that all RTL source files are present:

```bash
# From project root
dir rtl\src\*.sv
# Should show:
#   processing_element.sv
#   systolic_array.sv
```

Optionally, check SystemVerilog syntax:

```bash
cd sim
verilator --lint-only ..\rtl\src\processing_element.sv ..\rtl\src\systolic_array.sv --top-module systolic_array
```

---

## Step 4: Run Testbench

### Quick Test (Syntax Check Only)

```bash
cd sim
make check_syntax
```

Expected output:
```
Checking SystemVerilog syntax with Verilator...
(no errors = success)
```

### Full Simulation with Cocotb

**Important:** Cocotb testbenches must be run from `sim/` directory.

```bash
cd sim

# Run all tests
python -m cocotb.simulator --simulator verilator \
    --testbench test_systolic_array \
    --toplevel systolic_array \
    --verilog_sources ../rtl/src/processing_element.sv \
    --verilog_sources ../rtl/src/systolic_array.sv
```

Or use the Makefile:

```bash
cd sim
make sim
```

---

## Expected Output

When simulation runs successfully, you should see:

```
===========================
TEST: Systolic Array Basic Functionality
===========================
Starting 10ns clock...
Asserting reset...

Test 1: Small values (easy validation)
...
RTL output:
[[result matrix]]

Golden C: ...
RTL: ...
Result C[0,0]: golden=X, rtl=Y

✓ TEST PASSED
===========================
```

---

## Troubleshooting

### Error: "verilator: command not found"

**Solution:** Verilator is not in PATH. Verify installation:
```bash
where verilator    # Should show path
verilator --version
```

If not found, manually add Verilator `bin/` directory to Windows PATH:
- Settings → Environment Variables → Edit PATH → Add `C:\Program Files\verilator\bin`
- Restart terminal and try again

### Error: "cocotb: command not found" or "ModuleNotFoundError: No module named cocotb"

**Solution:** Cocotb not installed in Python environment:
```bash
# Make sure venv is activated
pip list | grep cocotb    # Should show installed packages
pip install cocotb cocotb-test
```

### Error: "C++ compiler not found"

**Solution:** Required for Verilator compilation. Install:
- **MSVC:** Visual Studio Community with C++ workload
- **OR MinGW:** Download from mingw-w64.org, add to PATH

### Error: SystemVerilog Syntax Issues

**Solution:** Verify RTL files are valid:
```bash
verilator --lint-only ../rtl/src/*.sv
```

Fix any reported syntax errors (modern SystemVerilog constructs like `interface` may need simulation-specific flags).

---

## Next Steps (For Development)

After verifying the systolic array works:

### 1. Run Simplified Tests

Create simpler test vectors for quicker iteration:

```bash
python test_data_gen.py  # Generate test vectors
python test_systolic_array.py  # Run basic tests
```

### 2. Generate Waveforms (Optional)

To visualize signal behavior:

```bash
# Run with trace enabled
verilator --trace ../rtl/src/systolic_array.sv

# View waveform
gtkwave trace.vcd
```

### 3. Extend Testbench

Add more complex test cases to `test_systolic_array.py`:
- Multi-block cascades
- Edge cases (saturation, boundary conditions)
- Performance metrics (throughput, latency)

---

## Configuration Options

Edit `sim/Makefile` or `pyproject.toml` to customize:

- **Simulator:** Change `SIM = verilator` to other simulators (iverilog, ModelSim, etc.)
- **Trace:** Add `--trace` to capture waveforms
- **Optimization:** Add `-O3` for simulation speed

---

## References

- **Verilator:** https://www.veripool.org/verilator/
- **Cocotb:** https://docs.cocotb.org/
- **SystemVerilog:** https://en.wikipedia.org/wiki/SystemVerilog
- **Fixed-Point Arithmetic:** Q-format reference in golden_model.py

---

## Support

For issues:
1. Check RTL syntax: `verilator --lint-only ../rtl/src/*.sv`
2. Verify Python environment: `pip list`
3. Review Cocotb logs: Check console output for detailed error messages
4. Consult RTL comments: Each module has inline documentation

Good luck with your FlashAttention RTL! 🚀