0% found this document useful (0 votes)
131 views

Verilog: Codificadores Y Decodificadores

The document describes various shift operations in ARM assembly language: 1. Arithmetic shift right (ASR) shifts bits to the right while preserving the sign bit. 2. Logical shift right (LSR) shifts bits to the right while filling with zeros. 3. Logical shift left (LSL) shifts bits to the left while filling with zeros. Shift operations can specify an immediate value or use a register to determine the number of bit positions to shift.

Uploaded by

Hamza Amari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
131 views

Verilog: Codificadores Y Decodificadores

The document describes various shift operations in ARM assembly language: 1. Arithmetic shift right (ASR) shifts bits to the right while preserving the sign bit. 2. Logical shift right (LSR) shifts bits to the right while filling with zeros. 3. Logical shift left (LSL) shifts bits to the left while filling with zeros. Shift operations can specify an immediate value or use a register to determine the number of bit positions to shift.

Uploaded by

Hamza Amari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

VERILOG

CODIFICADORES Y DECODIFICADORES

DECODIFICADOR 2x4 ESTRUCTURAL

module dec2x4:estructural (ent,sal,habilitacion);

input [1:0] ent;

output [3:0] sal;

input habilitacion;

wire [1:0] seleccion;

genvar 1

f_dec1x2_e dec_sel (.hab(habilitacion),.ent(ent[1]), .sal(seleccion));

