The technique has provided the clearest picture yet of how neural circuits function, and raised hopes of new therapies for depression and anxiety as well as cognitive problems.

The man in the hospital bed was playing video games on a laptop, absorbed and relaxed despite the bustle of scientists on all sides and the electrodes threaded through his skull and deep into his brain.

“O.K., that’s enough,” he told doctors after more than an hour. “All those memory tests, it’s exhausting.”

The man, Ralph, a health care worker who asked that his last name be omitted for privacy, has severe epilepsy; and the operation to find the source of his seizures had provided researchers an exquisite opportunity to study the biology of memory.

The Department of Defense on Tuesday announced a $40 million investment in what has become the fastest-moving branch of neuroscience: direct brain recording. Two centers, one at the University of Pennsylvania and the other at the University of California, Los Angeles, won contracts to develop brain implants for memory deficits.

Their aim is to develop new treatments for traumatic brain injury, the signature wound of the wars in Iraq and in Afghanistan. Its most devastating symptom is the blunting of memory and reasoning. Scientists have found in preliminary studies that they can sharpen some kinds of memory by directly recording, and stimulating, circuits deep in the brain.

Unlike brain imaging, direct brain recording allows scientists to conduct experiments while listening to the brain’s internal dialogue in real time, using epilepsy patients like Ralph or people with Parkinson’s disease as active collaborators.

The technique has provided the clearest picture yet of how neural circuits function, and raised hopes of new therapies for depression and anxiety as well as cognitive problems. But experts also worry about the possible side effects of directly tampering with memory.

“A decade ago, only a handful of centers had the expertise to perform such real-time experiments in the context of first-rate surgery,” said Michael Kahana, a neuroscientist at the University of Pennsylvania and the recipient of one of the new contracts granted by the Defense Advanced Research Projects Agency, or Darpa. “Today, there are dozens of them, and more on the way; this area is suddenly hot.”

Ralph was edgy on the way to the hospital. He knew that the “diagnostic evaluation” his doctor here at Thomas Jefferson University had recommended was no quick office procedure. It was a fishing expedition of sorts — in the depths of his own brain.

Epilepsy is one of medicine’s great mysteries. The seizures that characterize the disorder are caused by electrical storms in the brain that are as hard to predict as squalls on the open sea. They can erupt early in life, for reasons that may be partly genetic, and they are common after head injuries. But scientists cannot identify an exact cause.

What they do know is that many patients’ brains have a “hot spot” where the seizures originate — and that removing that pinch of tissue can reduce the symptoms, often drastically. The challenge: finding that spot in each person.

Since the 1950s, surgeons worked by instinct and experience, stimulating points on the brain’s surface, guided by the patient. Yet in people like Ralph, they need not only to map the brain’s surface but to sound its depths. They made punctures in the top of his skull and threaded 11 probes deep into his medial temporal lobes, near an area called the hippocampus, about level with the ear.

And then they listened and waited for a seizure to occur.

That wait can take two to three weeks, and surgeons are using this period to study patients who are awake and responsive with electrodes smack dab in areas of the brain that are most important for learning and memory.

The hippocampus is the very seat of memory formation, and its importance emerged from the study of an epilepsy patient whose procedure went famously awry. Henry Molaison, known worldwide as H.M., had severe seizures until a surgeon removed the hippocampus from both hemispheres of his brain in 1953. In a series of experiments, Brenda Milner of the Montreal Neurological Institute and McGill University showed that, without those seahorse-shaped organs, H.M. could form no new memories for facts, figures or faces. This finding, the most important in modern brain science, opened the way for direct-recording experiments.

“You put the electrodes in the brain for strictly clinical reasons and then come up with a good question that might be answered based on the location of the depth probes,” said Dr. Itzhak Fried, a professor of neurosurgery at Tel Aviv University and U.C.L.A., and the other recipient of a Darpa contract.

For example: How does the brain break down language to understand it? In a direct-recording study published early this year, neuroscientists at the University of California, San Francisco, found that the brain used just 12 distinct sound clusters to make meaning from words and sentences.

“Language is the most human of processes, and by recording, we were essentially able to show, for the first time, that the brain has this fundamental set of sounds that is like a periodic table of elements,” said Dr. Edward Chang, the neurosurgeon who led the team.

After the electrodes were implanted in Ralph’s brain, doctors recorded the moment-to-moment firing of tens of thousands of his neurons as he played memory games on the laptop. Later, they teased out discrete signals from the static that corresponded to specific mental actions during the laptop game, like recognizing a landmark in a virtual city.

“These readings are of great clinical value; the results help us not only ask basic questions about brain function but guide our decisions about what to do in this case — Ralph’s case,” said Dr. Ashwini Sharan, his surgeon at Thomas Jefferson.

Is further surgery — to cut out the seizure source — too risky?

Or is there another way to correct the problem?

The prospect of an answer to the second question is what attracted Defense Department officials. More than 270,000 veterans of the Iraq and Afghanistan wars have received a diagnosis of traumatic brain injury, or T.B.I. Darpa’s $40 million investment is in addition to more than $50 million the agency announced this spring to use direct brain recording techniques for mood problems from deployment; these commitments are in support of President Obama’s Brain Initiative, Darpa officials said.

Two years ago, researchers at U.C.L.A. found that they improved spatial memory by electrically stimulating an area near the hippocampus called the entorhinal cortex. The subjects played a virtual taxi-driver game in which the goal is to drop off passengers as quickly as possible in an unfamiliar city.

“The bottom line was that you turn this thing on, and later on, you remember better what you learned,” Dr. Fried said. Dr. Fried’s team will test the same technique more widely in epilepsy patients before trying it in people with T.B.I.

Dr. Kahana’s team will take a different approach. Working with several leading epilepsy centers, including Dr. Michael Sperling’s at Thomas Jefferson, the researchers will study the electrical signature of successful memory storage and retrieval in the brain — and attempt to isolate “biomarkers” for when that process goes wrong.

The frontier of the direct-recording approach is in implants that act something like thermostats, adjusting levels of simulation when signals in the brain become too faint or too noisy. Experts caution that some big practical and ethical questions remain. One is how closely stimulation truly mimics the brain’s internal language and whether such experiments may cause more harm than good.

“We have to keep reminding ourselves that, no, we are not speaking the brain’s secret language — we’re doing some very crude stimulating,” said Dr. Anthony Ritaccio, the director of neurosurgery at Albany Medical Center in New York. “When working with the brain, you have to keep slapping yourself in the face as a reality check; we still understand so little.”

Another is whether experiments done in people with healthy memory will help those with deficits.

“Just because stimulation helps normal brains with memory does not mean it will fix damaged brains, any more than putting higher octane gas in a car will help a car with a busted engine,” said Paul Root Wolpe, the director of the Center for Ethics at Emory University.

For Ralph, the results of his exploratory surgery were a cold dose of reality. The recordings showed that the region where his seizures originated overlapped with crucial memory organs. Removing that damaged area was possible but had a cost.

You are going to lose some memory if we do it, Dr. Sharan remembers telling him.

How much? Ralph asked.

I do not know for sure; but if your I.Q. is 120 now, it could go down to 80, the doctor said.

“That was too much,” Ralph said in an interview. “I still need to work. I can’t afford to lose that much.”