Where can I find the Arduino Mega core code?
Arduino Mega is part of the Arduino AVR Boards core. The location of that core depends on whether you are using the core version included with the Arduino IDE or have updated the core version using Boards Manager(in which case there will be multiple copies of the core installed on your computer but only the updated version will be used). The easiest way to find it is:


Tools > Board > Arduino/Genuino Mega or Mega 2560
File > Examples > EEPROM > eeprom_clear
Sketch > Show Sketch Folder
Move up folder levels until you reach the avr folder.
If there is a version number folder in the avr folder then open that folder.
HardwareSerial.h and the rest of the core files are located in cores/arduino.


Remember that you will need to redo any changes to the core after any Arduino IDE and/or Arduino AVR Boards updates.