Arithmetic Operators
CoddyのVerilogジャーニー「基礎」セクションの一部。レッスン 19/90。
Verilog には、基本的な算術演算子があります。+(加算)、-(減算)、*(乗算)、/(除算)です。
コード例:
module arithmetic_demo;
reg [3:0] a, b;
reg [7:0] add, sub, mul, div;
initial begin
a = 4'd9;
b = 4'd4;
add = a + b; // 9 + 4 = 13
sub = a - b; // 9 - 4 = 5
mul = a * b; // 9 * 4 = 36
div = a / b; // 9 / 4 = 2
$display("9 + 4 = %d", add);
$display("9 - 4 = %d", sub);
$display("9 * 4 = %d", mul);
$display("9 / 4 = %d", div);
$finish;
end
endmodule出力:
9 + 4 = 13 9 - 4 = 5 9 * 4 = 36 9 / 4 = 2
重要な注意事項
- 割り算は integer division(fractions なし)を実行します
- multiplication の結果を格納できるよう、result レジスタに十分な幅を確保してください
チャレンジ
このチャレンジでは、演算子 +、-、*、/ を使用して、正しい算術式を書く必要があります。
行うこと:
a + bを計算し、addに格納するa - bを計算し、subに格納するa * bを計算し、mulに格納するa / bを計算し、divに格納する
自分で試してみよう
module arithmetic_challenge;
reg [3:0] a, b;
reg [7:0] add, sub, mul, div;
initial begin
a = 4'd12;
b = 4'd5;
add = ______; // a + b
sub = ______; // a - b
mul = ______; // a * b
div = ______; // a / b
$display("12 + 5 = %d", add);
$display("12 - 5 = %d", sub);
$display("12 * 5 = %d", mul);
$display("12 / 5 = %d", div);
$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オンラインコンパイラ