-
Notifications
You must be signed in to change notification settings - Fork 161
add new lab "04_10_11_distributor" #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
32FedorovAlexey
wants to merge
2
commits into
yuri-panchul:main
Choose a base branch
from
32FedorovAlexey:04_10_11_distributor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
184 changes: 184 additions & 0 deletions
184
04_arithmetics_and_pipelining/04_10_11_distributor/03_08_float_discriminant.sv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| //---------------------------------------------------------------------------- | ||
| // Task | ||
| //---------------------------------------------------------------------------- | ||
|
|
||
| module float_discriminant ( | ||
| input clk, | ||
| input rst, | ||
|
|
||
| input arg_vld, | ||
| input [FLEN - 1:0] a, | ||
| input [FLEN - 1:0] b, | ||
| input [FLEN - 1:0] c, | ||
|
|
||
| output logic res_vld, | ||
| output logic [FLEN - 1:0] res, | ||
| output logic res_negative, | ||
| output logic err, | ||
|
|
||
| output logic busy | ||
| ); | ||
|
|
||
| // Task: | ||
| // Implement a module that accepts three Floating-Point numbers and outputs their discriminant. | ||
| // The resulting value res should be calculated as a discriminant of the quadratic polynomial. | ||
| // That is, res = b^2 - 4ac == b*b - 4*a*c | ||
| // | ||
| // Note: | ||
| // If any argument is not a valid number, that is NaN or Inf, the "err" flag should be set. | ||
| // | ||
| // The FLEN parameter is defined in the "import/preprocessed/cvw/config-shared.vh" file | ||
| // and usually equal to the bit width of the double-precision floating-point number, FP64, 64 bits. | ||
|
|
||
|
|
||
|
|
||
| //------------------------------------------------------------------------ | ||
| // вариант 2 FSM | ||
| //------------------------------------------------------------------------ | ||
|
|
||
| // States | ||
| enum logic [2:0] | ||
| { | ||
| st_idle = 3'd0, | ||
| st_1 = 3'd1, | ||
| st_2 = 3'd2, | ||
| st_3 = 3'd3, | ||
| st_4 = 3'd4, | ||
| st_5 = 3'd5 | ||
|
|
||
|
|
||
| } | ||
| state, next_state; | ||
|
|
||
| logic [FLEN - 1:0] f_mul_a, f_mul_b, f_mul_res; // connectors for multiplier | ||
| logic [FLEN - 1:0] b_b, ac; // tmp result; | ||
| logic [FLEN - 1:0] b_tmp; // b - lach | ||
| logic f_mul_busy, f_mul_error, f_mul_arg_vld; | ||
| //--------------------------------------------------------------------- | ||
| f_mult f_mul_i( // module multiplier | ||
| .clk(clk), | ||
| .rst(rst), | ||
| .a(f_mul_a), | ||
| .b(f_mul_b), | ||
| .up_valid(f_mul_arg_vld), | ||
| .res(f_mul_res), | ||
| .down_valid(f_mul_res_vld), | ||
| .busy(f_mul_busy), | ||
| .error(f_mul_error) | ||
| ); | ||
|
|
||
| //------------------------------------------------------------------------ | ||
|
|
||
| logic f_sub_arg_vld, f_sub_res_vld, f_sub_error; | ||
|
|
||
| f_sub f_sub_i( | ||
| .clk(clk), | ||
| .rst(rst), | ||
| .a(b_b), // аргументы берем из предыдущих | ||
| .b(ac), // результатаов | ||
| .up_valid(f_sub_arg_vld), | ||
| .res(res), // подключаем к выходу модуля | ||
| .down_valid(f_sub_res_vld), | ||
| .busy(sub_busy), | ||
| .error(f_sub_error) | ||
| ); | ||
|
|
||
| //------------------------------------------------------------------------ | ||
|
|
||
| always_comb | ||
| begin | ||
| next_state = state; | ||
| err = '0; // ошибки не обнаружены | ||
|
|
||
| case (state) | ||
| st_idle: | ||
| begin | ||
| f_mul_a = a; | ||
| f_mul_b = c; | ||
| res_vld = '0; | ||
| if (arg_vld) begin | ||
| f_mul_arg_vld = '1 ; // если входные данные валидны запускаем вычисление a*c | ||
| busy = '1 ; // выставдяем флаг занятости | ||
| next_state = st_1 ; // и переходим к следующей стадии возведение в квадрат | ||
| b_tmp = b; | ||
| end | ||
|
|
||
| end | ||
| st_1: | ||
| begin // умножитель у нас конвейерный на следующий такт запускаем возведение в степень b^2 | ||
| f_mul_a = b_tmp ; // загружаем умножитель значениями b | ||
| f_mul_b = b_tmp ; // и b | ||
| f_mul_arg_vld = '1 ; // запускаем вычисление | ||
| next_state = st_2 ; // на следующую стадию | ||
| end | ||
|
|
||
| st_2: | ||
| begin | ||
| f_mul_arg_vld = '0; | ||
| if (f_mul_error) begin | ||
| err = '1; // и выставляем флаг ошибки | ||
| end | ||
| if (f_mul_res_vld) begin // ждем окончания умножения a*c | ||
| f_mul_a = f_mul_res ; // загружаем умножитель значениями а*c | ||
| f_mul_b = $realtobits(4) ; // и 4 | ||
| f_mul_arg_vld = '1 ; // запускаем умножение 4*(a*c) | ||
| next_state = st_3; // и на следующую стадию | ||
| end | ||
| end | ||
|
|
||
| st_3: | ||
| begin | ||
| f_mul_arg_vld = '0 ; | ||
| if (f_mul_error) begin | ||
| err = '1 ; // за одно выставляем флаг ошибки | ||
| end | ||
| // так как мы на втором такте загрузили b*b | ||
| // проверку на готовность можно пропустить | ||
| b_b = f_mul_res ; // сохраняем результат | ||
| next_state = st_4 ; // к следующей стадии | ||
| end | ||
|
|
||
| st_4: | ||
| begin | ||
| f_mul_arg_vld = '0 ; | ||
| if (f_mul_error) begin | ||
| err = '1 ; // и выставляем флаг ошибки | ||
| end | ||
| if (f_mul_res_vld) begin // умножение 4*ac закончилось | ||
| ac = f_mul_res ; // сохраняем результат | ||
| // аргументы к вычитателю подключены | ||
| f_sub_arg_vld = '1 ; // запускаем вычитание | ||
| next_state = st_5 ; // к следующей стадии | ||
| end | ||
| end | ||
|
|
||
| st_5: | ||
| begin | ||
| if (f_sub_error) begin | ||
| err = '1 ; // и выставляем флаг ошибки | ||
|
|
||
| end | ||
| if (f_sub_res_vld) begin // вычитание b^2 - 4ac закончилось | ||
| res_vld = '1 ; // выставляем флаг готовности | ||
| busy = '0 ; // сбрасываем флаг занятости | ||
| f_sub_arg_vld = '0 ; // данные на входе вычитателя не актуальны | ||
| next_state = st_idle ; | ||
| end | ||
| end | ||
| endcase | ||
| end | ||
|
|
||
| //------------------------------------------------------------------------ | ||
| // Assigning next state | ||
|
|
||
| always_ff @ (posedge clk) | ||
| if (rst) | ||
| state <= st_idle; | ||
| else | ||
| state <= next_state; | ||
|
|
||
| //------------------------------------------------------------------------ | ||
|
|
||
| // конец 2 варианта FSM | ||
|
|
||
| endmodule |
96 changes: 96 additions & 0 deletions
96
04_arithmetics_and_pipelining/04_10_11_distributor/04_10_11_distributor.sv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| localparam LATENCY = 10 ; // латентность конвейера перенести в svh файл | ||
|
|
||
| // модуль имитируюший работу конвейера путем импементации нескольких N=LATENCY | ||
| // вычислителей с латентностью = LATENCY | ||
|
|
||
| // вижу несколько вариантов оформления решения | ||
| // первый выбор входов вычислителя идет через функцию "И" сигнала arg_vld и n-ного разряда one_hot | ||
| // регистра-селектора. При активном arg_vld, в каждом такте регистр-селектор сдвигается на 1 разряд | ||
| // выходы вычислителей через мультиплексор подключены к выходу модуля. При наличии сигнала res_vld | ||
| // каждый такт инкрементирует регистр-селектор выходов n_out. Сигнал res_vld формируется из входного | ||
| // сигнала arg_vld c задержкой в LATENCY тактов, в этом задержка реализуется на сдвиговом регистре | ||
| // delay_vld. Модули вычислителей могут быть подключены прямым текстом (copy - paste LATENCY раз), | ||
| // а могут через generate. | ||
|
|
||
| module distributor | ||
| ( | ||
| input clk, | ||
| input rst, | ||
|
|
||
| input arg_vld, | ||
| input [FLEN - 1:0] a, | ||
| input [FLEN - 1:0] b, | ||
| input [FLEN - 1:0] c, | ||
|
|
||
| output logic res_vld, | ||
| output logic [FLEN - 1:0] res, | ||
| output logic res_negative, | ||
| output logic err, | ||
|
|
||
| output logic busy | ||
| ); | ||
|
|
||
| logic [LATENCY-1:0] ptr_in; // one-hot registre | ||
| logic [$clog2(LATENCY-1):0] n_out; // регистр хранения номера вычислителя с валидным результатом | ||
| logic [LATENCY-1 :0] delay_vld; // сдвиговый регистр для сигнала vld | ||
| logic [FLEN - 1:0] res_0, res_1, res_2, res_3, res_4, res_5, res_6, res_7, res_8, res_9; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А зачем эти переменные? Они же не используются
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Переменные забыл удалить, я их использовал в промежуточном варианте. |
||
| logic start_0, start_1, start_2, start_3, start_4, start_5, start_6, start_7, start_8, start_9; | ||
|
|
||
| logic [LATENCY-1:0] res_vld_o, res_negative_o, err_o, busy_o; // векторы для подключения выходных сигналов вычислителей | ||
| logic [FLEN - 1:0] res_o [LATENCY-1:0]; | ||
|
|
||
| always_ff @ (posedge clk) begin | ||
| if (rst) begin | ||
| ptr_in <= 1; // загружаем "1" в one-hot регистр селектор | ||
| n_out <='0; // "0" в регистр-селектор выходов вычислителей | ||
| delay_vld <='0; | ||
| end | ||
| else begin | ||
| delay_vld <= {delay_vld[LATENCY-1:0],arg_vld}; // передаем данные arg_vld с задержкой в LATENCY тактов на выход модуля | ||
| if (arg_vld) begin | ||
| ptr_in <= {ptr_in[LATENCY-2:0],ptr_in[LATENCY-1]}; // пришли данные - сдвигаем входной регистр-селектор маски | ||
| end | ||
|
|
||
| if (delay_vld[LATENCY-1]) begin // если на выходе готовы данные то | ||
| if (n_out == (LATENCY-1 )) n_out <= '0; // и если дошли до последнего вычислителя то сбрасываем номер вычислителя | ||
| else n_out <= n_out + 1 ; // иначе увеличиваем номер вычислителя подключенного к выходу | ||
| end | ||
|
|
||
| end | ||
| end | ||
|
|
||
| genvar i; | ||
|
|
||
|
|
||
| // создаем модули вычислителей в количестве LATENCY | ||
| generate | ||
| for(i = 0; i < LATENCY; i = i + 1) begin:calc | ||
|
|
||
| float_discriminant inst ( | ||
| .clk(clk), | ||
| .rst(rst), | ||
| .arg_vld(arg_vld & ptr_in[i]), | ||
| .a(a), | ||
| .b(b), | ||
| .c(c), | ||
|
|
||
| .res_vld(res_vld_o[i]), | ||
| .res(res_o[i]), | ||
| .res_negative(res_negative_o[i]), | ||
| .err(err_o[i]), | ||
| .busy(busy_o[i]) | ||
| ); | ||
|
|
||
| end | ||
| endgenerate | ||
|
|
||
|
|
||
| always_comb begin | ||
| res = res_o[n_out] ; | ||
| res_negative = res_negative_o[n_out]; | ||
| err = err_o[n_out]; | ||
| res_vld = delay_vld[LATENCY-1]; | ||
| end | ||
|
|
||
|
|
||
| endmodule | ||
Binary file not shown.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Так как по условию задачи латентность модуля фиксирована, и при этом есть вывод res_vld, который тем самым находится в фиксированном отношении с arg_vld, то нам не нужно протаскивать arg_vld, не так ли? Достаточно использовать res_vld_o[n_out]?
Завтра посмотрю внимательнее
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Вы правы, можно легко обойтись без сдвигового регистра delay_vld. Можно еще упростить код если на выходе вычислителей использовать регистры с Z состоянием. Если есть на выходе корректные данные, то модуль выдает их на шину, если нет то отключается от шины. Тогда код сведется к двум строчкам, загрузить 1 в регистр-селектор, и сдвинуть его если на входе есть данные.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Не, Z на внутренних сигналах в дизайнах не используется