elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Entrar al Canal Oficial Telegram de elhacker.net


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Dudas con mi Micro Z80
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Dudas con mi Micro Z80  (Leído 1,645 veces)
NecBalaam

Desconectado Desconectado

Mensajes: 23



Ver Perfil
Dudas con mi Micro Z80
« en: 29 Septiembre 2011, 07:23 am »

Disculpen me estoy basando de este libro para hacer mi I.S.A. del micro Z80: www.zilog.com/docs/z80/um0080.pdf
Tengo este codigo inicial, para poder empezar a programar mis instrucciones:

Obviamente lo estoy haciendo en pascal, tengo muchas malas espinas, siento que me falta algo o siento que estoy mal en algo, espero me puedan decir, que me falta para poder empezar a programar mis instrucciones...

Y si de paso tienen algun material que me pueda facilitar el programado de mi I.S.A. micro se los agradeceria.

Citar
Código
  1. {$inline on}
  2. {$define DEBUG}
  3.  
  4. program emulador8080;
  5.  
  6. {$ifdef DEBUG}
  7. uses
  8.  crt    //Necesaria para el procedimiento clrscr
  9.  ;
  10. {$endif}
  11.  
  12. const
  13.  MAXMEM = $FFFF;
  14.  MAXPUERTOS= $FF;
  15.  VECTOR_RESET = $0000;
  16.  
  17. type
  18.  TDirMem = 0..MAXMEM;
  19.  TDirPuertos = 0..MAXPUERTOS;
  20.  
  21.  TMemoria = array[TDirMem] of byte;
  22.  TPuertos = array[TDirPuertos] of byte;
  23.  
  24.  TPSW = record
  25.    S,    
  26.    Z,
  27.    H, {Half Carry}
  28.    P, {Parity/Overflow}
  29.    N, {Add/Substract}
  30.    C {Carry} : boolean;
  31.  end;
  32.  
  33. var
  34.  Mem : TMemoria;
  35.  Puertos : TPuertos;
  36.  PSW : TPSW;
  37.  {General Purpose Register}
  38.  {Main Register Set}
  39.  A,
  40.  B,C,
  41.  D,E,
  42.  H,L,    
  43.  {Alternate Register Set}
  44.  A1,
  45.  B1,C1,
  46.  D1,E1,
  47.  H1,L1    : byte;
  48.  {Special Purpose Register}
  49.  IX,IY,SP,PC: word;
  50.  {I= Interruptor, R= Refresh}
  51.  I,R    : byte;
  52.  
  53.  INTE  : boolean;
  54.  Halt  : boolean;  
  55.  
  56. {=============================================================================
  57.  Interfaz de control del Emulador =============================================================================}
  58.  
  59. {$IFDEF DEBUG}
  60.  
  61. function Desensamblado(I: byte): string;
  62.  function NombreRegistro(r : byte): string; inline;
  63.  var
  64.    op  : string;
  65.  begin
  66.    case r of
  67.    0: op := 'B';
  68.    1: op := 'C';
  69.    2: op := 'D';
  70.    3: op := 'E';
  71.    4: op := 'H';
  72.    5: op := 'L';
  73.    6: op := '[HL]';
  74.    7: op := 'A';
  75.    end;
  76.    NombreRegistro := op;
  77.  end;
  78.  
  79. begin
  80.    case I of        {Decodify}
  81.    $00:  Desensamblado := 'NOP';        {Execute}
  82.  end;
  83. end;
  84.  
  85. procedure Interfaz_CargarContenidoMemoria;
  86. var
  87.  F : file of byte;
  88.  Nombre : string;
  89.  i     : word;
  90.  b     : byte;
  91. begin
  92.  write('Nombre del archivo: '); readln(Nombre);
  93.  assign(F, Nombre);
  94.  system.reset(F);
  95.  i := 0;
  96.  while not eof(F) do
  97.  begin
  98.    read(F,b);
  99.    Mem[i] := b;
  100.    i := i + 1;
  101.  end;
  102.  close(F);
  103. end;
  104.  
  105. procedure Interfaz_GuardarContenidoMemoria;
  106. var
  107.  F : file of byte;
  108.  Nombre : string;
  109.  i     : word;
  110. begin
  111.  write('Nombre del archivo: '); readln(Nombre);
  112.  assign(F, Nombre);
  113.  system.rewrite(F);
  114.  for i := 0 to MAXMEM do
  115.    write(F,Mem[i]);
  116.  close(F);
  117. end;
  118.  
  119. procedure Interfaz_Desplegar;
  120.  procedure DesplegarPSW;
  121.  begin
  122.    with PSW do
  123.    begin
  124.      if S then write('S(X)') else write('S( )');
  125.      if Z  then write(' Z(X)') else write(' Z( )');
  126.      if H  then write(' H(X)') else write(' H( )');
  127.      if P  then write(' P(X)') else write(' P( )');
  128.      if N  then write(' N(X)') else write(' N( )');
  129.      if C  then writeln(' C(X)') else writeln(' C( )');
  130.    end;
  131.  end;
  132. var
  133.  S,s2 : char;
  134.  valor : byte;
  135.  valor16 : word;
  136. begin
  137.  clrscr;
  138.  DesplegarPSW;
  139.  writeln('A= ', A);
  140.  write('B= ', B);    writeln(' C= ', C);
  141.  write('D= ', D);    writeln(' E= ', E);
  142.  write('H= ', H);    writeln(' L= ', L);
  143.  writeln('IX= ', IX);
  144.  writeln('IY= ', IY);
  145.  writeln('SP= ', SP);
  146.  writeln('PC= ', PC);
  147.  write('I= ', I); writeln(' : ', Desensamblado(i));
  148.  write('Q=salir, [ABCDEHL]=regs, F=PSW, M=mem, X=IX, Y=IY, P=PC, S=SP, R=leer, W=escribir, ENTER=continuar: ');
  149.  readln(S);
  150.  S := upcase(S);
  151.  case S of
  152.  'Q': Halt := true;
  153.  'A': begin
  154.          write('A= '); readln(valor);
  155.          A := valor;
  156.       end;
  157.  'B': begin
  158.          write('B= '); readln(valor);
  159.          B := valor;
  160.       end;
  161.  'C': begin
  162.          write('C= '); readln(valor);
  163.          C := valor;
  164.       end;
  165.  'D': begin
  166.          write('D= '); readln(valor);
  167.          D := valor;
  168.       end;
  169.  'E': begin
  170.          write('E= '); readln(valor);
  171.          E := valor;
  172.       end;
  173.  'F': begin
  174.          write('Bandera [SZHPNC]'); readln(S2);
  175.          S2 := upcase(S2);
  176.          case S2 of
  177.          'S':    PSW.S  := not PSW.S;
  178.          'Z':    PSW.Z  := not PSW.Z;
  179.          'H':    PSW.H  := not PSW.H;
  180.          'P':    PSW.P  := not PSW.P;
  181.          'N':    PSW.N  := not PSW.N;
  182.          'C':  PSW.C  := not PSW.C;
  183.          end;
  184.       end;
  185.  'H': begin
  186.          write('H= '); readln(valor);
  187.          H := valor;
  188.       end;
  189.  'L': begin
  190.          write('L= '); readln(valor);
  191.          L := valor;
  192.       end;
  193.  'P': begin
  194.          write('PC= '); readln(valor16);
  195.          PC := valor16;
  196.       end;
  197.  'S': begin
  198.          write('SP= '); readln(valor16);
  199.          SP := valor16;
  200.       end;
  201.  'M': begin
  202.          write('Dir= '); readln(valor16);
  203.          write('valor= '); readln(valor);
  204.          Mem[valor16] := valor;
  205.       end;
  206.  'R': Interfaz_CargarContenidoMemoria;
  207.  'X': begin
  208.          write('IX= '); readln(valor16);
  209.          IX := valor16;
  210.       end;
  211.  'Y': begin
  212.          write('IY= '); readln(valor16);
  213.          IY := valor16;
  214.       end;
  215.  'W': Interfaz_GuardarContenidoMemoria;
  216.  end;
  217. end;
  218.  
  219. {$ENDIF}
  220.  
  221. {=============================================================================
  222.  Funciones relativas al PSW  =============================================================================}
  223.  
  224. function PSW_ParidadPar: boolean; inline;
  225. var
  226.  conta : byte;    //Número de 1's presentes en A
  227.  i     : byte;
  228. begin
  229.  conta := 0;
  230.  for i := 0 to 7 do
  231.    conta := conta + (A shr i) and $01;
  232.  PSW_ParidadPar := (conta mod 2) = 0;
  233. end;
  234.  
  235. procedure PSW_ActualizarLogico(temp  : byte); inline;
  236. begin
  237.  with PSW do
  238.  begin
  239.    S  := (temp and $80) = $80;    
  240.    Z  := temp = $00;    
  241.    H := false;
  242.    P  := PSW_ParidadPar;    
  243.    C  := false;
  244.  end;
  245. end;
  246.  
  247. procedure PSW_ActualizarAritmetico(r1, r2 : byte; temp : word); inline;
  248. begin
  249.  with PSW do
  250.  begin
  251.    S  := (temp and $80) = $80;
  252.    Z  := temp = $00;
  253.    PSW.H := ((temp xor r1 xor r2) and $10) = $10;
  254.    P  := PSW_ParidadPar;
  255.    PSW.C  := temp > 255;
  256.  end;
  257. end;
  258.  
  259. {=============================================================================
  260.  Funciones relativas a la ALU  =============================================================================}
  261.  
  262. function ALU_add(r1, r2, Cin : byte): byte; inline; {ADD}
  263. var
  264.  temp : word;
  265. begin
  266.  temp   := r1 + r2 + Cin;
  267.  PSW_ActualizarAritmetico(r1, r2, temp);
  268.  ALU_add := temp;
  269. end;
  270.  
  271. function ALU_sub(r1, r2, Bin : byte): byte; inline; {Subtract}
  272. var
  273.  temp : word;
  274. begin
  275.  temp   := r1 - r2 - Bin;
  276.  PSW_ActualizarAritmetico(r1, r2, temp);
  277.  ALU_sub := temp;
  278. end;
  279.  
  280.  
  281. function ALU_and(r1, r2 : byte): byte; inline;  {Logical AND}
  282. var
  283.  temp : byte;
  284. begin
  285.  temp := r1 and r2;
  286.  PSW_ActualizarLogico(temp);
  287.  ALU_and := temp;
  288. end;
  289.  
  290. function ALU_or(r1, r2 : byte): byte; inline; {Logical OR}
  291. var
  292.  temp : byte;
  293. begin
  294.  temp := r1 or r2;
  295.  PSW_ActualizarLogico(temp);
  296.  ALU_or := temp;
  297. end;
  298.  
  299. function ALU_xor(r1, r2 : byte): byte; inline; {Logical Exclusive OR}
  300. var
  301.  temp : byte;
  302. begin
  303.  temp := r1 xor r2;
  304.  PSW_ActualizarLogico(temp);
  305.  ALU_xor := temp;
  306. end;
  307.  
  308. function ALU_bcd(r : byte): byte; inline;
  309. var
  310. temp:byte;
  311. begin
  312.  temp:=(r and $0f);
  313.  ALU_bcd:=((r shr 4)*16)+temp;
  314. end;
  315.  
  316. {=============================================================================
  317.  Funciones relativas a la Unidad de Control =============================================================================}
  318.  
  319. procedure CPU_VerificarInterrupciones;
  320. begin
  321. end;
  322.  
  323. procedure CPU_Reset;
  324. begin
  325.  A := $00;
  326.  B := $00;    C := $00;
  327.  D := $00;    E := $00;
  328.  H := $00;    L := $00;
  329.  with PSW do
  330.  begin
  331.    S := false;    
  332.    Z := true;
  333.    H := false;    
  334.    P  := false;    //NOTA : Checar      
  335.    C := false;
  336.  end;
  337.  SP := MAXMEM;
  338.  PC := VECTOR_RESET;
  339.  Halt := false;
  340.  INTE := true; {NOTA: Los estudiante deben verificar esto.}
  341. end;
  342.  
  343. {=============================================================================
  344.  Funciones relativas a la Memoria =============================================================================}
  345.  
  346. function Memoria_Leer(d : word): byte; inline;
  347. begin
  348.  Memoria_Leer := Mem[d];
  349. end;
  350.  
  351. procedure Memoria_Escribir(d : word; b : byte); inline;
  352. begin
  353.  Mem[d] := b;
  354. end;
  355.  
  356. function Memoria_DecodificarDirecto: word; inline;
  357. begin
  358.  Memoria_DecodificarDirecto := (Memoria_Leer(PC+2) shl 8) or Memoria_Leer(PC+1);
  359. end;
  360.  
  361. function Memoria_LeerInmediato: byte; inline;
  362. begin
  363.  Memoria_LeerInmediato := Memoria_Leer(PC+1);
  364. end;
  365.  
  366. function Memoria_LeerDirecto: byte; inline;
  367. begin
  368.  Memoria_LeerDirecto := Memoria_Leer( Memoria_DecodificarDirecto );
  369. end;
  370.  
  371. procedure Memoria_EscribirDirecto(v : byte);
  372. begin
  373.  Memoria_Escribir( Memoria_DecodificarDirecto, v );
  374. end;
  375.  
  376. function Memoria_LeerPila: word; inline;
  377. var
  378.  alto, bajo : byte;
  379. begin
  380.  bajo := Memoria_Leer(SP);
  381.  SP := SP + 1;
  382.  alto := Memoria_Leer(SP);
  383.  SP := SP + 1;
  384.  Memoria_LeerPila := (alto shl 8) or bajo;
  385. end;
  386.  
  387. procedure Memoria_EscribirPila(v : word);
  388. begin
  389.  SP := SP - 2;
  390.  Memoria_Escribir(SP, v and $00FF);
  391.  Memoria_Escribir(SP+1, (v shr 8) and $00FF);
  392. end;
  393.  
  394. {=============================================================================
  395.  Funciones relativas a los Registros
  396.  =============================================================================}
  397.  
  398. procedure Registros_Escribir(r : byte; v : byte);
  399. begin
  400.  case r of
  401.  $00: B := v;
  402.  $01: C := v;
  403.  $02: D := v;
  404.  $03: E := v;
  405.  $04: H := v;
  406.  $05: L := v;
  407.  $06: Mem[(H shl 8) or L] := v;
  408.  $07: A := v;
  409.  end;
  410. end;
  411.  
  412. function Registros_Leer(r : byte): byte; inline;
  413. var
  414.  op  : byte;
  415. begin
  416.  case r of
  417.  0: op := B;
  418.  1: op := C;
  419.  2: op := D;
  420.  3: op := E;
  421.  4: op := H;
  422.  5: op := L;
  423.  6: op := Mem[(H shl 8) or L];
  424.  7: op := A;
  425.  end;
  426.  Registros_Leer := op;
  427. end;
  428.  
  429. function Registros_LeerPar(I : byte): word; inline;
  430. var
  431.  r : byte;
  432.  temp : word;
  433. begin
  434.  r := (I and $30) shr 4;
  435.  case r of
  436.  $00: temp := (B shl 8) or C;
  437.  $01: temp := (D shl 8) or E;
  438.  $02: temp := (H shl 8) or L;
  439.  $03: temp := SP;
  440.  end;
  441.  Registros_LeerPar := temp;
  442. end;
  443.  
  444. procedure Registros_EscribirPar(I: byte; v : word);
  445. var
  446.  r : byte;
  447. begin
  448.  r := (I and $30) shr 4;
  449.  case r of
  450.  $00: begin
  451.        B := (v and $FF00) shr 8;
  452.        C := (v and $00FF);
  453.      end;
  454.  $01: begin
  455.        D := (v and $FF00) shr 8;
  456.        E := (v and $00FF);
  457.      end;
  458.  
  459.  $02: begin
  460.        H := (v and $FF00) shr 8;
  461.        L := (v and $00FF);
  462.      end;
  463.  
  464.  $03: SP := v;
  465.  end;
  466. end;
  467.  
  468. procedure IXIY(Registro : boolean);
  469. var
  470. Reg: word;
  471. begin
  472.      if Registro= true then
  473.      Reg:=IX
  474.      else Reg:=IY;
  475.    PC:=PC+1;
  476.    I:=Mem[PC];
  477. end;
  478.  
  479. {=============================================================================
  480.  Instrucciones del Procesador  Z80 =============================================================================}
  481.  
  482. procedure InstruccionDesconocida( ByteCode : byte);
  483. begin
  484.  writeln(stderr, 'Instrucción desconocida: ', ByteCode);
  485.  PC := PC + 1;
  486. end;
  487.  
  488. procedure NOP;
  489. begin
  490.  PC := PC + 1;
  491. end;
  492.  
  493. {=============================================================================
  494.  R U T I N A   P R I N C I P A L   D E L   E M U L A D O R  
  495.  =============================================================================}
  496. begin
  497.  {Arranque de la máquina (RESET)}
  498.  CPU_Reset;
  499.  {Ciclo de Instrucción}
  500.  repeat
  501.    I := Mem[PC];    {Fetch}
  502.    case I of        {Decodify}
  503.    $00:  NOP;        {Execute}
  504.    else
  505.      InstruccionDesconocida(I);
  506.    end;
  507.    if INTE then
  508.      CPU_VerificarInterrupciones;
  509. {$ifdef DEBUG}
  510.    Interfaz_Desplegar;
  511. {$endif}
  512.  until Halt;
  513. end.
  514.  


En línea

No esperes tener el 100% de todo, por que no tendrás nada, mejor trabaja en equipo y obtendrás el 50% de algo.
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Pasar un micro a otro micro
Electrónica
Meta 0 3,897 Último mensaje 15 Octubre 2008, 22:53 pm
por Meta
MOVIDO: Dudas con mi Micro Z80
ASM
Eternal Idol 0 2,283 Último mensaje 29 Septiembre 2011, 11:00 am
por Eternal Idol
Dudas de placa base y micro.
Hardware
mortenol 6 3,168 Último mensaje 10 Junio 2022, 02:15 am
por MinusFour
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines