0

UPDATE: Updated the code and the xxd output.

I asked this question before, but I don't believe I explained it well enough to get an answer. I am attempting to generate a file to load on to an EPROM like in the photo. enter image description here

I need to create a binary file which will output 16-bit data on the data pins. Such that, for example, when I go to address 0xFF (e.g. 5 volts on the first 8 address pins) it would output 0xAD12 (on the data pins) for example. In binary this would mean: 1111000100010010 and the EPROM would output a 1 (5 volts) on pin 2, 5, 9, 13, 14, 15, 16.

However, when I load it on the EPROM it seems to have only put the lower 8-bit values into the file and all the MSB are high (which they are when the EPROM is blank) so it doesn't seem to be writing those bits. I don't seem to be able to see the 8 MSB using hexdump or xxd?

I've written a quick example. I tried to make it as near to my actual program as possible. I realise the datasize is huge, but it is the size of file I need to fill the EPROM.

#include <stdio.h>                                                                                                    
#include <stdlib.h>                                                                                                   
#include <stdint.h>                                                                                                   
                                                                                                                      
#define scRom1024 16384                                                                                               
uint16_t firstEpromFile[scRom1024] =  { 0xFFFF, 0xFFDD, 0xFDDD, 0xF000, 0x0F0F, 0x0001, 0x1010 };                     
                                                                                                                      
int main ()                                                                                                           
{                                                                                                                     
                                                                                                                      
  const char *path = "text.bin";                                                                                      
  FILE *fp = fopen(path, "wb");
  const void *data = firstEpromFile;                                                                                  
                                                                                                                      
  if (!fp)                                                                                                           
   {                                                                                                                  
    fprintf(stderr, "fopen() failed for '%s'\n", path);                                                               
  }                                                                                                                   
  else                                                                                                                
    {                                                                                                                 
      fwrite(firstEpromFile, sizeof firstEpromFile[0], sizeof firstEpromFile / sizeof firstEpromFile[0], fp);         
      fclose(fp);                                                                                                     
    }                                                                                                                 
                                                                                                                      
  return 0;                                                                                                           
}                                                                                                                     


xxd shows only the 8 LSB.

00000000: 11111111 11111111 11011101 11111111 11011101 11111101  ......
00000006: 00000000 11110000 00001111 00001111 00000001 00000000  ......

== hex version ===

00000000: ffff ddff ddfd 00f0 0f0f 0100 1010 0000  ................

So my question is:

Is the program correctly writing out the 16 bit values and I can't see them with xxd (seems unlikely since the physical hardware is outputting 5volts on pins 9-16) or do I need to modify the code so that it will output 16-bit values?

Rick Dearman
  • 356
  • 2
  • 12
  • 1
    What is `data`? Should that be `firstEpromFile`? – Barmar Feb 13 '21 at 17:46
  • And why are you calling `fwrite` with a size parameter of 1 byte (8 bits)? Looks like you should have 2 (16 bits) for each array element. – Adrian Mole Feb 13 '21 at 17:47
  • Try `fwrite(firstEpromFile, sizeof firstEpromFile[0], sizeof firstEpromFile / sizeof firstEpromFile[0], fp);` – chux - Reinstate Monica Feb 13 '21 at 17:50
  • data seems to have been shifted way left when I pasted it. – Rick Dearman Feb 13 '21 at 17:50
  • "data seems to have been shifted way left when I pasted it. " --> Research _endian_. – chux - Reinstate Monica Feb 13 '21 at 17:52
  • Even simpler: `fwrite(firstEpromFile, sizeof(firstEpromFile), 1, fp)` – Barmar Feb 13 '21 at 17:53
  • No, I mean stackoverflow editor shifted the line: const void *data = firstEpromFile; You have to move the slider bar to see it. – Rick Dearman Feb 13 '21 at 17:53
  • None of the changes to fwrite() made any difference to what I can see in xxd or hexdump. They still only show the 8 least significant bits. – Rick Dearman Feb 13 '21 at 17:57
  • I don't think the photo serves any purpose here - or even the explanation of how an EPROM works for that matter. It has been a many years since I've seen a UV EPROM in use! – Clifford Feb 13 '21 at 18:06
  • You are writing 16-bit [`little-endian`](https://en.wikipedia.org/wiki/Endianness) integers to the file. The hex dump looks correct to me. – Blastfurnace Feb 13 '21 at 18:09
  • I added the photo because in the previous question people didn't seem to understand why I must have 16 bit values. – Rick Dearman Feb 13 '21 at 18:10
  • In which case just say it is for an AM27C1024 EPROM. You might also tag it [EPROM] and [embedded] so people who understand what you are referring to will see it. Since you have inspected the file _before_ you write the EPROM it is clearly not the code that is the problem (so not an SO question). It seems more likely to me that you have configured your EPROM programmer for an 8-bit EPROM with 16 bit data. In that case it will write even addressed bytes to one EPROM, and expect to write odd addressed bytes to a second EPROM. – Clifford Feb 13 '21 at 18:18
  • "_xxd shows only the 8 LSB._" : Clearly that is untrue. It is in little-endian order, but all the data is there. You are relying on the byte order of the machine generating the file to be the same as that of the machine reading the EEPROM. What is reading the the EPROM? – Clifford Feb 13 '21 at 18:29
  • For example if you swap the bytes `ddff` you get the `0xFFDD` you used in the intialiser. Byte order error (or minsunderstanding) does not explain why the MSB is high - that still is down to your EPROM programmer configuration. If you have a question about the byte order though that remains a legitimate question. – Clifford Feb 13 '21 at 18:39

1 Answers1

0

The program is outputing the correct data. The modification of using

fwrite(firstEpromFile, sizeof firstEpromFile[0], sizeof firstEpromFile / sizeof firstEpromFile[0], fp);

fixed the issue I originally had. There was a second issue with the EPROM programmer I was using because I wasn't specifying that the file was INTEL and so it was trying (I think) to use big-endian values.

Thank you to everyone for your suggestions and comments! It was very helpful!

Rick Dearman
  • 356
  • 2
  • 12