חזור לתיקייה: סדנה

2 - SystemVerilog II

חיפוש בתיקיה
Ctrl K
לחץ כאן או הקש Ctrl K כדי לחפש בכל התיקיה
  • כללי
  • יצוג מספרים
  • פעולות על ביטים
  • הזזות ביטים
  • קדימות פעולות
  • בניית מודולים
  • השמות
  • תהליכים
  • פרמטרים
  • קבועים
  • עיבוד מקבילי בשפות תיאור תוכנה

2 - SystemVerilog II

00440252

מערכות ספרתיות ומבנה המחשב

רשימות הרצאה לקריאה בשבת

SystemVerilog II

הופק מתוך תקיית התואר של שקד

uni-folder.vercel.app

תרגיל

אילו ערכים logic a יכול לקבל והאם הם תקינים?

logic  a={0תקין1תקיןxתלוי מתיzלא תקיןlogic\,\, a=\begin{cases} 0 & \R{\text{תקין}} \\ 1 & \R{\text{תקין}} \\ x & \R{\text{תלוי מתי}} \\ z & \R{\text{לא תקין}} \end{cases}logica=⎩⎨⎧​01xz​תקיןתקיןתלוי מתילא תקין​
תרגיל

אני רוצה להגדיר וקטור של 8 ביטים. איך כדאי לי לעשות את זה?

  1. vec w1 [7:0];
  2. logic w1 [7:0];
  3. logic w1 [8:0];
  4. logic [7:0] w1;

המילה vecvecvec אינה מילה שמורה ולכן 111 לא נכון.
אנו רוצים וקטור באורך 8 ולא 9 ולכן 333 לא נכון.
כעת יש לנו שתי אפשרויות - אפשרות 222 לא נותנת שגיאות קומפילציה אבל היא לא מגדירה מערך כמו שאנחנו רוצים - אז אל תעשו את זה.
לכן, התשובה הנכונה היא 444.

יצוג מספרים

ב-SystemVerilogSystemVerilogSystemVerilog, בגלל שאנחנו עובדים בחוטים אנו יכולים לעבוד רק עם 000 ו-111 אך בני אדם לא אוהבים לעבוד בבינארית, אלא אוהבים לעבוד בבסיס 101010.
לשם כך, ב-SystemVerilogSystemVerilogSystemVerilog לכל מספר נוכל להגדיר את מספר הביטים שהוא יקח, האם הוא בעל סימן, באיזה בסיס הוא כך:

<width'><signed><radix> value

כאשר הברירת מחדל לערכים אלה היא אורך של 322 ביטים, מספר ללא סימן המייוצג בבסיס 10.
האופציות שיש לנו ל-radixradixradix הן:

  • בינארי - b  or  Bb \; or\; BborB
  • בסיס שמונה - o  or  Oo\; or\; OoorO
  • עשרוני - d  or  Dd\; or\; DdorD
  • הקסה - h  or  Hh\; or\;HhorH
דוגמה

1'b0 - מספר בעל ביט אחד עם ערך 0 ללא סימן בבסיס בינארי
8'hFF - מספר בעל 8 ביטים מבסיס הקסה ללא סימן עם ערך של 0xFF0xFF0xFF או 255255255 בעשרוני.
14 - מספר של 32 ביטים מבסיס עשרוני ללא סימן עם ערך של 141414
-14 - מספר של 32 ביטים מבסיס עשרוני עם סימן עם ערך של −14-14−14 (שכמובן נשמר בשיטת המשלים ל-2)
4'sb1011 - מספר באורך 4 ביטים מבסיס בינארי עם סימן עם ערך של −5-5−5
12'b1010_1101_0001=12'hAD1 - מספר באורך 12 ביטים ללא סימן עם ערך של 276927692769 (יש התעלמות ממקפים תחתונים)

הערה

אם נרצה לעשות הרחבת משתנה ללא סימן אז נדרש לעשות:

logic [7:0] out; 
logic [3:0] A; 
out = {{4{1'b0}}, A[3:0]};

פעולות על ביטים

ישנן כמה פעולות בינאריות/אונריות שאנו יכולים לבצע על ביטים והן:

  1. שער 'וגם': &\&&
  2. שער 'או': ∣|∣
  3. שער היפוך(NOTNOTNOT): ∼\sim∼
  4. שער XORXORXOR: ^
  5. שער XNORXNORXNOR: ^ או ^

הפעולות נעשות על ביטים יחידים בלבד!

הזזות ביטים

נוכל להזיז ביטים ימינה עם >>>>>> או שמאלה עם <<<<<<.

דוגמה

a = 4'b1010;
b = a << 2; // b = 4'b1000
c = a >> 1; // c = 4'b0101

קדימות פעולות

Verilog OperatorNameFunctional Group()bit-select or part-select()parenthesis!logical negationLogical∼negationBit-wise&reduction ANDReduction∣reduction ORReduction∼&reduction NANDReduction∼∣reduction NORReduction∧reduction XORReduction∼∧ or ∧∼reduction XNORReduction+unary (sign) plusArithmetic−unary (sign) minusArithmetic{}concatenationConcatenation{{}}replicationReplication∗multiplyArithmetic/divideArithmetic%modulusArithmetic+binary plusArithmetic−binary minusArithmetic<<shift leftShift>>shift rightShift>greater thanRelational>=greater than or equal toRelational<less thanRelational<=less than or equal toRelational==logical equalityEquality!=logical inequalityEquality===case equalityEquality!==case inequalityEquality&bit-wise ANDBit-wise∧bit-wise XORBit-wise∼∧ or ∧∼bit-wise XNORBit-wise∣bit-wise ORBit-wise&&logical ANDLogical∣∣logical ORLogical?:conditionalConditional\begin{array}{|c|l|l|} \hline \textbf{Verilog Operator} & \textbf{Name} & \textbf{Functional Group} \\ \hline \mathtt{()} & \text{bit-select or part-select} & \\ \hline \mathtt{()} & \text{parenthesis} & \\ \hline \mathtt{!} & \text{logical negation} & \text{Logical} \\ \mathtt{\sim} & \text{negation} & \text{Bit-wise} \\ \mathtt{\&} & \text{reduction AND} & \text{Reduction} \\ \mathtt{|} & \text{reduction OR} & \text{Reduction} \\ \mathtt{\sim\&} & \text{reduction NAND} & \text{Reduction} \\ \mathtt{\sim|} & \text{reduction NOR} & \text{Reduction} \\ \mathtt{\wedge} & \text{reduction XOR} & \text{Reduction} \\ \mathtt{\sim\wedge} \text{ or } \mathtt{\wedge\sim} & \text{reduction XNOR} & \text{Reduction} \\ \hline \mathtt{+} & \text{unary (sign) plus} & \text{Arithmetic} \\ \mathtt{-} & \text{unary (sign) minus} & \text{Arithmetic} \\ \hline \mathtt{\{\}} & \text{concatenation} & \text{Concatenation} \\ \hline \mathtt{\{\{\}\}} & \text{replication} & \text{Replication} \\ \hline \mathtt{*} & \text{multiply} & \text{Arithmetic} \\ \mathtt{/} & \text{divide} & \text{Arithmetic} \\ \mathtt{\%} & \text{modulus} & \text{Arithmetic} \\ \hline \mathtt{+} & \text{binary plus} & \text{Arithmetic} \\ \mathtt{-} & \text{binary minus} & \text{Arithmetic} \\ \hline \mathtt{<<} & \text{shift left} & \text{Shift} \\ \mathtt{>>} & \text{shift right} & \text{Shift} \\ \hline \mathtt{>} & \text{greater than} & \text{Relational} \\ \mathtt{>=} & \text{greater than or equal to} & \text{Relational} \\ \mathtt{<} & \text{less than} & \text{Relational} \\ \mathtt{<=} & \text{less than or equal to} & \text{Relational} \\ \hline \mathtt{==} & \text{logical equality} & \text{Equality} \\ \mathtt{!=} & \text{logical inequality} & \text{Equality} \\ \hline \mathtt{===} & \text{case equality} & \text{Equality} \\ \mathtt{!==} & \text{case inequality} & \text{Equality} \\ \hline \mathtt{\&} & \text{bit-wise AND} & \text{Bit-wise} \\ \hline \mathtt{\wedge} & \text{bit-wise XOR} & \text{Bit-wise} \\ \mathtt{\sim\wedge} \text{ or } \mathtt{\wedge\sim} & \text{bit-wise XNOR} & \text{Bit-wise} \\ \hline \mathtt{|} & \text{bit-wise OR} & \text{Bit-wise} \\ \hline \mathtt{\&\&} & \text{logical AND} & \text{Logical} \\ \hline \mathtt{||} & \text{logical OR} & \text{Logical} \\ \hline \mathtt{?:} & \text{conditional} & \text{Conditional} \\ \hline \end{array}Verilog Operator()()!∼&∣∼&∼∣∧∼∧ or ∧∼+−{}{{}}∗/%+−<<>>>>=<<===!====!==&∧∼∧ or ∧∼∣&&∣∣?:​Namebit-select or part-selectparenthesislogical negationnegationreduction ANDreduction ORreduction NANDreduction NORreduction XORreduction XNORunary (sign) plusunary (sign) minusconcatenationreplicationmultiplydividemodulusbinary plusbinary minusshift leftshift rightgreater thangreater than or equal toless thanless than or equal tological equalitylogical inequalitycase equalitycase inequalitybit-wise ANDbit-wise XORbit-wise XNORbit-wise ORlogical ANDlogical ORconditional​Functional GroupLogicalBit-wiseReductionReductionReductionReductionReductionReductionArithmeticArithmeticConcatenationReplicationArithmeticArithmeticArithmeticArithmeticArithmeticShiftShiftRelationalRelationalRelationalRelationalEqualityEqualityEqualityEqualityBit-wiseBit-wiseBit-wiseBit-wiseLogicalLogicalConditional​​

בניית מודולים

לצערנו, לא כל המודולים שאנחנו רוצים שיהיו קיימים, קיימים. לכן, נדרש לדעת איך לממש בעצמנו מודול.

השמות

כאן אנו מחברים חוטים ואומרים באופן די מפורש מה הערך שאנו רוצים שיתקבל.

דוגמה

logic  signal;
assign signal = 1'b0;

ברגע שאנו עושים את זה אנחנו בעצם לקחנו את החוט שלנו וחיברנו אותו למקור מתח המוציא את הערך הנתון - במקרה הזה הוא 0.
אם ננסה גם לחבר עכשיו מתח גבוה אז בעצם המחשב יתפוצץ.

דוגמה

logic  signal1;
logic  signal2;
assign = signal2 = signal1;

פה אנחנו פשוט מאריכים את החוט signal1signal1signal1 עד לחוט signal2signal2signal2.
זה לא נשמע מאוד שימושי אך בהמשך נראה מתי זה כן יהיה שימושי.

דוגמה

module and_module (
	output logic Z,
	input  logic A,
	input  logic B
);
	assign Z = (A & B) | C;
endmodule

תהליכים

נוכל להשתמש במילה initialinitialinitial כדי להגדיר בלוק אשר מה שיש בו יקרה רק פעם אחת.

initial begin
	<statement1>;
	#time_to_wait
	<statement2>;
	#time_to_wait
	...
end
דוגמה

module mymodule_tb;
	logic a;
	logic out;
	mymodule uut (
		.a(a),
		.out(out)
	);
	
	initial begin
		a = 1'b0;
		#5
		a = 1'b1;
		#5
		a = 1'b0;
	end
endmodule
הערה

נשים לב כי בדוגמה הזאת שינינו את aaa. מה שמקודם המתרגל אמר שיפוצץ את המחשב.
בגלל שזה לא ב-assignassignassign אז זה עובד. למה? כי ככה - אין לי מושג מה הולך עם ה-syntaxsyntaxsyntax של השפה הזאת, הוא ממש לא אחיד וברור.

נוכל בנוסף להשתמש במילה always_combalways\_combalways_comb אשר מאפשר לנו לבצע דברים שמתקיימים תמיד.
זה עוזר לנו לממש חומרה כדי להגיד שהדבר הזה תמיד קיים שם.

always_comb begin
	<statement1>;
	<statement1>;
end

כאשר אנו ממשים חומרה עם always_combalways\_combalways_comb זה כמו לעשות assignassignassign.
בסדנאות הבאות אנו נראה איך זה בדיוק שונה.

דוגמה

module add_module (
	output logic Z,
	input  logic A,
	input  logic B
);
	always_comb begin
		Z = A & B;
	end
endmodule

זה יוצר שער ANDANDAND שמתעדכן על פי הערכים A,BA,BA,B.

דוגמה

module shift_module (
	output logic [3:0] out,
	input  logic [3:0] a
);
	always_comb begin
		out = a << 2;
	end
endmodule

פרמטרים

פרמטרים הם בעצם משתנים שניתן להכניס למודולים ומאפשרים לנו לשנות את התנהגות המודול באופן דינמי.
הפרמטרים הם לא באמת חוט אלא הוא איזשהו תכונה של האלמנטים הפיזיים.

הגדרה:

module module_name ( 
	output logic out1, 
	..., 
	input logic in1 
); 

parameter param1 = 1; 
parameter param2 = 2; 
parameter param3 = 3; 
... 
endmodule

שימוש:

module_name #( 
	.param1(value1), 
	.param2(value2), 
	.param3(value3) 
) instname ( 
	.in1(in1), 
	... 
	.out1(out1)
);
דוגמה

module shift_N ( 
	output logic [3:0] out, 
	input  logic [3:0] a 
); 
	parameter N = 1; 
	always_comb begin 
		out = a << N; 
	end 
endmodule

module shifts ( 
	output logic [3:0] b, 
	output logic [3:0] c, 
	input logic [3:0] a 
); 
	shift_N #( 
		.N(2) 
	) shift_inst1 ( 
		.a(a), 
		.out(b) 
	); 
	shift_N #( 
		.N(3) 
	) shift_inst2 ( 
		.a(a), 
		.out(c) 
	); 
endmodule

קבועים

נוכל להגדיר קבועים כך:

localparam <param_name> = <param_value>;

עיבוד מקבילי בשפות תיאור תוכנה

דוגמה

module my_module ( 
	output logic c 
); 
	logic a; 
	logic b; 
	assign c = a | b; 
	assign a = 1'b0; 
	assign b = 1'b1; 
endmodule
דוגמה (מימוש MUXMUXMUX)

module mux (
	output logic out,
	output logic outn,
	input logic [1:0] in,
	input logic sel
);
	logic out1, out2, selb;
	
	and a1 (out1, in[0], sel);
	not i1 (selb, sel);
	and a2 (out2, in[1], selb);
	always_comb begin
		out = out1 | out2;
	end
	assign outn = ~out;
endmodule