Arrays
Coddy Verilog 여정의 기초 섹션에 포함된 레슨. 90개 중 10번째.
array를 사용하면 하나의 변수에 여러 값을 저장할 수 있습니다. array의 각 elements에는 index를 통해 액세스할 수 있습니다.
참고: array는 별도의 data type이 아닙니다. wire, reg, integer 또는 real type의 모음입니다.
배열 선언
구문: <data_type> <name> [<size>];
reg [7:0] memory [0:255]; // 256바이트의 메모리 (각각 8비트)
reg data [0:7]; // 8개의 단일 비트 레지스터
wire [3:0] bus [0:3]; // 4개의 버스, 각각 4비트 폭
integer counters [0:9]; // 10개의 정수대괄호 안의 숫자 [ ]는 비트 너비가 아니라 array 크기입니다.
array 요소에 접근하기
reg [7:0] memory [0:3];
memory[0] = 165; // 십진수 165
memory[1] = 90; // 십진수 90
memory[2] = memory[0] + memory[1];
$display("%d", memory[2]); // 출력: 255arrays는 testbench에서 test_data를 저장하는 데 매우 유용합니다.
다차원 배열
여러 차원의 배열을 만들 수 있습니다:
reg [7:0] matrix [0:3][0:3]; // 8비트 값의 4x4 배열
matrix[0][0] = 255; // 8'hFF = 255 십진수
matrix[2][1] = 85; // 8'h55 = 85 십진수array와 벡터
| 벡터 | array | |
|---|---|---|
| 무엇인가 | 여러 비트의 wire 또는 reg | 여러 values의 모음 |
| 구문 | [MSB:LSB] | [size] |
| 예시 | reg [7:0] data; | reg [7:0] mem [0:255]; |
| 접근 | data[3] (bit 3) | mem[3] (요소 3) |
벡터는 여러 bits를 가진 하나의 값입니다.
array는 여러 values이며, each 고유한 bits를 가집니다.
중요 참고 사항
- 대부분의 도구에서 큰 크기로 사용되는 경우 Array는 합성할 수 없습니다
- Array는 주로 testbenches에서 사용됩니다
- 하드웨어 메모리에는 특수 메모리 프리미티브를 사용하세요
챌린지
테스트 값 4개를 저장하는 array를 생성하도록 아래 코드를 완성하세요.
할 일:
- array를
test_data라고 Declare하세요.-
regdata type을 사용하세요 (testbench에서 values를 stores하기 때문입니다) - 각 element는 8 bits wide여야 합니다 (
[7:0]) - array에는 4 elements가 있어야 합니다 (
[0:3])
-
직접 해보기
module arrays;
// test_data라는 배열을 선언하세요
// 4개의 요소를 가져야 하며, 각각 8비트 너비여야 합니다
// reg 데이터 타입을 사용하세요 (테스트벤치에서 값을 저장하기 때문입니다)
integer i;
initial begin
test_data[0] = 170;
test_data[1] = 240;
test_data[2] = 204;
test_data[3] = 15;
for (i = 0; i < 4; i = i + 1) begin
$display("test_data[%0d] = %b", i, test_data[i]);
end
$finish;
end
endmodule이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.
기초의 모든 레슨
4Operators Part 1
Arithmetic OperatorsModulo OperatorComparison OperatorsRecap - Simple MathBitwise Operators7Assign And Gates
Continuous AssignmentAssign With OperatorsBuilt In Gate PrimitivesAND OR NOT GatesXOR XNOR GatesRecap - Logic Gate Circuit10Decision Making
If StatementIf - ElseRecap - Simple ComparatorCase StatementCasex And CasezRecap - ALU Design5Operators Part 2
Logical OperatorsReduction OperatorsShift OperatorsConcatenation OperatorConditional OperatorRecap - Operator Challenge3Number System
Binary RepresentationSized NumbersUnsized NumbersNegative NumbersSpecial Values X And ZRecap - Number Formats6Modules
Module StructureInput And Output PortsInout PortsModule InstantiationPort Mapping By NamePort Mapping By OrderRecap - Build A Module9Procedural Blocks
Always BlockInitial BlockSensitivity ListBlocking AssignmentNon Blocking AssignmentRecap - Always vs Initial15Traffic Light Controller
Defining The StatesState Machine Logic직접 연습해 보세요: 온라인 Verilog 컴파일러