Codiode

Bidirectional Arithmetic Shift Register

HardVerilog / SystemVerilogBuild1 solved

Shifting a signed number right by one divides it by two, provided the sign bit is copied in at the top. The bidirectional arithmetic shift register holds an 8-bit value that can be loaded in parallel or shifted serially. When shifting left, the serial input fills the least significant bit. When shifting right, the behavior depends on the arithmetic control flag. A logical right shift fills the most significant bit with the serial input. An arithmetic right shift ignores the serial input and replicates the current most significant bit (the sign bit) to preserve the sign of the two's complement value.

The module operates on the positive edge of clk and features an asynchronous active-low reset rst_n. The parallel load signal has priority over the shift en signal. When load is asserted, the register captures d_in. When load is de-asserted and en is asserted, the register shifts its contents based on the dir (direction) and arith (arithmetic) flags. If neither load nor en are asserted, the register holds its current value. Left shifts always shift in s_in to the LSB, regardless of the arith flag.

  • Cycle 1: rst_n=0 → q_out=8'h00 (asynchronous reset)
  • Cycle 2: rst_n=1, load=1, d_in=8'hA5 → q_out=8'hA5 at clock edge
  • Cycle 3: load=0, en=1, dir=0, arith=1 → q_out=8'hD2 at clock edge
  • Cycle 4: en=1, dir=0, arith=1 → q_out=8'hE9 at clock edge
  • Cycle 5: en=1, dir=1, s_in=1 → q_out=8'hD3 at clock edge
  • Cycle 6: en=1, dir=0, arith=0, s_in=0 → q_out=8'h69 at clock edge
  • Cycle 7: en=0 → q_out=8'h69 (hold) at clock edge
Signal Direction Width Description
clk input 1 Positive-edge triggered clock
rst_n input 1 Asynchronous active-low reset; drives q_out to 8'b00000000
en input 1 Shift enable; shifts q_out on posedge clk when 1 and load is 0
load input 1 Synchronous parallel load; overrides en
dir input 1 Shift direction; 1 for left shift, 0 for right shift
arith input 1 Arithmetic shift flag; when 1 during a right shift, replicates the MSB
s_in input 1 Serial input; shifted into LSB on left shift, or MSB on logical right shift
d_in input 8 Parallel data input loaded when load is 1
q_out output 8 Registered shift register output

Constraints

  • Clock edge and reset polarity: posedge clk, asynchronous active-low rst_n.
  • Output values on reset: q_out must be exactly 8'b00000000.
  • Priority when multiple inputs are simultaneously asserted: rst_n > load > en.
  • Left shifts always shift in s_in to the LSB, regardless of the arith flag.
  • Right shifts with arith=1 ignore s_in and replicate the current MSB (q_out[7]).

Topics

Shift RegisterDSPArithmetic Logic

Solve this problem

Write the module in Verilog, SystemVerilog or VHDL. Your submission is compiled and simulated against the problem's testbench. You get the waveform back, not a stored answer.

This problem is part of Codiode Pro. The statement above is free to read.

The circuit builder and code editor need a desktop screen. On a phone, read the problem here and open it on a laptop to solve.

Part of Sequential RTL

Problem 14 of 26 in the Sequential RTL track.

Related problems

Browse all problems · Verilog practice problems · Learning tracks

Solve this on a laptopSend yourself the link and continue there.