f_dec1x2_e dec_sal (.hab(seleccion[0]),.ent(ent[0]), .sal(sal[1:0]);

f_dec1x2_e dec_sal2 (.hab(seleccion[1]),.ent(ent[0]), .sal(sal[3:2]);

CODIFICADOR SIN PRIORIDAD

module cod4x2 (ent,sal);

input [3:0] ent;

output reg [1:0] sal;

always @ (ent)begin

sal = 0;

case (ent)

4'b0001 : sal = 2'b00;

4'b0010 : sal = 2'b01;

4'b0100 : sal = 2'b10;

4'b1000 : sal = 2'b11;

default: sal = 2'bx;

endcase;

end

endmodule

CODIFICADOR CON PRIORIDAD

module coden(solicitud, codigo, gs);

parameter ANCHO = 4;

input [(1<<ANCHO) - 1:0] solicitud;

output reg [ANCHO - 1:0] codigo;


output reg gs;

integer i;

always @ (solicitud)

begin

codigo = 0;

gs = 1;

for (i=0; i<=((1<<ANCHO)-1); i=i+1)

if (solicitud[i]) begin

gs = 0;

codigo[i] = 1;

end

end

endmodule

CODIGO GRAY

module gray(ent, sal);

input [1:0] ent;

output reg [1:0] sal;

always @ (ent)begin

sal = 0;

case (ent)

2'b00 : sal = 2'b00;

2'b01 : sal = 2'b01;

2'b10 : sal = 2'b11;

2'b11 : sal = 2'b10;

default: sal = 2'bx;

endcase;

end

endmodule

CODIFICADOR 9x3

module cod9x3 (ent, sal, prioridad);

parameter ANCHO = 3;

input [(1<<ANCHO)-1:0] ent;

output reg [ANCHO-1:0] sal;

output reg prioridad;

integer i;
always @ (ent) begin

prioridad = 1;

sal= 0;

for (i=0; i<= ((1<<ANCHO)-1); i=i+1)

if (ent[i]) begin

priodidad = 0;

sal[i] = 1;

end

end

endmodule

module cod9x4 (ent, sal, gs);

input [3:0] ent;

output reg [8:0] sal;

output reg gs;

integer i;

always @(ent) begin

gs = 1;

sal = 0;

for (i=0; i<=8; i=i+1)

if (ent[i]) begin

gs = 0;

sal[i] = 1;

end

end

endmodule

MULTIPLEXORES
module mux4a1 (ent,sal,control,enable)

parameter ANCHO = 4;

input [(ANCHO)-1:0] ent;

input [(1<<ANCHO)-1:0] control;

input enable;

output sal;

assign sal = enable?ent[control]:0;

endmodule
TESTBENCH
TESTBENCH PARA DECODIFICADOR 1X2

module dec1x2_tb ();

reg ent;

wire [1:0] sal;

dec1x2 UUT (.ent(ent),.sal(sal));

initial begin: TB

integer i;

$dumpfile("test.vdc");

$dumpvars(0,dec1x2_tb);

for (i=0; i<=1; i=i+1) begin

ent=i;

#10;

$write("Entra %d y sale %d\n", ent, sal);

end

endmodule

TESTBENCH PARA CIRCUITOS SINCRONOS

module testbasic ();

reg MCLK, ent;

wire salD,salI;

reg [0:9] testvector;

inmediato uut (.ent(ent),.clk(MCLK),salI(salI));

diferido uut (.ent(ent),.clk(MCLK),.salD(salD));

always begin

#6 MCLK = 1; #4 MCLK = 0;

end

initial begin: TB

integer i;

$dumpfile("test.vdc");

$dumpvars(0,testbasic);

MCLK = 1;

testvector = 10'b0011100111;

for (i=0; i<=9; i=i+1) begin

ent = testvector[i];

#10;

$write("%d / %d - \n"), salD, salI);


end

$finish(1);

end

endmodule

TESTBENCH BIESTABLE JK

module jkff_tb();

reg MCLK, J, K, qold;

wire q;

reg [0:8] testvectorJ, testvector K;

jfkk uut (.J(J),.K(K),.CLK(MCLK),.Q(q);

always begin

#6 MCLK = 1; #4 MCLK = 0;

end

initial begin TB:

integer i;

testvectorJ = 9b'010101011;

testvectorK = 9b'110010111;

MCLK = 1;

$dumpfile("test.vcd");

$dumpvars(0,jkff_tb);

for (i=0; i<=8; i=i+1) begin

qold = q;

J = testvectorJ[i];

K = testvectorK[i];

#10;

$write("Entradas %d y %d con estado antiguo %d y estado actual %d",J,K,qold,q);

end

$finish(1);

end

endmodule
CIRCUITOS SECUENCIALES
BIESTABLE JK

module jkff(CLK,J,K,Q);

input CLK, J, K;

output reg Q = 0;

always @ (posedge CLK)

case ({J,K})

1: Q <= 0;

2: Q <= 1;

3: Q <= ~Q;

default Q <= Q;

endcase

endmodule

BIESTABLE RS

module rs_ff(rs,clk,q,qb);

input [1:0] rs;

input clk;

output reg q, qb;

always @ (posedge clk)

begin

case(rs)

2'b00: q = q;

2'b01: q = 1'b1;

2'b10: q = 1'b0;

2'b11: q = 1'bx;

endcase

qb = ~q;

end

endmodule

BIESTABLE T

module t_ff(t,clk,q,qb);

input t;

input clk;

output reg q, qb;

always @ (posedge clk)


begin

case(t)

1'b0: q = q;

1'b1: q = ~q;

endcase

qb = ~q;

end

endmodule

REGISTRO DE DESPLAZAMIENTO

module ej1 (Q, sel, ent, nuevovalor);

output reg [7:0] Q=0;

input ent, clk;

input [1:0] sel;

input [7:0] nuevo valor;

always @ (posedge clk)

case (sel)

0: Q<=Q; //mantiene valor almacenado

1: Q<={Q[6:0],ent}; //desplaza a la izquierda

2: Q<={ent,Q[7:1]}; //desplaza a la derecha

3: Q<=nuevovalor; //carga en paralelo

default: Q<=8'bx;

endcase

endmodule

CONTADOR JOHNSON

module contjohnson (CLOCK_50,G_LED, SW);

input CLOCK_50;

input [0:0] SW;

output wire [9:0] G_LED;

wire CLK1;

reg [7:0] Q;

divfre_mej UUT (.mclk(CLOCK_50),.divfre(CLK1));

assign G_LED = Q;

initial Q = 0;

always @(posedge (CLK1))


if (SW[0]) Q<=0;

else

if (Q == 0) Q <= {1'b1, Q[7:1]};

else if (Q == 255) Q <= {1'b0, Q[7:1]};

else Q <= {Q[7], Q[7:1]};

endmodule

DISPLAY

initial Q = 48'hB00B5A55;

always @(posedge (CLK1))

if (SW[0]) Q<=0;

else Q <= {Q[3:0], Q[47:4]};

endmodule

CONTADOR BÁSICO

initial Q = 0;

always @(posedge (CLK1))

if (SW[0]) Q<=0;

else Q<=Q+1;

endmodule

DIVISOR DE FRECUENCIA

module ex(in1,in2,clk,sal,divfre);

input in1; //Entrada 1

input in2; //Entrada 2

input clk; //Reloj de 100 MHz

output reg sal; //Salida

output reg divfre = 1; //Reloj de 10 MHz

parameter frecdst = 10000000; //10 MHz

parameter frecreloj = 100000000; //100 MHz

localparam div = frecreloj/frecdst/2; //Cálculos entre frecuencias, fraccionar

reg cuenta; //Cuenta de fracciones

always @(posedge clk)

begin

cuenta <= cuenta +1; //

if (cuenta == div-1) begin //Cuando cuenta es igual a div-1

divfre <= ~divfre; //Cambiar el valor de divfre


cuenta <= 0; //Reiniciar cuenta

end

end

always @(posedge divfre) //Cuando frivfre tenga pulso de subida (Para cada ciclo)

begin

case (sal) //Cambia el valor la salida

in1: sal = in2;

in2: sal = in1;

endcase

end

endmodule
ENSAMBLADOR

Ejercicio 1:

.data

V: .word 2, -7, 6, -3, 10 @Vector

negat: .word -50 @Valor negativo para inicializar la comparativa de valores negativos

N: .word 5 @Nº de valores del vector

space1: .space 8 @Guardar espacio de memoria para suma de valores positivos

space2: .space 8 @Guardar espacio de memoria para el mayor número negativo

.text

main: ldr r0, =V

ldr r1, =space2

ldr r5, =space1

ldr r3, =negat

ldr r3, [r3]

mov r6, #0

mov r2, #0

ldr r7, =N

ldr r7, [r7] @Nº de valores del vector

bucle: ldr r4, [r0]

cmp r7, r2 @Comprobar si ha termionado el vector

beq stop

cond1: tst r4,r4 @Condicional si es positivo

bpl suma
cond2: cmp r4, r3 @Condicional del mayor de los negativos

bgt Neg

b bucle

Neg: add r3,r4,#0 @Dar el mayor valor de los negativos

str r3, [r1] @Guardar en memoria

add r0, r0, #4 @Pasar a la siguiente posición del vector

add r2,r2,#1 @Sumar posición del vector

b bucle

suma: add r6, r6, r4 @Sumar valores

str r6, [r5] @Guardar en memoria

add r0,r0,#4 @Pasar a la siguiente posición del vector

add r2,r2,#1 @Sumar posición del vector

b bucle

stop: wfi

Ejercicio laboratorio:

.data

V: .word 10, 20

n: .word 2

suma: .word 0

.text

main: ldr r0, =V

ldr r1, =n

ldr r1, [r1]

ldr r2, =suma

ldr r2, [r2]

mov r3, #0

bucle: cmp r3, r1

beq finbuc

ldr r4, [r0]

add r2, r2, r4

add r0, r0, #4

add r3, r3, #1

b bucle

finbuc: ldr r0, = suma

str r2, [r0]

stop: wfi
Desplazamiento aritmético a la derecha —arithmetic shift right—:
• «asr rd, rm, #Shift», desplaza el contenido de rm hacia la derecha conservando su signo, tantas
veces como las indicadas por el dato inmediato «Shift», y almacena el resultado en rd (es decir,
rd ← rm >>a Shif t).
• «asr rd, rs», desplaza el contenido de rd hacia la derecha conservando su signo, tantas veces
como las indicadas por el byte más bajo de rs (es decir, rd ← rd >>a rs). 3

Desplazamiento lógico a la derecha —logical shift right—:


• «lsr rd, rm, #Shift», desplaza el contenido de rm hacia la derecha rellenando con ceros por la
izquierda, tantas veces como las indicadas por el dato inmediato «Shift», y almacena el resultado
en rd (es decir, rd ← Rm >>l Shif t).
• «lsr rd, rs», desplaza el contenido de rd hacia la derecha rellenando con ceros por la izquierda,
tantas veces como las indicadas por el byte más bajo de rs (es decir, rd ← rd >>l rs).

Desplazamiento lógico a la izquierda —logical shift left—:


• «lsl rd, rm, #Shift», desplaza el contenido de rm hacia la izquierda rellenando con ceros por la
derecha, tantas veces como las indicadas por el dato inmediato «Shift», y almacena el resultado
en rd (es decir, rd ← rm << Shif t).
• «lsl rd, rs», desplaza el contenido de rd hacia la izquierda rellenando con ceros por la derecha,
tantas veces como las indicadas por el byte más bajo de rs (es decir, rd ← rd << rs)

You might also like